跳到正文

算法·理论

栈

Charles_with_wkc约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
stack<unsigned long long int>str;
string s,a="push",b="pop",c="query",d="size";
unsigned long long int x;
long long int n,t;
void clean(){
	for(;;){
		if(str.empty()==1){
			return ;
		}
		else{
			str.pop();
		}
	}
}
void push(unsigned long long int x){
	str.push(x);
}
void pop(){
	if(str.empty()==1){
		cout<<"Empty"<<endl;//printf("Empty\n");
	}
     else{
     	str.pop();
	}
}
void query(){
	if(str.empty()==1){
		cout<<"Anguei!"<<endl;//printf("Anguei!\n");
	}
     else{
     	cout<<str.top()<<endl;//printf("%llu\n",str.top());
	}
}
void size(){
    cout<<str.size()<<endl;//printf("%llu\n",str.size());
}
int main(){
	cin>>t;//scanf("%llu",&t);
	while(t--){
	     cin>>n;//scanf("%llu",&n);
		for(int i=1;i<=n;i++){
			cin>>s;
			if(s==a){
			    cin>>x;//scanf("%llu",&x);
			    push(x);
			}
			else if(s==b){
				pop();
			}
			else if(s==c){
				query();
			}
			else if(s==d){
				size();
			}
		}
		clean();
	}
	return 0;
}
原文