-
Description:
-
A stack is a basic data structure. Stack has two basic operations: Push and pop. Push a value to the top of the stack, and pop will pop up the value at the top of the stack. Now let's verify the usage of the stack.
-
Input:
-
For each group of test data, the first row is a positive integer N, 0 <n <= 10000 (n = 0 ). In the next n rows, the first character of each line may be 'P', 'O', or 'A'. If it is 'P', it will be followed by an integer, this means to push the data into the stack. If it is 'O', it means to pop the value at the top of the stack. If there are no elements in the stack, ignore this operation. If it is 'A ', indicates the value at the top of the current stack. If the current stack is empty, 'E' is output '. Stack start is empty.
-
Output:
-
For each group of test data, the stack is processed according to the command characters. For all the 'A' operations, the value at the top of the stack is output, each occupies one row, if the stack is empty at that time, 'E' is output '. After each group of test data is complete, an empty row is output.
-
Sample input:
-
3AP 5A4P 3P 6O A0
Sample output:
E
5
3
Code:
#include <iostream>#include <stack>using namespace std;int main(){int n, data;char a;cin >> n;while (n != 0&&n<=10000){stack<int >s;for (int i = 0; i < n; i++){cin >> a;switch (a){case 'P':{cin >> data;s.push(data);break;}case 'O':{if (!s.empty()){s.pop();}break;}case 'A':{if (s.empty()){cout << "E";cout << endl;break;}else{cout << s.top();cout << endl;break;}}default:break;}}cout << endl;cin >> n;}}