使用單向連結表 (single linked list) 寫一個 C++ 堆疊 (stack) 類別 (class) 的程式。堆疊的主要運算如下:

  1. void push(int): 將一個節點加入堆疊的頂部;

  2. int pop(void): 從堆疊頂部的節點移除;

  3. int top(void): 檢查堆疊頂部節點的值;

  4. bool isempty(void): 檢查堆疊是否為空;

  5. void reverse(Stack &): 將堆疊反向倒置

  6. ostream &operator<<(ostream&, const Stack&):友函式,從底部至頂部輸出堆疊中所有節點的值。

在主程式中宣告一個佇列物件的變數,並寫一個無限迴圈 (infinite loop),選擇並執行下列的指令:

  1. 將一個節點加入堆疊的頂部;

  2. 從堆疊頂部的節點移除;

  3. 測試堆疊是否為空的;

  4. 從底部至頂部輸出堆疊中所有節點的值;

  5. 終止程式執行。

以下為程式執行範例:

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 1

Input the element being pushed: 20

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 1

Input the element being pushed: 15

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 1

Input the element being pushed: 30

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 4

The stack elements are (from bottom to top):
    20
    15
    30

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 2

The poped element: 30

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 3

The top element: 15

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 1

Input the element being pushed: 10

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 4

The stack elements are (from bottom to top):
    20
    15
    10

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

1. Push an elemnet to the stack
2. Pop an element from the stack
3. Print the top element
4. List all stack elements
5. Exit the program

Input command: 5