10820: 스택

네르기·2021년 8월 2일
0

알고리즘

목록 보기
19/76

https://www.acmicpc.net/short/status/10828/0/2

스택 구현의 정석

https://www.google.com/amp/s/www.geeksforgeeks.org/stack-data-structure-introduction-program/amp/

한편 백준에서는...

#include <stdio.h>

int A[10000];

int main()
{
	int n,*p=A;
	char s[6];
	for(scanf("%d",&n);n--;)
	{
		scanf("%s",s);
		if(s[1]=='u') scanf("%d",p++);
		if(s[0]=='t') printf("%d\n",p==A ? -1 : *(p-1));
		if(s[0]=='s') printf("%d\n",p-A);
		if(s[0]=='e') printf("%d\n",p==A);
		if(s[0]=='p' && s[1]=='o') printf("%d\n",p==A ? -1 : *--p);
	}
    return 0;
}

??? : 그거 그렇게 하는거 아닌데 ㅋㅋ
포인터랑 배열 하나씩만 있으면 충분.

한가지 더

push 4 처럼 두개의 인자를 받는 경우,
scanf는 스페이스바 또는 개행 문자가 들어오면 읽는 것을 중단함. 근데 이렇게 쓰면 stdin 버퍼에 문자가 들어간 상태라 바로 읽어들임.

scanf("%s", cmd);
scanf("%d", &a);
// push 4 -> allocates "push" to cmd, 4 to a.
profile
프로그래머와 애니메이터가 되고파

0개의 댓글