[Algorithm] c++ stack 구현
| 
#include <iostream> 
#include <string.h> 
using namespace std; 
int stack[10001],s_top=-1; 
void push(int X){ 
    stack[++s_top] = X; 
} 
int pop(){ 
    if(s_top==-1) return -1; 
    return stack[s_top--]; 
} 
int size(){ 
    return s_top+1; 
} 
int top(){ 
    if(s_top==-1) return -1; 
    return stack[s_top]; 
} 
int empty(){ 
    if(s_top==-1) return 1; 
    else return 0; 
} 
int main(){ 
    int n,temp; 
    string order; 
    cin>>n; 
    for(int i=0;i<n;i++){ 
        cin>>order; 
        if (order=="push"){ 
            cin>>temp; 
            push(temp); 
        } 
        else if (order=="pop"){ 
            cout<<pop()<<endl; 
        } 
        else if (order=="size"){ 
            cout<<size()<<endl; 
        } 
        else if (order=="empty"){ 
            cout<<empty()<<endl; 
        } 
        else{ 
            cout<<top()<<endl; 
        } 
    } 
} | cs | 

No comments: