[백준] 10828번 : 스택 - C

강재원·2022년 11월 2일
0

[코딩테스트] C/C++

목록 보기
167/200



https://www.acmicpc.net/problem/10828

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

struct Stack{
    int stack[10001];
    int si;
}t;

void push(int num) {
	t.stack[t.si] = num;
	t.si++;
}
void pop() {
	if(t.si == 0) printf("-1\n");
	else {
	    int ans = t.stack[t.si - 1];
		t.stack[t.si - 1] = 0;
		t.si--;
		printf("%d\n",ans);
	}
}
void size() {
	printf("%d\n",t.si);
}
void empty() {
	if(t.si == 0) printf("1\n");
	else printf("0\n");
}
void top() {
	if(t.si == 0) printf("-1\n");
	else printf("%d\n",t.stack[t.si - 1]);
}

int main() {
    t.si=0;
    
    int n;
    scanf("%d",&n);
    
    for(int i=0;i<n;i++){
        char st[10];
        scanf("%s",&st);
        if (strcmp(st, "push")==0) {
            int num;
			scanf("%d", &num);
			push(num);
		}
		else if(strcmp(st, "pop")==0) {
			pop();
		}
		else if(strcmp(st, "top")==0) {
			top();
		}
		else if(strcmp(st, "size")==0) {
			size();
		}
		else if(strcmp(st, "empty")==0) {
			empty();
		}
    }
}
profile
개념정리 & 문법 정리 & 알고리즘 공부

0개의 댓글