Explorar el Código

first commit

master
huangyulong hace 4 años
commit
0e9ceb8cf1
Se han modificado 13 ficheros con 586 adiciones y 0 borrados
  1. 0
    0
      README.md
  2. BIN
      btree
  3. 125
    0
      btree.c
  4. 14
    0
      btree.h
  5. BIN
      btree.o
  6. 101
    0
      linkqueue.c
  7. 31
    0
      linkqueue.h
  8. BIN
      linkqueue.o
  9. 7
    0
      makefile
  10. 296
    0
      se/se.vpj
  11. 6
    0
      se/se.vpw
  12. 6
    0
      se/se.vpwhist
  13. BIN
      se/se.vtg

+ 0
- 0
README.md Ver fichero


BIN
btree Ver fichero


+ 125
- 0
btree.c Ver fichero

@@ -0,0 +1,125 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+#include "btree.h"
4
+#include "linkqueue.h"
5
+
6
+btree_t pkt_creat_btree(void)
7
+{
8
+    btree_t n;
9
+    data_type data;
10
+    scanf("%lc", &data);
11
+    if('#' == data)
12
+    {
13
+        return NULL; /* 开始递归调用返回,返回到最后一层 pkt_prev_order() 调用处,子树指针被赋值为NULL */
14
+    }
15
+    else
16
+    {
17
+        if((n = (btree_t)malloc(sizeof(struct btree))) == NULL)
18
+        {
19
+            printf("malloc failed!\n");
20
+            return NULL;
21
+        }
22
+        n->data = data;
23
+        n->lchild = pkt_creat_btree();  /* 开始递归调用 */
24
+        n->rchild = pkt_creat_btree();
25
+    }
26
+    return n;
27
+}
28
+
29
+/* 先序遍历 */
30
+void pkt_prev_order(btree_t t)
31
+{
32
+    if(t != NULL)
33
+    {
34
+        printf("%lc\n", t->data);  /* 访问节点 */
35
+        pkt_prev_order(t->lchild); /* 递归遍历左子树 */
36
+        pkt_prev_order(t->rchild); /* 递归遍历右子树 */
37
+    }
38
+}
39
+
40
+/* 中序遍历 */
41
+void pkt_mid_order(btree_t t)
42
+{
43
+    if(t != NULL)
44
+    {
45
+        pkt_mid_order(t->lchild); /* 递归遍历左子树 */
46
+        printf("%lc\n", t->data); /* 访问节点 */
47
+        pkt_mid_order(t->rchild); /* 递归遍历右子树 */
48
+    }
49
+}
50
+
51
+/* 后序遍历 */
52
+void pkt_after_order(btree_t t)
53
+{
54
+    if(t != NULL)
55
+    {
56
+        pkt_after_order(t->lchild); /* 递归遍历左子树 */
57
+        pkt_after_order(t->rchild); /* 递归遍历右子树 */
58
+        printf("%lc\n", t->data); /* 访问节点 */
59
+    }
60
+}
61
+
62
+/* 按层遍历 */
63
+void pkt_level_order(btree_t t)
64
+{
65
+    linkqueue_t q;
66
+    struct message m;
67
+    q = pkt_creat_linkqueue();
68
+    int ret;
69
+
70
+    while (t != NULL)
71
+    {
72
+        printf("%lc\n", t->data); /* 访问节点 */
73
+       if(t->lchild != NULL)
74
+       {
75
+           m.data = t->lchild;
76
+           pkt_put_message_linkqueue(q, &m); /* 左子树指针入队 */
77
+       }
78
+       if(t->rchild != NULL)
79
+       {
80
+           m.data = t->rchild;
81
+           pkt_put_message_linkqueue(q, &m); /* 右子树指针入队 */
82
+       }
83
+
84
+       if(!(ret = pkt_empty_linkqueue(q)))  /* 队列非空出队,否则结束循环 */
85
+       {
86
+           pkt_take_message_linkqueue(q, &m);
87
+           t = m.data;
88
+       }
89
+       else
90
+       {
91
+           break;
92
+       }
93
+    }
94
+}
95
+
96
+void pkt_travel_btree(btree_t t, const char *str, void(*order)(btree_t t))
97
+{
98
+    printf("%s\n", str);
99
+    order(t);
100
+}
101
+
102
+int main(int argc, char **argv)
103
+{
104
+    btree_t bt;
105
+    bt = pkt_creat_btree();
106
+#if 0
107
+    printf("prev order:\n");
108
+    pkt_prev_order(bt);
109
+
110
+    printf("mid order:\n");
111
+    pkt_mid_order(bt);
112
+
113
+    printf("after order:\n");
114
+    pkt_after_order(bt);
115
+#else
116
+    pkt_travel_btree(bt, "prev order", pkt_prev_order);
117
+    pkt_travel_btree(bt, "mid order", pkt_mid_order);
118
+    pkt_travel_btree(bt, "after order", pkt_after_order);
119
+    pkt_travel_btree(bt, "level order", pkt_level_order);
120
+#endif
121
+    return 0;
122
+}
123
+
124
+
125
+

+ 14
- 0
btree.h Ver fichero

@@ -0,0 +1,14 @@
1
+#ifndef _BTREE_H_
2
+#define _BTREE_H_
3
+
4
+typedef int data_type;
5
+
6
+typedef struct btree *btree_t;
7
+struct btree
8
+{
9
+    data_type data;
10
+    btree_t lchild;
11
+    btree_t rchild;
12
+};
13
+
14
+#endif

BIN
btree.o Ver fichero


+ 101
- 0
linkqueue.c Ver fichero

@@ -0,0 +1,101 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+#include "linkqueue.h"
4
+
5
+linkqueue_t pkt_creat_linkqueue(void)
6
+{
7
+    linkqueue_t q;
8
+    if ((q = (linkqueue_t)malloc(sizeof(struct linkqueue))) == NULL)  /* malloc queue */
9
+    {
10
+        printf("malloc filed!\n");
11
+        return NULL;
12
+    }
13
+
14
+    queue_node_t h;
15
+    if ((h = (queue_node_t)malloc(sizeof(struct queue_node))) == NULL)  /* malloc head node */
16
+    {
17
+        printf("malloc filed!\n");
18
+        return NULL;
19
+    }
20
+    h->next = NULL;
21
+    q->front = q->rear = h;
22
+    return q;
23
+}
24
+
25
+int pkt_empty_linkqueue(linkqueue_t q)
26
+{
27
+    return q->front->next == NULL;
28
+}
29
+
30
+int pkt_put_message_linkqueue(linkqueue_t q, message_t m)
31
+{
32
+    /* linkqueue not have sapce limit */
33
+    queue_node_t n;
34
+    if ((n = (queue_node_t)malloc(sizeof(struct queue_node))) == NULL)
35
+    {
36
+        printf("malloc filed!\n");
37
+        return -1;
38
+    }
39
+    
40
+    n->msg = *m;
41
+    n->next = q->rear->next;
42
+    q->rear->next = n;
43
+    q->rear = n;  /* move q->rear to new node, is tail */
44
+
45
+//  printf("put queue node = %p data = %p\n", q->rear->next, n->msg.data);
46
+//  ptk_show_linkqueue(q);
47
+
48
+    return 0;
49
+}
50
+
51
+int pkt_take_message_linkqueue(linkqueue_t q, message_t m)
52
+{
53
+    if (pkt_empty_linkqueue(q))
54
+    {
55
+        printf("linkqueue is empty!\n");
56
+        return -1;
57
+    }
58
+//  printf("take queue node = %p\n", q->front->next);
59
+    *m = q->front->next->msg;
60
+    queue_node_t del = q->front->next;
61
+    q->front->next = del->next;  /* q->front is alwasy in head, did not move */
62
+    free(del);
63
+    return 0;
64
+}
65
+
66
+void ptk_show_linkqueue(linkqueue_t q)
67
+{
68
+    queue_node_t m;
69
+
70
+    for (m = q->front->next; m != NULL; m = m->next)
71
+    {
72
+        printf("linkqueue = %p\n", m->msg.data);
73
+    }
74
+}
75
+#if 0
76
+int main(int argc, char *argv[])
77
+{
78
+    linkqueue_t q;
79
+    struct message m;
80
+
81
+    q = pkt_creat_linkqueue();
82
+
83
+    for (int i = 1; i <= 10; i++)
84
+    {
85
+        m.data = i;
86
+        printf("put = %d\n", m.data);
87
+        pkt_put_message_linkqueue(q, &m);
88
+    }
89
+
90
+    ptk_show_linkqueue(q);
91
+
92
+    while (!pkt_empty_linkqueue(q))
93
+    {
94
+        pkt_take_message_linkqueue(q, &m);
95
+        printf("take = %d\n", m.data);
96
+    }
97
+
98
+    pkt_take_message_linkqueue(q, &m);
99
+    return 0;
100
+}
101
+#endif

