공부/Baekjoon

Baekjoon(10828) C Language

완이버스 2024. 1. 16. 00:13
#include <stdio.h>
#include <string.h>
#define SIZE 10000

int res[SIZE] = {0};
int N;
int count = 0;

void push(int input){
    res[count] = input;
    count++;
}

void pop(){
    if(count == 0){
        printf("-1\n");
        return;
    }
    printf("%d\n",res[count-1]);
    res[count - 1] = 0;
    count--;
}

void size(){
    printf("%d\n",count);
}

void empty(){
    if(count == 0){
        printf("1\n");
    }
    else{
        printf("0\n");
    }
}

void top(){
    if(count == 0){
        printf("-1\n");
    }
    else{
    printf("%d\n",res[count -1]);
    }
}

int main() {
    scanf("%d", &N);
    char input[10] = {};
    
    for (int i = 0; i < N; i++) {
        scanf("%s", &input);
        //printf("%s\n",input);
        if(strcmp(input,"push") == 0){
            int temp;
            scanf("%d",&temp);
            push(temp);
            continue;
        }
        else if(strcmp(input,"pop") == 0){
            pop();
            continue;
        }
        else if(strcmp(input,"size") == 0){
            size();
            continue;
        }
        else if(strcmp(input,"empty") == 0){
            empty();
            continue;
        }
        else if(strcmp(input,"top") == 0){
            top();
            continue;
        }
    }
    return 0;
}

'공부 > Baekjoon' 카테고리의 다른 글

Baekjoon(9012) 괄호 in C  (0) 2024.01.18
Baekjoon(9093) 단어 뒤짚기 in C  (0) 2024.01.17