10828번 - 스택
#include<stdio.h> #include<string.h> #define LEN10000 typedef struct arrayStack { int stArr[LEN]; int topInx; }stack; void stackinit(stack *pstack) { pstack->topInx = -1; } void push(stack *pstack,int data) { pstack->topInx += 1; pstack->stArr[pstack->topInx] = data; } int pop(stack *pstack) { int ridx; if (pstack->topInx == -1) printf("-1\n"); else if(pstack->topInx != -1){ ridx = pstack->topInx; pstack->topInx -= 1; printf("%d\n", pstack->stArr[ridx]); } } int size(stack *pstack) { int count = 1; if (pstack->topInx == -1) printf("0\n"); else if (pstack->topInx != -1) { for (int i = 0;i < pstack->topInx;i++) count = count++; printf("%d\n", count); } } int empty(stack *pstack) { if (pstack->topInx == -1) printf("1\n"); else printf("0\n"); } int top(stack *pstack){ if (pstack->topInx == -1) printf("-1\n"); else printf("%d\n",pstack->stArr[pstack->topInx]); } int main() { int n,item=0; stack q; stackinit(&q); scanf("%d", &n); for (int i = 0;i < n;i++) { char command[11]; scanf("%s", command); if (strcmp(command, "push") == 0) { scanf("%d", &item); push(&q, item); } else if (strcmp(command, "pop") == 0) { pop(&q); } else if (strcmp(command, "top") == 0) { top(&q); } else if (strcmp(command, "size") == 0) { size(&q); } else if (strcmp(command, "empty") == 0) empty(&q); } }
댓글을 작성하려면 로그인해야 합니다.
ksmabcd135 2년 전