+ 31
- 0
linkqueue.h Ver fichero

@@ -0,0 +1,31 @@
1
+#ifndef _LINKQUEUE_H_
2
+#define _LINKQUEUE_H_
3
+
4
+#include "btree.h"
5
+struct message
6
+{
7
+    btree_t data;
8
+};
9
+typedef struct message *message_t; /* data struct */
10
+
11
+typedef struct queue_node *queue_node_t;  /* queue node struct */
12
+struct queue_node
13
+{
14
+    struct message msg;
15
+    queue_node_t next;
16
+};
17
+
18
+struct linkqueue
19
+{
20
+    queue_node_t front;
21
+    queue_node_t rear;
22
+};
23
+typedef struct linkqueue *linkqueue_t; /* queue struct include ponit front and ponit rear */
24
+
25
+linkqueue_t pkt_creat_linkqueue(void);
26
+int pkt_empty_linkqueue(linkqueue_t q);
27
+int pkt_put_message_linkqueue(linkqueue_t q, message_t m);
28
+int pkt_take_message_linkqueue(linkqueue_t q, message_t m);
29
+void ptk_show_linkqueue(linkqueue_t q);
30
+
31
+#endif

BIN
linkqueue.o Ver fichero


+ 7
- 0
makefile Ver fichero

@@ -0,0 +1,7 @@
1
+CFLAGS := -Wall
2
+
3
+btree: btree.o linkqueue.o
4
+
5
+.PHONY clean:
6
+clean:
7
+	rm btree btree.o linkqueue.o

+ 296
- 0
se/se.vpj Ver fichero

