Skip to content

Instantly share code, notes, and snippets.

@HyeonWooKim
Last active February 26, 2016 18:19
Show Gist options
  • Save HyeonWooKim/f4f5689cc45b8123953d to your computer and use it in GitHub Desktop.
Save HyeonWooKim/f4f5689cc45b8123953d to your computer and use it in GitHub Desktop.
배열을 이용한 스택 구현(BOJ 10828)
#include<iostream>
#include<string>
using namespace std;
int stack[10000],top;
void push(int n){
stack[top++]=n;
}
void pop(){
if(top==0)cout<<"-1\n";
else{
top--;
cout<<stack[top]<<"\n";
}
}
void Top(){
if(top==0)cout<<"-1\n";
else{
cout<<stack[top-1]<<"\n";
}
}
int main(){
string command;
int n,k;
cin>>n;
for(int i=0;i<n;i++){
cin>>command;
if(command=="push"){
cin>>k;
push(k);
}
else if(command=="pop"){
pop();
}
else if(command=="size") {
cout<<top<<"\n";
}
else if(command=="top"){
Top();
}
else if(command=="empty"){
cout<<(top==0)<<"\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment