Programming Practice

使用雙向連結表 (double-linked list) 實作一個佇列 (queue)。佇列的主要運算如下:

  1. initial_queue():設排序定連結表成為空的連結表,

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

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

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

  5. print_queue_reverse():從尾部至頭部列印佇列中所有節點的值。

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

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

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

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

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

  5. 終止程式執行。

以下為程式執行範例:

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:
4

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

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

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