스택관련 질문
조회수 404회
Stop 함수를 실행하면 가장 마지막에 들어간 데이터가 보여져야하는데 나오질 않네요ㅠㅠ 고수님들 무엇인 문제인지 아시나요?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define STACK_LEN 10001
typedef struct _Stack
{
int arr[STACK_LEN];
int topIndex;
} ST;
void SstackInit(ST *pstack)
{
pstack->topIndex = -1;
}
void Spush(ST *pstack, int X)
{
pstack->topIndex += 1;
pstack->arr[pstack->topIndex] = X;
}
int Sempty(ST *pstack)
{
if (pstack->topIndex == -1)
return TRUE;
else
return FALSE;
}
int Spop(ST *pstack)
{
int imsi;
if (Sempty(pstack))
{
printf("error!");
return -1;
}
imsi = pstack->topIndex;
pstack->topIndex -= 1;
return pstack->arr[imsi];
}
int Ssize(ST *pstack)
{
return pstack->topIndex + 1; //-1부터 시작이니깐
}
int Stop(ST *pstack)
{
if (Sempty(pstack))
{
return -1;
}
return pstack->arr[pstack->topIndex];
}
int main()
{
ST stack;
char str[10];
int N = 0, i, num;
scanf("%d", &N);
SstackInit(&stack);
for (i = 0; i < N; i++)
{
scanf("%s", str);
if (!strcmp(str, "push"))
{
scanf("%d", &num);
Spush(&stack, num);
}
else if (!strcmp(str, "pop"))
{
printf("%d\n", Spop(&stack));
}
else if (!strcmp(str, "size"))
{
printf("%d\n", Ssize(&stack));
}
else if (!strcmp(str, "empty"))
{
printf("%d\n",Sempty(&stack));
}
else if (!strcmp(str, "top"))
{
printf("%d\n", Stop(&stack));
}
}
return 0;
}
1 답변
-
코드가 실행이 잘되는군요. 다만 else if (!strcmp(str, "top")) { printf("%d\n", Stop(&stack)); }에서 "top" 가 "stop"으로 바뀌면 딱이겠군요.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력