使用陣列 (array) 寫一個 C++ 堆疊 (stack) 類別 (class) 的程式。堆疊的主要運算如下:

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

  2. int pop(void): 從堆疊的頂部移除一個元素;

  3. int top(void): 檢查堆疊頂部的元素;

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

在主程式中宣告一個佇列物件的變數,並寫一個無限迴圈(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