Programming Practice

使用陣列 (array) 實作一個佇列 (queue)。佇列的主要運算如下:

  1. initial_queue():設排序定連結表成為空的連結表,將陣列設定為一個選擇的長度 (如 10 個元素);

  2. enqueue():將某個值的節點放入佇列的尾部 (tail);

  3. dequeue():將佇列頭部 (head) 的節點移出,並回傳該節點的值 ;

  4. head(): 回傳佇列頭部的元素;

  5. isEmpty():檢查佇列是否為空;

  6. print_queue():從頭部至尾部列印佇列中所有節點的值 ;

在主程式中宣告一個佇列連結表型態的變數,然後使用 initial_queue() 產生這個佇列連結表設為空的連結表。寫一個無限迴圈 (infinite loop),選擇並執行下列的指令:

  1. 將某個值的節點放入佇列的尾部,

  2. 將佇列的頭部的節點移出,並印出該節點的值

  3. 輸出頭部的元素,

  4. 測試佇列是否為空,

  5. 從頭部至尾部列印佇列中所有節點的值

  6. 終止程式執行。

以下為程式執行範例:

1. Enqueue
2. Dequeue
3. Print queue
4. Print queue reverse
5. Quit
**** Enter a command:
1

Enter an enqueued element:
50
Element 50 have been enqueued.

****************************************************

1. Enqueue
2. Dequeue
3. Print queue
4. Print queue reverse
5. Quit
**** Enter a command:
1

Enter an enqueued element:
64
Element 64 have been enqueued.

****************************************************

1. Enqueue
2. Dequeue
3. Print queue
4. Print queue reverse
5. Quit
**** Enter a command: 1


Enter an enqueued element:
40
Element 40 have been enqueued.

****************************************************

1. Enqueue
2. Dequeue
3. Print queue
4. Print queue reverse
5. Quit
**** Enter a command:
3

The element(s) of queue from the head to the tail are (is):
 50  64  40

****************************************************

1. Enqueue
2. Dequeue
3. Print queue
4. Print queue reverse
5. Quit
**** Enter a command:
2

Element 50 has been dequeued.

****************************************************

1. Enqueue
2. Dequeue
3. Print queue
4. Print queue reverse
5. Quit
**** Enter a command:
1

Enter an enqueued element:
86
Element 86 have been enqueued.

****************************************************

1. Enqueue
2. Dequeue
3. Print queue
4. Print queue reverse
5. Quit
**** Enter a command:
1

Enter an enqueued element:
18
Element 18 have been enqueued.

****************************************************

1. Enqueue
2. Dequeue
3. Print queue
4. Print queue reverse
5. Quit
**** Enter a command:
5