-
Notifications
You must be signed in to change notification settings - Fork 0
/
array-stack.c
73 lines (54 loc) · 976 Bytes
/
array-stack.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
70
71
72
73
/*array stack */
//数组堆栈
#include <stdio.h>
#define STACK_SIZE 20
struct stack {
int array[STACK_SIZE];
int sp; /* top point of stack */
};
int stack_init(struct stack *s)
{
s->sp = -1;
}
int stack_empty(struct stack *s)
{
return (s->sp == -1);
}
int stack_full(struct stack *s)
{
return (s->sp == STACK_SIZE - 1);
}
int push(struct stack *s, int value)
{
if (stack_full(s))
return -1;
s->array[s->sp + 1] = value;
s->sp++;
return 0;
}
int pop(struct stack *s)
{
if (stack_empty(s))
return -1;
return s->array[s->sp--];
}
/*************test sample***************/
struct stack stack;
int main (int argc, char *argv[])
{
struct stack *s = &stack;
stack_init(s);
push(s, 1);
printf("push 1!\n");
push(s, 2);
printf("push 2!\n");
push(s, 3);
printf("push 3!\n");
push(s, 4);
printf("push 4!\n");
printf("pop:%d\n", pop(s));
printf("pop:%d\n", pop(s));
printf("pop:%d\n", pop(s));
printf("pop:%d\n", pop(s));
return 0;
}