The data structure and algorithm problem stack is a true Study of computer science at Jilin University in 2011.

Source: Internet
Author: User
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;}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.