@@ -0,0 +1,296 @@
1
+<!DOCTYPE Project SYSTEM "http://www.slickedit.com/dtd/vse/10.0/vpj.dtd">
2
+<Project
3
+    Version="10.0"
4
+    VendorName="SlickEdit"
5
+    TemplateName="GNU C/C++"
6
+    WorkingDir="."
7
+    BuildSystem="vsbuild">
8
+    <Config
9
+        Name="Debug"
10
+        Type="gnuc"
11
+        DebugCallbackName="gdb"
12
+        Version="1"
13
+        OutputFile="%bdse.exe"
14
+        CompilerConfigName="Latest Version">
15
+        <Menu>
16
+            <Target
17
+                Name="Compile"
18
+                MenuCaption="&amp;Compile"
19
+                Dialog="_gnuc_options_form Compile"
20
+                CaptureOutputWith="ProcessBuffer"
21
+                Deletable="0"
22
+                OutputExts="*.o"
23
+                SaveOption="SaveCurrent"
24
+                RunFromDir="%rw">
25
+                <Exec CmdLine='gcc -c %xup  %defd -g -o "%bd%n%oe" %i "%f"'/>
26
+            </Target>
27
+            <Target
28
+                Name="Link"
29
+                MenuCaption="&amp;Link"
30
+                ShowOnMenu="Never"
31
+                Dialog="_gnuc_options_form Link"
32
+                CaptureOutputWith="ProcessBuffer"
33
+                Deletable="0"
34
+                SaveOption="SaveCurrent"
35
+                RunFromDir="%rw">
36
+                <Exec CmdLine='gcc %xup -g -o "%o" %f %libs'/>
37
+            </Target>
38
+            <Target
39
+                Name="Build"
40
+                MenuCaption="&amp;Build"
41
+                Dialog="_gnuc_options_form Compile"
42
+                CaptureOutputWith="ProcessBuffer"
43
+                Deletable="0"
44
+                SaveOption="SaveWorkspaceFiles"
45
+                RunFromDir="%rw">
46
+                <Exec CmdLine='"%(VSLICKBIN1)vsbuild" "%w" "%r" -t build'/>
47
+            </Target>
48
+            <Target
49
+                Name="Rebuild"
50
+                MenuCaption="&amp;Rebuild"
51
+                Dialog="_gnuc_options_form Compile"
52
+                CaptureOutputWith="ProcessBuffer"
53
+                Deletable="0"
54
+                SaveOption="SaveWorkspaceFiles"
55
+                RunFromDir="%rw">
56
+                <Exec CmdLine='"%(VSLICKBIN1)vsbuild" "%w" "%r" -t rebuild'/>
57
+            </Target>
58
+            <Target
59
+                Name="Debug"
60
+                MenuCaption="&amp;Debug"
61
+                Dialog="_gnuc_options_form Run/Debug"
62
+                BuildFirst="1"
63
+                CaptureOutputWith="ProcessBuffer"
64
+                Deletable="0"
65
+                SaveOption="SaveNone"
66
+                RunFromDir="%rw">
67
+                <Exec CmdLine='vsdebugio -prog "%o"'/>
68
+            </Target>
69
+            <Target
70
+                Name="Execute"
71
+                MenuCaption="E&amp;xecute"
72
+                Dialog="_gnuc_options_form Run/Debug"
73
+                BuildFirst="1"
74
+                CaptureOutputWith="ProcessBuffer"
75
+                Deletable="0"
76
+                SaveOption="SaveWorkspaceFiles"
77
+                RunFromDir="%rw">
78
+                <Exec CmdLine='"%o"'/>
79
+            </Target>
80
+            <Target
81
+                Name="dash"
82
+                MenuCaption="-"
83
+                Deletable="0">
84
+                <Exec/>
85
+            </Target>
86
+            <Target
87
+                Name="GNU C Options"
88
+                MenuCaption="GNU C &amp;Options..."
89
+                ShowOnMenu="HideIfNoCmdLine"
90
+                Deletable="0"
91
+                SaveOption="SaveNone">
92
+                <Exec
93
+                    CmdLine="gnucoptions"
94
+                    Type="Slick-C"/>
95
+            </Target>
96
+        </Menu>
97
+        <Rules Name="Compile">
98
+            <Rule
99
+                InputExts="*.ada"
100
+                OutputExts="*.o"
101
+                LinkObject="1">
102
+                <Exec CmdLine='gnat -g -c -o "%bd%n.o" "%f"'/>
103
+            </Rule>
104
+            <Rule
105
+                InputExts="*.adb"
106
+                OutputExts="*.o"
107
+                LinkObject="1">
108
+                <Exec CmdLine='gnat -g -c -o "%bd%n.o" "%f"'/>
109
+            </Rule>
110
+            <Rule
111
+                InputExts="*.f"
112
+                OutputExts="*.o"
113
+                LinkObject="1">
114
+                <Exec CmdLine='gfortran -c -g -o "%bd%n.o" "%f"'/>
115
+            </Rule>
116
+            <Rule
117
+                InputExts="*.f90"
118
+                OutputExts="*.o"
119
+                LinkObject="1">
120
+                <Exec CmdLine='gfortran -c -g -o "%bd%n.o" "%f"'/>
121
+            </Rule>
122
+            <Rule
123
+                InputExts="*.d"
124
+                OutputExts="*.o"
125
+                LinkObject="1">
126
+                <Exec CmdLine='gdc -c -g -o "%bd%n.o" "%f"'/>
127
+            </Rule>
128
+        </Rules>
129
+        <List Name="GNUC Options">
130
+            <Item
131
+                Name="LinkerOutputType"
132
+                Value="Executable"/>
133
+        </List>
134
+    </Config>
135
+    <Config
136
+        Name="Release"
137
+        Type="gnuc"
138
+        DebugCallbackName="gdb"
139
+        Version="1"
140
+        OutputFile="%bdse.exe"
141
+        CompilerConfigName="Latest Version">
142
+        <Menu>
143
+            <Target
144
+                Name="Compile"
145
+                MenuCaption="&amp;Compile"
146
+                Dialog="_gnuc_options_form Compile"
147
+                CaptureOutputWith="ProcessBuffer"
148
+                Deletable="0"
149
+                OutputExts="*.o"
150
+                SaveOption="SaveCurrent"
151
+                RunFromDir="%rw">
152
+                <Exec CmdLine='gcc -c %xup %defd -o "%bd%n%oe" %i "%f"'/>
153
+            </Target>
154
+            <Target
155
+                Name="Link"
156
+                MenuCaption="&amp;Link"
157
+                ShowOnMenu="Never"
158
+                Dialog="_gnuc_options_form Link"
159
+                CaptureOutputWith="ProcessBuffer"
160
+                Deletable="0"
161
+                SaveOption="SaveCurrent"
162
+                RunFromDir="%rw">
163
+                <Exec CmdLine='gcc %xup -o "%o" %f %libs'/>
164
+            </Target>
165
+            <Target
166
+                Name="Build"
167
+                MenuCaption="&amp;Build"
168
+                Dialog="_gnuc_options_form Compile"
169
+                CaptureOutputWith="ProcessBuffer"
170
+                Deletable="0"
171
+                SaveOption="SaveWorkspaceFiles"
172
+                RunFromDir="%rw">
173
+                <Exec CmdLine='"%(VSLICKBIN1)vsbuild" "%w" "%r" -t build'/>
174
+            </Target>
175
+            <Target
176
+                Name="Rebuild"
177
+                MenuCaption="&amp;Rebuild"
178
+                Dialog="_gnuc_options_form Compile"
179
+                CaptureOutputWith="ProcessBuffer"
180
+                Deletable="0"
181
+                SaveOption="SaveWorkspaceFiles"
182
+                RunFromDir="%rw">
183
+                <Exec CmdLine='"%(VSLICKBIN1)vsbuild" "%w" "%r" -t rebuild'/>
184
+            </Target>
185
+            <Target
186
+                Name="Debug"
187
+                MenuCaption="&amp;Debug"
188
+                Dialog="_gnuc_options_form Run/Debug"
189
+                BuildFirst="1"
190
+                CaptureOutputWith="ProcessBuffer"
191
+                Deletable="0"
192
+                SaveOption="SaveNone"
193
+                RunFromDir="%rw">
194
+                <Exec CmdLine='vsdebugio -prog "%o"'/>
195
+            </Target>
196
+            <Target
197
+                Name="Execute"
198
+                MenuCaption="E&amp;xecute"
199
+                Dialog="_gnuc_options_form Run/Debug"
200
+                BuildFirst="1"
201
+                CaptureOutputWith="ProcessBuffer"
202
+                Deletable="0"
203
+                SaveOption="SaveWorkspaceFiles"
204
+                RunFromDir="%rw">
205
+                <Exec CmdLine='"%o"'/>
206
+            </Target>
207
+            <Target
208
+                Name="dash"
209
+                MenuCaption="-"
210
+                Deletable="0">
211
+                <Exec/>
212
+            </Target>
213
+            <Target
214
+                Name="GNU C Options"
215
+                MenuCaption="GNU C &amp;Options..."
216
+                ShowOnMenu="HideIfNoCmdLine"
217
+                Deletable="0"
218
+                SaveOption="SaveNone">
219
+                <Exec
220
+                    CmdLine="gnucoptions"
221
+                    Type="Slick-C"/>
222
+            </Target>
223
+        </Menu>
224
+        <Rules Name="Compile">
225
+            <Rule
226
+                InputExts="*.ada"
227
+                OutputExts="*.o"
228
+                LinkObject="1">
229
+                <Exec CmdLine='gnat -O -c -o "%bd%n.o" "%f"'/>
230
+            </Rule>
231
+            <Rule
232
+                InputExts="*.adb"
233
+                OutputExts="*.o"
234
+                LinkObject="1">
235
+                <Exec CmdLine='gnat -O -c -o "%bd%n.o" "%f"'/>
236
+            </Rule>
237
+            <Rule
238
+                InputExts="*.f"
239
+                OutputExts="*.o"
240
+                LinkObject="1">
241
+                <Exec CmdLine='gfortran -O -g -o "%bd%n.o" "%f"'/>
242
+            </Rule>
243
+            <Rule
244
+                InputExts="*.f90"
245
+                OutputExts="*.o"
246
+                LinkObject="1">
247
+                <Exec CmdLine='gfortran -O -g -o "%bd%n.o" "%f"'/>
248
+            </Rule>
249
+            <Rule
250
+                InputExts="*.d"
251
+                OutputExts="*.o"
252
+                LinkObject="1">
253
+                <Exec CmdLine='gdc -c -g -o "%bd%n.o" "%f"'/>
254
+            </Rule>
255
+        </Rules>
256
+        <List Name="GNUC Options">
257
+            <Item
258
+                Name="LinkerOutputType"
259
+                Value="Executable"/>
260
+        </List>
261
+    </Config>
262
+    <Files>
263
+        <Folder
264
+            Name="Source Files"
265
+            Filters="*.c;*.C;*.cc;*.cpp;*.cp;*.cxx;*.c++;*.prg;*.pas;*.dpr;*.asm;*.s;*.bas;*.java;*.cs;*.sc;*.scala;*.e;*.cob;*.html;*.rc;*.tcl;*.py;*.pl;*.d;*.m;*.mm;*.go;*.groovy;*.gsh"
266
+            GUID="{3236A226-CEA1-4DD0-B9B5-CD3DDB35C564}">
267
+            <F N="../btree.c"/>
268
+            <F N="../linkqueue.c"/>
269
+        </Folder>
270
+        <Folder
271
+            Name="Header Files"
272
+            Filters="*.h;*.H;*.hh;*.hpp;*.hxx;*.h++;*.inc;*.sh;*.cpy;*.if"
273
+            GUID="{BE7CCD9A-76F8-4460-810D-ADBF87730D8E}">
274
+            <F N="../btree.h"/>
275
+            <F N="../linkqueue.h"/>
276
+        </Folder>
277
+        <Folder
278
+            Name="Resource Files"
279
+            Filters="*.ico;*.cur;*.dlg"
280
+            GUID="{C8B39CBE-97C4-4477-89B1-99730A70A7A5}"/>
281
+        <Folder
282
+            Name="Bitmaps"
283
+            Filters="*.bmp"
284
+            GUID="{D3D6D498-5930-4514-A47B-BDF0DE21883D}"/>
285
+        <Folder
286
+            Name="Other Files"
287
+            Filters=""
288
+            GUID="{955B53DC-0E30-4357-92CF-7D12D9F4A6AA}">
289
+            <F
290
+                N="../makefile"
291
+                Type="Makefile"/>
292
+        </Folder>
293
+    </Files>
294
+    <List Name="RTE">
295
+    </List>
296
+</Project>

+ 6
- 0
se/se.vpw Ver fichero

@@ -0,0 +1,6 @@
1
+<!DOCTYPE Workspace SYSTEM "http://www.slickedit.com/dtd/vse/10.0/vpw.dtd">
2
+<Workspace Version="10.0" VendorName="SlickEdit">
3
+    <Projects>
4
+        <Project File="se.vpj"/>
5
+    </Projects>
6
+</Workspace>

+ 6
- 0
se/se.vpwhist Ver fichero

@@ -0,0 +1,6 @@
1
+[Global]
2
+CurrentProject=se.vpj
3
+[ProjectDates]
4
+se.vpj=20210626042643899
5
+[ActiveConfig]
6
+se.vpj=Debug

BIN
se/se.vtg Ver fichero


Loading…
Cancelar
Guardar