Bläddra i källkod

add linkstack

master
huangyulong 4 år sedan
förälder
incheckning
74770753b0
4 ändrade filer med 133 tillägg och 0 borttagningar
  1. Binär
      stack/linkstack
  2. 112
    0
      stack/linkstack.c
  3. 21
    0
      stack/linkstack.h
  4. Binär
      stack/linkstack.o

Binär
stack/linkstack Visa fil


+ 112
- 0
stack/linkstack.c Visa fil

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
+

+ 21
- 0
stack/linkstack.h Visa fil

1
+#ifndef _LINKSTACK_H_
2
+#define _LINKSTACK_H_
3
+
4
+#define PKT_ASSERT(x) {if((x) == NULL){printf("assert failed!\n"); while(1);}}
5
+
6
+
7
+struct stack_frame
8
+{
9
+    int data;
10
+};
11
+typedef struct stack_frame *stack_frame_t;
12
+
13
+typedef struct linkstack *linkstack_t;
14
+struct linkstack
15
+{
16
+    struct stack_frame frame;
17
+    linkstack_t next;
18
+};
19
+
20
+
21
+#endif

Binär
stack/linkstack.o Visa fil


Laddar…
Avbryt
Spara