10828

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    // 명령어 
    const char *push = "push";
    const char *pop = "pop";
    const char *size = "size";
    const char *empty = "empty";
    const char *top = "top";

    // 명령어 횟수 입력
    int command_num = 0;
    scanf("%d", &command_num);

    // 스택 배열 생성
    int *stack = malloc(sizeof(int)*command_num);
    int index = 0;
    for(int i = 0; i < command_num; i++){
        // 명령어 입력
        char command[6] = {0};
        getchar();
        scanf("%s", &command);

        if(strcmp(command, push) == 0){ // push 
            scanf("%d",&stack[index++]);
        }
        else if(strcmp(command, pop) == 0){ // pop
            if(index){
                printf("%d\n", stack[index-1]);
                stack[index--] = 0;
            }
            else{
                printf("-1\n");
            }
        }
        else if(strcmp(command, size) == 0){ // size
            printf("%d\n", index);
        }
        else if(strcmp(command, empty) == 0){ // empty
            if(index){
                printf("0\n");
            }
            else{
                printf("1\n");
            }
        }
        else if(strcmp(command, top) == 0){ // top
            if(index){
                printf("%d\n", stack[index-1]);
            }
            else{
                printf("-1\n");
            }
        }
    }
}

+ Recent posts