|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+#include <stdio.h>
|
|
|
2
|
+#include <stdlib.h>
|
|
|
3
|
+#include "linkstack.h"
|
|
|
4
|
+
|
|
|
5
|
+linkstack_t pkt_creat_linkstack(void)
|
|
|
6
|
+{
|
|
|
7
|
+ linkstack_t s ;
|
|
|
8
|
+ if ((s = (linkstack_t)malloc(sizeof(struct linkstack))) == NULL)
|
|
|
9
|
+ {
|
|
|
10
|
+ printf("malloc failed!\n");
|
|
|
11
|
+ return NULL;
|
|
|
12
|
+ }
|
|
|
13
|
+
|
|
|
14
|
+ s->next = NULL;
|
|
|
15
|
+ return s;
|
|
|
16
|
+}
|
|
|
17
|
+
|
|
|
18
|
+int pkt_empty_linkstack(linkstack_t s)
|
|
|
19
|
+{
|
|
|
20
|
+ return s->next == NULL;
|
|
|
21
|
+}
|
|
|
22
|
+
|
|
|
23
|
+stack_frame_t pkt_get_linkstack_top(linkstack_t s)
|
|
|
24
|
+{
|
|
|
25
|
+ if (pkt_empty_linkstack(s))
|
|
|
26
|
+ {
|
|
|
27
|
+ printf("linkstack is empty!\n");
|
|
|
28
|
+ return NULL;
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ return &s->next->frame;
|
|
|
32
|
+}
|
|
|
33
|
+
|
|
|
34
|
+int pkt_linkstack_push(linkstack_t s, stack_frame_t frame)
|
|
|
35
|
+{
|
|
|
36
|
+ PKT_ASSERT(s);
|
|
|
37
|
+ PKT_ASSERT(frame);
|
|
|
38
|
+ linkstack_t f;
|
|
|
39
|
+ if ((f = (linkstack_t)malloc(sizeof(struct linkstack))) == NULL)
|
|
|
40
|
+ {
|
|
|
41
|
+ printf("malloc failed!\n");
|
|
|
42
|
+ return -1;
|
|
|
43
|
+ }
|
|
|
44
|
+ f->frame = *frame;
|
|
|
45
|
+ f->next = s->next;
|
|
|
46
|
+ s->next = f;
|
|
|
47
|
+ return 0;
|
|
|
48
|
+}
|
|
|
49
|
+
|
|
|
50
|
+struct stack_frame pkt_linkstack_pop(linkstack_t s)
|
|
|
51
|
+{
|
|
|
52
|
+ struct stack_frame f = {0};
|
|
|
53
|
+
|
|
|
54
|
+ if (pkt_empty_linkstack(s))
|
|
|
55
|
+ {
|
|
|
56
|
+ printf("linkstack is empty!\n");
|
|
|
57
|
+ return f;
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ linkstack_t d = s->next;
|
|
|
61
|
+ s->next = d->next;
|
|
|
62
|
+ f = d->frame;
|
|
|
63
|
+ free(d);
|
|
|
64
|
+ return f;
|
|
|
65
|
+}
|
|
|
66
|
+
|
|
|
67
|
+int pkt_clear_linkstack(linkstack_t s)
|
|
|
68
|
+{
|
|
|
69
|
+ linkstack_t d = s->next;
|
|
|
70
|
+ while (d)
|
|
|
71
|
+ {
|
|
|
72
|
+ s->next = d->next;
|
|
|
73
|
+ free(d);
|
|
|
74
|
+ d = s->next;
|
|
|
75
|
+ }
|
|
|
76
|
+ return 0;
|
|
|
77
|
+}
|
|
|
78
|
+
|
|
|
79
|
+int pkt_free_linkstack(linkstack_t s) /* include free head */
|
|
|
80
|
+{
|
|
|
81
|
+ linkstack_t d = s;
|
|
|
82
|
+
|
|
|
83
|
+ while (d)
|
|
|
84
|
+ {
|
|
|
85
|
+ s = d->next;
|
|
|
86
|
+ free(d);
|
|
|
87
|
+ d = s;
|
|
|
88
|
+ }
|
|
|
89
|
+ return 0;
|
|
|
90
|
+}
|
|
|
91
|
+
|
|
|
92
|
+
|
|
|
93
|
+int main(void)
|
|
|
94
|
+{
|
|
|
95
|
+ linkstack_t s = pkt_creat_linkstack();
|
|
|
96
|
+
|
|
|
97
|
+ struct stack_frame frame;
|
|
|
98
|
+
|
|
|
99
|
+ for (int i = 0; i < 10; i++)
|
|
|
100
|
+ {
|
|
|
101
|
+ frame.data = i;
|
|
|
102
|
+ pkt_linkstack_push(s, &frame);
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
|
105
|
+ while (!pkt_empty_linkstack(s))
|
|
|
106
|
+ {
|
|
|
107
|
+ printf("%d\n", pkt_linkstack_pop(s).data);
|
|
|
108
|
+ }
|
|
|
109
|
+ pkt_free_linkstack(s);
|
|
|
110
|
+ return 0;
|
|
|
111
|
+}
|
|
|
112
|
+
|