---
# System prepended metadata

title: 資料結構 20221024_Queue
tags: [java, Data Structure]

---

# 資料結構 20221024_Queue

>撰寫人[name=AmiYaku1049] [首頁--天空路1049號](https://skys-kid-lai.github.io/1004/)
>>最後編輯[time=Wed, Jan 18, 2023 8:04 PM]
___
## 題目敘述
- 請設計一個佇列結構，此結構用來記錄急診室七個診間病人的叫號順序
可選擇使用陣列、也可選擇使用串列
可使用一般佇列、也可使用環狀佇列、也可使用雙向佇列

- 病人資料
時間序號(202210240900)
身分證號碼(一碼英文+九碼數字)
連絡電話(十碼數字)

- 功能
1.新增叫號病人
2.移除結束看診之病人
3.顯示候診順序

## 程式碼
```java=
import java.io.*;
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;

class ClassWork_1110832059_1024_Queue 
{
	private String password, phonenum, datetime;
	
	//checking for charters and length of Date time is correct
	public String MathDate(String datetime)
	{
		Scanner scn = new Scanner(System.in);

		this.datetime = datetime;
		int rite = 0;
		//looking for which charter is not a number or input's length is not correct
		for(int i = 0; i < datetime.length(); i++)
		{
			rite = datetime.charAt(i);
			
			if((rite < 47 || rite > 58) || datetime.length() < 12)
			{
				System.out.println("	Format is not correct ! !\n");
				System.out.print("	input yyyyddddtttt: ");
				datetime = scn.next();
				return MathDate(datetime);
			}
		}
		return datetime;
	}

	//checking for first word and another words of ID number are correct
	public String MathPass(String password)
	{
		Scanner scn = new Scanner(System.in);

		this.password = password;
		int rite = 0, sum = 0;
		for(int i = 0; i < password.length(); i ++)
		{
			rite = password.charAt(i);
			//checing for first charter is a word at english
			if(i == 0 && (rite > 64 && rite < 91))
				sum ++;
			//checking for other of first word are number
			else if(i >= 1 && (rite > 47 && rite < 58))
				sum ++;
			else
			{
				System.out.println("	Format is not correct !\n" );
				System.out.print("	input ex:A123456789: ");
				password = scn.next();
				return MathPass(password);
			}

			if(password.length() != 10)
			{
				System.out.println("	Format is not correct !\n" );
				System.out.print("	input ex:A123456789: ");
				password = scn.next();
				return MathPass(password);
			}
		}
		return password;
	}
	
	//checking for length and charters of phone number are correct
	public String MathPhone(String phonenum)
	{
		Scanner scn = new Scanner(System.in);

		this.phonenum = phonenum;
		int rite = 0;
		//checking for length of phone number is correct
		if(phonenum.length() != 10)
		{
			System.out.println("	Format is not correct !\n" );
			System.out.print("	input 09xxxxxxxx: ");
			phonenum = scn.next();
			return MathPhone(phonenum);	
		}
		//checking for charters of phone number are correct
		for(int i = 0; i < phonenum.length(); i++)
		{
			rite = phonenum.charAt(i);
			if(rite < 47 || rite > 58)
			{
				System.out.println("	Format is not correct !\n");
				System.out.print("	input 09xxxxxxxx: ");
				phonenum = scn.next();
				return MathPhone(phonenum);
			}
		}
		return phonenum;
	}

	//Main
	public static void main(String[] args) throws IOException
	{
		ClassWork_1110832059_1024_Queue sequl = new ClassWork_1110832059_1024_Queue();
		Queue<String> queue = new LinkedList<String>();

		Scanner scn = new Scanner(System.in);
		String time,password,phonenum;


		while(1 == 1)
		{
			System.out.print("insert[n], see all[a], delete[d], over program[b]: ");
            String choise = scn.next();
			System.out.println();
			switch(choise)
			{
				//Select
				case "n":
					if(queue.size() < 21)
					{
						//date
						System.out.print("	input yyyyddddtttt: ");
						time = scn.next();
						time = sequl.MathDate(time);
						queue.offer(time);
					
						//password
						System.out.print("	input ex:A123456789: ");
						password = scn.next();
						password = sequl.MathPass(password);
						queue.offer(password);
					
						//phone number
						System.out.print("	input 09xxxxxxxx: ");
						phonenum = scn.next();
						phonenum = sequl.MathPhone(phonenum);
						queue.offer(phonenum);

						//********list.insert(time,password,phonenum); //Node
						System.out.println("------------------");
					}
					else if(queue.size() >= 21)
					{
						System.out.println("List is full ! Please delete some data !");
					}
					System.out.println();
					break;

				//Watch all in queue
				case "a":
                    System.out.println("Disp List");
                    //list.print(); //Node
					System.out.println();
					int train = 0;
					for(String q : queue)
					{
						if(train < 3)
							System.out.print(q +"             ");
						if(train == 3)
						{
							System.out.println();
							System.out.print(q +"             ");
							train = 0;
						}
						train ++;
					}
					System.out.println("\n------------------");
					System.out.println("\n");
					break;

				//Delete one which at queue first sit
				case "d":
					if(queue.peek() != null)
					{
						System.out.println("Delete this data now: ");
						for(int i = 0; i < 3; i++)
						{
							//System.out.print(queue.remove() +"             ");
							System.out.print(queue.poll() +"             ");
						}
						System.out.println();
					}
					else if(queue.peek() == null)
						System.out.println("No data here now, please insert data !");

					System.out.println("\n");
					break;

				//end program
				case "b":
					System.exit(1);
					break;
			}
		}
	}
}
```

## 結果圖示
![](https://i.imgur.com/lN3vM39.png =70%x)


## 用書資訊：
>書名：圖解資料結構 使用java（第三版）
>作者：吳燦銘、胡昭民
>出版社：博碩文化
>出版年份：2018年05月