[CodeUp] #3102-STL stack(스택)

chrmqgozj·2022년 1월 22일
0

CodeUp

목록 보기
18/48
#include <iostream>
using namespace std;

int stack[200];
int top=-1;

void push(int x){
    stack[++top] = x;
}
int f_top(){
    if(top!=-1){
        return stack[top];
    }
    else{
        return -1;
    }
    
}
void pop(){
    if(top!=-1){
        top--;
    }
}
bool empty(){
    if(top==-1){
        return true;
    }
    else{
        return false;
    }
}

int main(){
    int n;
    cin >> n;
    string q,s;
    char t;

    for(int i=0;i<n;i++){
        cin >> q;
        s = q.substr(0,4);
        if(s=="push"){
            int x;
            cin >> x;
            cin >> t;
            push(x);
        }
        else if(s=="top("){
            cout << f_top() << "\n";
        }
        else if(s=="pop("){
            pop();
        }
        else if(s=="size"){
            cout << top+1 << "\n";
        }
        else{
            if(empty()){
                cout << "true" << "\n";
            }
            else{
                cout << "false" << "\n";
            }
            
        }
    }
}

0개의 댓글

관련 채용 정보