算法训练 栈
算法训练 栈
算法训练 栈

#include<iostream>
#include<string>
using namespace std;
int num = 100000;
string stack[100000];
int top, n;
void push(string name) {
	stack[++top] = name; //先++ 再赋值为top 
}
string pop() {
	return stack[top--];//先返回栈顶层 再将top向下移动 
}
string query(int pos) {
	return stack[pos];
}
int main() {
	cin >> n;
	while (n--) {
		int op;
		string name;
		cin >> op;
		if (op == 1) {
			cin >> name;
			push(name);
		}
		else if (op == 2) {
			cout << pop() << endl;
		}
		else if (op == 3) {
			int num;
			cin >> num;
			cout << query(num) << endl;
		}
	}
	return 0;
}

相关文章: