- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-stack.c
More file actions
Latest commit
73 lines (54 loc) · 976 Bytes
/
Copy patharray-stack.c
File metadata and controls
73 lines (54 loc) · 976 Bytes
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>
#defineSTACK_SIZE 20
structstack {
intarray[STACK_SIZE];
intsp; /* top point of stack */
};
intstack_init(structstack*s)
{
s->sp=-1;
}
intstack_empty(structstack*s)
{
return (s->sp==-1);
}
intstack_full(structstack*s)
{
return (s->sp==STACK_SIZE-1);
}
intpush(structstack*s, intvalue)
{
if (stack_full(s))
return-1;
s->array[s->sp+1] =value;
s->sp++;
return0;
}
intpop(structstack*s)
{
if (stack_empty(s))
return-1;
returns->array[s->sp--];
}
/*************test sample***************/
structstackstack;
intmain (intargc, char*argv[])
{
structstack*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));
return0;
}