【王道机试】-栈-堆栈的使用-吉林大学

//#include "pch.h"
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main() {
	int n;
	cin >> n;
	char opp;
	int numm;
	stack<int> st;
	queue<char> op;
	queue<int> num;
	for (int i = 0; i < n; i++) {
		cin >> opp;
		op.push(opp);
		if (opp == 'P') {
			cin >> numm;
			num.push(numm);
		}
	}
	
	for (int i = 0; i < n; i++) {
		opp = op.front();
		if (opp == 'A') {
			if (st.empty())
				cout << "E" << endl;
			else
				cout << st.top() << endl;
		}
		if (opp == 'P') {
			numm = num.front();
			st.push(numm);
			num.pop();
		}
		if (opp == 'O') {
			if (!st.empty())
				st.pop();
		}
		op.pop();
	}
	cout << endl;
	

}

猜你喜欢

转载自blog.csdn.net/qq_39328436/article/details/114656970