#include <iostream>
#include <string>
using namespace std;
class stack{
private:
//使用int时,无法存储大的数据,如25888888888
long long s[100000];
int top_index=-1;
public:
void push(long long x){
s[++top_index]=x;
}
void pop(){
if(top_index>-1){
cout<<s[top_index--]<<endl;
}
else
cout<<"error"<<endl;
}
void top(){
if(top_index>-1){
cout<<s[top_index]<<endl;
}
else
cout<<"error"<<endl;
}
};
int main() {
stack s;
int n;
cin>>n;
long long x;
for(int i=0;i<n;i++){
string op;
cin>>op;
//空格符可以作为字符串之间或者数字之间的分隔符,所以输入push 1时op=push x=1
if(op=="push"){
cin>>x;
s.push(x);
}
if(op=="pop"){
s.pop();
}
if(op=="top"){
s.top();
}
}
}
牛客网算法入门(AB1模板 栈)c++
最新推荐文章于 2024-07-25 11:27:31 发布