C언어 간단한 스택구현 질문입니당
조회수 611회
include
include
void main(){ int stack[1000]; int top; int n; int num = 0; top = 0; scanf_s("%d", &n);
for (int i = 0; i < n;i++) {
char word[10];
gets(word);
if (strcmp(word, "push") == 0) { //push 1 하면 1이 스택에 저장되야 하는데
scanf_s(" %d",&num); // push 부분이 잘 안되는 것 같아요.
stack[++top] = num; //다른 것들은 괜찮은데..
}
else if (strcmp(word, "pop") == 0) {
if (top <= 0)
printf("no pop\n");
else {
printf("%d\n", stack[top - 1]);
top--;
}
}
else if (strcmp(word, "empty") == 0) {
if (top <= 0)
printf("1\n");
else
printf("0\n");
}
else if (strcmp(word, "top") == 0) {
if (top <= 0)
printf("-1\n");
else printf("%d\n", stack[top]);
}
else if (strcmp(word, "size") == 0) {
printf("%d\n", top);
}
}
}
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력