acwing828模拟栈,数组模拟栈

板子如下
#include <bits/stdc++.h>
using namespace std;

const int N=100005;
int n,Stack[N],tt,x;//tt是top 

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	tt=0;
	cin>>n;
	while(n--){
		string op;
		cin>>op;
		if(op=="push"){//压人x 
			cin>>x; Stack[++tt]=x;
		}else if(op=="pop"){
			--tt;
		}else if(op=="empty"){//是否为空 
			if(tt==0) cout<<"YES\n";
			else cout<<"NO\n";
		}else cout<<Stack[tt]<<endl;//取top值 
	}
	return 0;
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务