초기 네이버 기록/알고리즘(C)

백준 (C언어)백준 10828번 스택 스택 구현문제

뜨거운 개발자 2023. 1. 4. 00:11

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max_size 1000000
int stack[max_size];
int top=-1;

void push(int x)
{
	//if (top == max_size - 1)
	//	printf("stackoverflow\n");
	//else
	//{
		top++;
		stack[top] = x;
		
	//}
}

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

int main()
{
	int input;
	int len;
	int x;
	scanf("%d", &input);
	char order[10];
	for (int i = 0; i < input; i++)
	{
		scanf("%s", order);
		if (!strcmp(order, "push"))
		{
			scanf("%d", &x);
			push(x);
		}
		else if (!strcmp(order, "top"))
		{
			if (top == -1)
				printf("-1\n");
			else
				printf("%d\n", stack[top]);
		}
		else if (!strcmp(order, "size"))
		{
			printf("%d\n", top + 1);
		}
		else if (!strcmp(order, "pop"))
		{
			pop();
		}
		else if (!strcmp(order, "empty"))
		{
			if (top == -1)
			{
				printf("1\n");
			}
			else
				printf("0\n");
		}
	}
}

생각보다 간단한 문제이다. 스택에 대한 개념을 스스로 문제를 풀면서 정립하게 해준 문제이다.

이 글은 코딩 꼬꼬마 시절에 푼 문제를 보관한 글로 네이버에 저장해둔 글을 옮긴 글입니다.
혹시나 참고하시는 부분에 이상한 부분이나 질문이 생긴다면 남겨주시면 친절히 답변 드리겠습니다.

728x90