-
Notifications
You must be signed in to change notification settings - Fork 1
/
10828.c
69 lines (55 loc) · 1.31 KB
/
10828.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define STACK_LEN 10001
typedef int Data;
typedef struct _arrayStack {
Data stackArr[STACK_LEN];
int topIndex;
} ArrayStack;
typedef ArrayStack Stack;
void StackInit(Stack *pstack) {
pstack->topIndex = -1;
}
int SIsEmpty(Stack *pstack) {
return pstack->topIndex == -1 ? 1 : 0;
}
void SPush(Stack *pstack, Data data) {
pstack->topIndex += 1;
pstack->stackArr[pstack->topIndex] = data;
}
Data SPop(Stack *pstack) {
if (SIsEmpty(pstack))
return -1;
int rIdx = pstack->topIndex;
pstack->topIndex -= 1;
return pstack->stackArr[rIdx];
}
Data SPeek(Stack *pstack) {
if (SIsEmpty(pstack))
return -1;
return pstack->stackArr[pstack->topIndex];
}
int main(void) {
int i, j, n, num;
char command[6];
Stack stack;
StackInit(&stack);
scanf("%d", &n);
for (i=0; i<n; i++) {
scanf("%s", command);
if (!strcmp(command, "push")) {
scanf("%d", &num);
SPush(&stack, num);
} else if (!strcmp(command, "pop"))
printf("%d\n", SPop(&stack));
else if (!strcmp(command, "size"))
printf("%d\n", stack.topIndex + 1);
else if (!strcmp(command, "empty"))
printf("%d\n", SIsEmpty(&stack));
else if (!strcmp(command, "top"))
printf("%d\n", SPeek(&stack));
}
return 0;
}