├── .gitignore
├── .idea
├── .name
├── c_data_structure.iml
├── codeStyles
│ └── Project.xml
├── misc.xml
├── modules.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── big_int
├── big_int.c
└── big_int.h
├── binary_tree
├── binary_tree.c
├── binary_tree.h
├── dll_queue.c
├── dll_queue.h
├── dll_stack.c
├── dll_stack.h
└── elem_type_define.h
├── double_circular_linked_list
├── CMakeLists.txt
├── double_circular_linked_list.c
├── double_circular_linked_list.h
└── test_double_circular_linked_list.c
├── double_linked_list
├── double_linked_list.c
├── double_linked_list.h
└── test_double_linked_list.c
├── double_linked_list_queue
├── double_linked_list_queue.c
├── double_linked_list_queue.h
└── test_double_linked_list_queue.c
├── double_linked_list_stack
├── double_linked_list_stack.c
├── double_linked_list_stack.h
└── test_double_linked_list_stack.c
├── g_list
├── g_list.c
├── g_list.h
├── my_string.c
├── my_string.h
├── my_string_array.c
├── my_string_array.h
└── test_g_list.c
├── linear_list
├── linear_list.c
├── linear_list.h
└── test_linear_list.c
├── linear_list_stack
├── bracket_matching.c
├── evaluate_expression.c
├── labyrinth_analyze.c
├── line_edit.c
├── linear_list_stack.c
├── linear_list_stack.h
├── number_conversion.c
├── test_linear_list_stack.c
└── tower_of_hanoi.c
├── matrix
├── matrix.c
├── matrix.h
└── test_matrix.c
├── polynomial
├── polynomial.c
├── polynomial.h
└── test_polynomial.c
├── single_circular_linked_list
├── single_circular_linked_list.c
├── single_circular_linked_list.h
└── test_single_circular_linked_list.c
├── single_circular_linked_list_queue
├── single_circular_linked_list_queue.c
├── single_circular_linked_list_queue.h
└── test_single_circular_linked_list_queue.c
├── single_linked_list
├── CMakeLists.txt
├── single_linked_list.c
├── single_linked_list.h
└── test_single_linked_list.c
├── single_linked_list_queue
├── single_linked_list_queue.c
├── single_linked_list_queue.h
└── test_single_linked_list_queue.c
└── string
├── string.c
├── string.h
├── string_array.c
├── string_array.h
├── test_string.c
└── test_string_array.c
/.gitignore:
--------------------------------------------------------------------------------
1 | #folder and other
2 | *cmake-build-debug
3 | *uploading.cfg
4 |
5 | # Prerequisites
6 | *.d
7 |
8 | # Object files
9 | *.o
10 | *.ko
11 | *.obj
12 | *.elf
13 |
14 | # Linker output
15 | *.ilk
16 | *.map
17 | *.exp
18 |
19 | # Precompiled Headers
20 | *.gch
21 | *.pch
22 |
23 | # Libraries
24 | *.lib
25 | *.a
26 | *.la
27 | *.lo
28 |
29 | # Shared objects (inc. Windows DLLs)
30 | *.dll
31 | *.so
32 | *.so.*
33 | *.dylib
34 |
35 | # Executables
36 | *.exe
37 | *.out
38 | *.app
39 | *.i*86
40 | *.x86_64
41 | *.hex
42 |
43 | # Debug files
44 | *.dSYM/
45 | *.su
46 | *.idb
47 | *.pdb
48 |
49 | # Kernel Module Compile Results
50 | *.mod*
51 | *.cmd
52 | .tmp_versions/
53 | modules.order
54 | Module.symvers
55 | Mkfile.old
56 | dkms.conf
57 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | double_circular_linked_list
--------------------------------------------------------------------------------
/.idea/c_data_structure.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 | get_priornode
118 |
119 |
120 |
121 |
122 |
123 |
124 |
132 |
133 |
134 |
135 |
136 | true
137 | DEFINITION_ORDER
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 | 1567263504569
210 |
211 |
212 | 1567263504569
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 | 1.8
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # c_data_structure
--------------------------------------------------------------------------------
/big_int/big_int.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __BIGINT_H
3 | #define __BIGINT_H
4 |
5 | typedef struct BigInt{
6 | unsigned int elem_num; //elem的个数
7 | char *elem; //指向大数存放位置,序号0为低位,每个元素保存1位十进制长度数据。
8 | struct BigInt *higher; //higher指向更高位,higher = NULL为大数的head
9 | struct BigInt *lower; //lower指向更低位,lower = NULL为大数的tear
10 | char sign; //符号位,1表示正数,-1表示负数,只有head node使用此符号位。
11 | }BigInt;
12 |
13 |
14 |
15 | #endif
--------------------------------------------------------------------------------
/binary_tree/binary_tree.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "BinaryTree.h"
4 | #include "DLLStack.h"
5 | //二叉树
6 |
7 | BinaryTree *InitBinaryTree(){
8 | BinaryTree *T = (BinaryTree *)malloc(sizeof(BinaryTree));
9 | if(!T) return NULL;
10 | BiTNode *n = (BiTNode *)malloc(sizeof(BiTNode));
11 | T->This = n;
12 | return T;
13 | }
14 |
15 | static void clearBiTNode(BiTNode *T,int (*destroyElem)(void **elem)){
16 | if(!T){
17 | clearBiTNode(T->left,destroyElem);
18 | clearBiTNode(T->right,destroyElem);
19 | if()
20 | if(visit(&T) != 0) return;
21 | }
22 | }
23 |
24 | void DestroyBinaryTree(BinaryTree *T){
25 | if(T){
26 | if(T->This){
27 | if(T->This->elem){
28 | free(T->This->elem);
29 | }
30 | free(T->This);
31 | }
32 | free(T);
33 | L = NULL;
34 | }
35 | }
36 |
37 | //递归前序遍历
38 | static void preOrderTraversal(BiTNode *T,int (*visit)(BiTNode **T)){
39 | if(!T){
40 | if(visit(&T) != 0) return;
41 | preOrderTraversal(T->left,visit);
42 | preOrderTraversal(T->right,visit);
43 | }
44 | }
45 |
46 | //递归中序遍历
47 | static void inOrderTraversal(BiTNode *T,int (*visit)(BiTNode **T)){
48 | if(!T){
49 | inOrderTraversal(T->left,visit);
50 | if(visit(&T) != 0) return;
51 | inOrderTraversal(T->right,visit);
52 | }
53 | }
54 |
55 | //递归后序遍历
56 | static void postOrderTraversal(BiTNode *T,int (*visit)(BiTNode **T)){
57 | if(!T){
58 | postOrderTraversal(T->left,visit);
59 | postOrderTraversal(T->right,visit);
60 | if(visit(&T) != 0) return;
61 | }
62 | }
63 |
64 | //非递归前序遍历
65 | static void preOrderTraversal2(BiTNode *T,int (*visit)(BiTNode **T)){
66 | if(!T) return;
67 | DLLStack *BiTreeStack = InitDLLStack();
68 | BiTNode *p = T;
69 | while(p || !BiTreeStack->isEmpty(BiTreeStack)){ //p不空或者栈不空
70 | if(!p){
71 | BiTreeStack->pushElem(BiTreeStack,p); //存入栈中
72 | if(visit(&p) != 0) break;
73 | p = p->left; //遍历左子树
74 | }
75 | else{
76 | BiTreeStack->popElem(BiTreeStack,(void **)&p); //退栈
77 | p = p->right; //遍历右子树
78 | }
79 | }
80 | DestroyDLLStack(BiTreeStack);
81 | }
82 |
83 | //非递归中序遍历
84 | static void inOrderTraversal2(BiTNode *T,int (*visit)(BiTNode **T)){
85 | if(!T) return;
86 | DLLStack *BiTreeStack = InitDLLStack();
87 | BiTNode *p = T;
88 | while(p || !BiTreeStack->isEmpty(BiTreeStack)){ //p不空或者栈不空
89 | if(!p){
90 | BiTreeStack->pushElem(BiTreeStack,p); //存入栈中
91 | p = p->left; //遍历左子树
92 | }
93 | else{
94 | BiTreeStack->popElem(BiTreeStack,(void **)&p); //退栈
95 | if(visit(&p) != 0) break;
96 | p = p->right; //遍历右子树
97 | }
98 | }
99 | DestroyDLLStack(BiTreeStack);
100 | }
101 |
102 | //非递归后序遍历
103 | static void postOrderTraversal2(BiTNode *T,int (*visit)(BiTNode **T)){
104 | if(!T) return;
105 | DLLStack *BiTreeStack = InitDLLStack();
106 | BiTNode *p = T;
107 | BiTNode *pre = NULL;//前一次访问的结点
108 | BiTreeStack->pushElem(BiTreeStack,p); //存入栈中
109 | while(!BiTreeStack->isEmpty(BiTreeStac)){ //栈不空
110 | S->getTopElem(S,&p);
111 | if((p->left == NULL && p->right == NULL) || //如果当前节点没有子节点或
112 | (pre != NULL && (pre == p->left || pre == p->right))) //子节点都已被访问过
113 | {
114 | if(visit(&p) != 0) break;
115 | BiTreeStack->popElem(BiTreeStack,(void **)&p); //退栈
116 | pre = p;
117 | }
118 | else{
119 | if(p->right != NULL){
120 | BiTreeStack->pushElem(BiTreeStack,p->right); //先将右子树存入栈中
121 | }
122 | if(p->left != NULL){
123 | BiTreeStack->pushElem(BiTreeStack,p->left); //再将左子树存入栈中
124 | }
125 | }
126 | }
127 | DestroyDLLStack(BiTreeStack);
128 | }
129 |
130 | //递归遍历
131 | static void depthFirstTraversal(BinaryTree *T,int (*visit)(BiTNode **T),BinaryTreeOrder binTreeOrder){
132 | if(!T) return;
133 | switch(binTreeOrder){
134 | case preOrder:
135 | preOrderTraversal(T->Root,visit);
136 | break;
137 | case inOrder:
138 | inOrderTraversal(T->Root,visit);
139 | break;
140 | case postOrder:
141 | postOrderTraversal(T->Root,visit);
142 | break;
143 | }
144 | }
145 |
146 | //非递归遍历
147 | static void depthFirstTraversal2(BinaryTree *T,int (*visit)(BiTNode **T),BinaryTreeOrder binTreeOrder){
148 | if(!T) return;
149 | switch(binTreeOrder){
150 | case preOrder:
151 | preOrderTraversal2(T->Root,visit);
152 | break;
153 | case inOrder:
154 | inOrderTraversal2(T->Root,visit);
155 | break;
156 | case postOrder:
157 | postOrderTraversal2(T->Root,visit);
158 | break;
159 | }
160 | }
161 |
162 | //广度优先遍历
163 | static void breadthFirstTraversal(BiTNode *T,int (*visit)(BiTNode **T)){
164 | if(!T || !T->Root) return;
165 | DLLQueue *BiTreeQueue = InitDLLQueue();
166 | BiTNode *p = NULL;
167 | BiTreeQueue->enQueue(BiTreeQueue,T->Root);
168 | while(!BiTreeQueue->isEmpty(BiTreeQueue)){ //队列不空
169 | BiTreeQueue->deQueue(BiTreeQueue,(void **)&p);
170 | if(visit(&p) != 0) break;
171 | if(p->left){
172 | BiTreeQueue->enQueue(BiTreeQueue,p->left); //先将左子树入队
173 | }
174 | if(p->right){
175 | BiTreeQueue->enQueue(BiTreeQueue,p->right); //再将右子树入队
176 | }
177 | }
178 | DestroyDLLQueue(BiTreeQueue);
179 | }
180 |
--------------------------------------------------------------------------------
/binary_tree/binary_tree.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __BINARYTREE_H
3 | #define __BINARYTREE_H
4 | /* Includes ------------------------------------------------------------------*/
5 | #include "ElemTypeDefine.h"
6 | /* Exported types ------------------------------------------------------------*/
7 |
8 | typedef enum BinaryTreeOrder{
9 | preOrder, //前序
10 | inOrder, //中序
11 | postOrder //后序
12 | }BinaryTreeOrder;
13 |
14 | typedef struct BiTNode{
15 | void *elem;
16 | struct BiTNode *parent;
17 | struct BiTNode *left;
18 | struct BiTNode *right;
19 | }BiTNode;
20 |
21 | typedef struct BinaryTree{
22 | BiTNode *Root;
23 | int length;
24 | void (*clear)(struct BinaryTree *This);
25 | int (*isEmpty)(struct BinaryTree *This);
26 | }BinaryTree;
27 |
28 | BinaryTree *InitBinaryTree();
29 | void DestroyBinaryTree(BinaryTree *T);
30 |
31 | #endif
32 |
--------------------------------------------------------------------------------
/binary_tree/dll_queue.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "DLLQueue.h"
4 |
5 | static void clear(DLLQueue *This);
6 | static int isEmpty(DLLQueue *This);
7 | static int length(DLLQueue *This);
8 | static void riseTraverse(DLLQueue *This,int (*visit)(void **e));
9 | static void downTraverse(DLLQueue *This,int (*visit)(void **e));
10 | static int getHead(DLLQueue *This, void **e);
11 | static int enQueue(DLLQueue *This, void *e);
12 | static int deQueue(DLLQueue *This, void **e);
13 |
14 | DLLQueue *InitDLLQueue(){
15 | DLLQueue *Q = (DLLQueue *)malloc(sizeof(DLLQueue));
16 | if(!Q) return NULL;
17 | DLLQNode *p = (DLLQNode *)malloc(sizeof(DLLQNode));
18 | if(!p){
19 | free(Q);
20 | return NULL;
21 | }
22 | Q->front = p;
23 | Q->tear = Q->front;
24 | p->prior = NULL;
25 | p->next = NULL;
26 | Q->clear = clear;
27 | Q->isEmpty = isEmpty;
28 | Q->length = length;
29 | Q->riseTraverse = riseTraverse;
30 | Q->downTraverse = downTraverse;
31 | Q->getHead = getHead;
32 | Q->enQueue = enQueue;
33 | Q->deQueue = deQueue;
34 | return Q;
35 | }
36 |
37 | void DestroyDLLQueue(DLLQueue *Q){
38 | if(Q){
39 | if(Q->front){
40 | Q->clear(Q);
41 | free(Q->front);
42 | }
43 | free(Q);
44 | Q = NULL;
45 | }
46 | }
47 |
48 | static void clear(DLLQueue *This){
49 | DLLQNode *p = This->front->next;
50 | DLLQNode *temp = NULL;
51 | while(p){
52 | temp = p;
53 | p = p->next;
54 | free(temp);
55 | }
56 | p = This->front;
57 | p->next = NULL;
58 | This->tear = This->front;
59 | }
60 |
61 | static int isEmpty(DLLQueue *This){
62 | DLLQNode *p = This->front;
63 | if(p->next){
64 | return 0;
65 | }else{
66 | return 1;
67 | }
68 | }
69 |
70 | static int length(DLLQueue *This){
71 | int j = 0;
72 | DLLQNode *p = This->front->next;
73 | while(p){
74 | j++;
75 | p = p->next;
76 | }
77 | return j;
78 | }
79 |
80 | static void riseTraverse(DLLQueue *This,int (*visit)(void **e)){
81 | DLLQNode *p = This->front->next;
82 | while(p){
83 | if(visit(&(p->elem)) != 0) break;
84 | p = p->next;
85 | }
86 | }
87 |
88 | static void downTraverse(DLLQueue *This,int (*visit)(void **e)){
89 | DLLQNode *p = This->tear;
90 | while(p != This->front){
91 | if(visit(&(p->elem)) != 0) break;
92 | p = p->prior;
93 | }
94 | }
95 |
96 |
97 | static int getHead(DLLQueue *This, void **e){
98 | if(isEmpty(This)) return -1;
99 | *e = This->front->next->elem;
100 | return 0;
101 | }
102 |
103 | static int enQueue(DLLQueue *This, void *e){
104 | DLLQNode *p = This->tear;
105 | DLLQNode *temp = (DLLQNode *)malloc(sizeof(DLLQNode));
106 | if(!temp) return -1;
107 | temp->elem = e;
108 | p->next = temp;
109 | temp->prior = p;
110 | This->tear = temp;
111 | temp->next = NULL;
112 | return 0;
113 | }
114 |
115 | static int deQueue(DLLQueue *This, void **e){
116 | if(This->front == This->tear){
117 | *e = NULL;
118 | return -1;
119 | }
120 | DLLQNode *p = This->front->next;
121 | *e = p->elem;
122 | This->front->next = p->next;
123 | if(This->tear == p){
124 | This->tear = This->front;
125 | }else{
126 | p->next->prior = This->front;
127 | }
128 | free(p);
129 | return 0;
130 | }
131 |
132 |
--------------------------------------------------------------------------------
/binary_tree/dll_queue.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _DLLQUEUE_H
3 | #define _DLLQUEUE_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef struct DLLQNode{
7 | void *elem;
8 | struct DLLQNode *prior;
9 | struct DLLQNode *next;
10 | }DLLQNode;
11 |
12 | typedef struct DLLQueue{
13 | DLLQNode *front;
14 | DLLQNode *tear;
15 | void (*clear)(struct DLLQueue *This);
16 | int (*isEmpty)(struct DLLQueue *This);
17 | int (*length)(struct DLLQueue *This);
18 | void (*riseTraverse)(struct DLLQueue *This,int (*visit)(void **e));
19 | void (*downTraverse)(struct DLLQueue *This,int (*visit)(void **e));
20 | int (*getHead)(struct DLLQueue *This, void **e);
21 | int (*enQueue)(struct DLLQueue *This, void *e);
22 | int (*deQueue)(struct DLLQueue *This, void **e);
23 | }DLLQueue;
24 |
25 | /* Exported macro ------------------------------------------------------------*/
26 | DLLQueue *InitDLLQueue();
27 | void DestroyDLLQueue(DLLQueue *Q);
28 |
29 | #endif
--------------------------------------------------------------------------------
/binary_tree/dll_stack.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "DLLStack.h"
4 |
5 | static void clear(DLLStack *This);
6 | static int isEmpty(DLLStack *This);
7 | static int length(DLLStack *This);
8 | static void riseTraverse(DLLStack *This,int (*visit)(void **e));
9 | static void downTraverse(DLLStack *This,int (*visit)(void **e));
10 | static int getTopElem(DLLStack *This, void **e);
11 | static int pushElem(DLLStack *This, void *e);
12 | static int popElem(DLLStack *This, void **e);
13 |
14 | DLLStack *InitDLLStack(){
15 | DLLStack *S = (DLLStack *)malloc(sizeof(DLLStack));
16 | if(!S) return NULL;
17 | DLLSNode *p = (DLLSNode *)malloc(sizeof(DLLSNode));
18 | if(!p){
19 | free(S);
20 | return NULL;
21 | }
22 | S->front = p;
23 | S->tear = S->front;
24 | p->prior = NULL;
25 | p->next = NULL;
26 | S->clear = clear;
27 | S->isEmpty = isEmpty;
28 | S->length = length;
29 | S->riseTraverse = riseTraverse;
30 | S->downTraverse = downTraverse;
31 | S->getTopElem = getTopElem;
32 | S->pushElem = pushElem;
33 | S->popElem = popElem;
34 | return S;
35 | }
36 |
37 | void DestroyDLLStack(DLLStack *S){
38 | if(S){
39 | if(S->front){
40 | S->clear(S);
41 | free(S->front);
42 | }
43 | free(S);
44 | S = NULL;
45 | }
46 | }
47 |
48 | static void clear(DLLStack *This){
49 | DLLSNode *p = This->front->next;
50 | DLLSNode *temp = NULL;
51 | while(p){
52 | temp = p;
53 | p = p->next;
54 | free(temp);
55 | }
56 | p = This->front;
57 | p->next = NULL;
58 | This->tear = This->front;
59 | }
60 |
61 | static int isEmpty(DLLStack *This){
62 | DLLSNode *p = This->front;
63 | if(p->next){
64 | return 0;
65 | }else{
66 | return 1;
67 | }
68 | }
69 |
70 | static int length(DLLStack *This){
71 | int j = 0;
72 | DLLSNode *p = This->front->next;
73 | while(p){
74 | j++;
75 | p = p->next;
76 | }
77 | return j;
78 | }
79 |
80 | static void riseTraverse(DLLStack *This,int (*visit)(void **e)){
81 | DLLSNode *p = This->front->next;
82 | while(p){
83 | if(visit(&(p->elem)) != 0) break;
84 | p = p->next;
85 | }
86 | }
87 |
88 | static void downTraverse(DLLStack *This,int (*visit)(void **e)){
89 | DLLSNode *p = This->tear;
90 | while(p != This->front){
91 | if(visit(&(p->elem)) != 0) break;
92 | p = p->prior;
93 | }
94 | }
95 |
96 |
97 | static int getTopElem(DLLStack *This, void **e){
98 | if(isEmpty(This)) return -1;
99 | *e = This->tear->elem;
100 | return 0;
101 | }
102 |
103 | static int pushElem(DLLStack *This, void *e){
104 | DLLSNode *p = This->tear;
105 | DLLSNode *temp = (DLLSNode *)malloc(sizeof(DLLSNode));
106 | if(!temp) return -1;
107 | temp->elem = e;
108 | p->next = temp;
109 | temp->prior = p;
110 | This->tear = temp;
111 | temp->next = NULL;
112 | return 0;
113 | }
114 |
115 | static int popElem(DLLStack *This, void **e){
116 | if(This->front == This->tear){
117 | *e = NULL;
118 | return -1;
119 | }
120 | DLLSNode *p = This->tear;
121 | DLLSNode *temp = NULL;
122 | temp = p->prior;
123 | *e = p->elem;
124 | free(p);
125 | This->tear = temp;
126 | temp->next = NULL;
127 | return 0;
128 | }
129 |
130 |
--------------------------------------------------------------------------------
/binary_tree/dll_stack.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _DLLSTACK_H
3 | #define _DLLSTACK_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef struct DLLSNode{
7 | void *elem;
8 | struct DLLSNode *prior;
9 | struct DLLSNode *next;
10 | }DLLSNode;
11 |
12 | typedef struct DLLStack{
13 | DLLSNode *front;
14 | DLLSNode *tear;
15 | void (*clear)(struct DLLStack *This);
16 | int (*isEmpty)(struct DLLStack *This);
17 | int (*length)(struct DLLStack *This);
18 | void (*riseTraverse)(struct DLLStack *This,int (*visit)(void **e));
19 | void (*downTraverse)(struct DLLStack *This,int (*visit)(void **e));
20 | int (*getTopElem)(struct DLLStack *This, void **e);
21 | int (*pushElem)(struct DLLStack *This, void *e);
22 | int (*popElem)(struct DLLStack *This, void **e);
23 | }DLLStack;
24 |
25 | /* Exported macro ------------------------------------------------------------*/
26 | DLLStack *InitDLLStack();
27 | void DestroyDLLStack(DLLStack *S);
28 |
29 | #endif
--------------------------------------------------------------------------------
/binary_tree/elem_type_define.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _ELEMTYPEDEFINE_H
3 | #define _ELEMTYPEDEFINE_H
4 |
5 | typedef int BiTNodeElemType;
6 | typedef BiTNode DLLSElemType;
7 | typedef BiTNode DLLQElemType;
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/double_circular_linked_list/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.12)
2 | project(double_circular_linked_list)
3 |
4 | set(CMAKE_CXX_STANDARD 14)
5 |
6 | add_executable(double_circular_linked_list test_double_circular_linked_list.c double_circular_linked_list.c)
--------------------------------------------------------------------------------
/double_circular_linked_list/double_circular_linked_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "double_circular_linked_list.h"
4 |
5 | static void clear(struct double_circular_linked_list *linked_list);
6 | static int is_empty(struct double_circular_linked_list *linked_list);
7 | static int length(struct double_circular_linked_list *linked_list);
8 | static void print(struct double_circular_linked_list *linked_list);
9 | static void circle_print(struct double_circular_linked_list *linked_list,int times);
10 | static int index_elem(struct double_circular_linked_list *linked_list, double_circular_elem_type* elem);
11 | static int index_node(struct double_circular_linked_list *linked_list, struct double_circular_linked_list_node* node);
12 | static int get_elem(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type *elem);
13 | static struct double_circular_linked_list_node *get_node(struct double_circular_linked_list *linked_list, int index);
14 | static struct double_circular_linked_list_node *get_prior_node(struct double_circular_linked_list_node *node);
15 | static struct double_circular_linked_list_node *get_next_node(struct double_circular_linked_list_node *node);
16 | static int modify_elem(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type* elem);
17 | static int insert_elem(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type *elem);
18 | static int delete_node(struct double_circular_linked_list *linked_list, struct double_circular_linked_list_node *node);
19 | static int append_elem(struct double_circular_linked_list *linked_list, double_circular_elem_type *elem);
20 | static int pop_elem(struct double_circular_linked_list *linked_list, double_circular_elem_type *elem);
21 |
22 | //初始化链表
23 | struct double_circular_linked_list *init_double_circular_linked_list(){
24 | //创建双循环链表类
25 | struct double_circular_linked_list *linked_list =
26 | (struct double_circular_linked_list *)malloc(sizeof(struct double_circular_linked_list));
27 | //创建头结点
28 | struct double_circular_linked_list_node *node =
29 | (struct double_circular_linked_list_node *)malloc(sizeof(struct double_circular_linked_list_node));
30 | linked_list->head = node; //链表实例的this指向头结点
31 | node->prior = node; //只有一个节点,上一个也指向这个节点
32 | node->next = node; //只有一个节点,下一个也指向这个节点
33 | linked_list->clear = clear;
34 | linked_list->is_empty = is_empty;
35 | linked_list->length = length;
36 | linked_list->print = print;
37 | linked_list->circle_print = circle_print;
38 | linked_list->index_elem = index_elem;
39 | linked_list->index_node = index_node;
40 | linked_list->get_elem = get_elem;
41 | linked_list->get_node = get_node;
42 | linked_list->get_prior_node = get_prior_node;
43 | linked_list->get_next_node = get_next_node;
44 | linked_list->modify_elem = modify_elem;
45 | linked_list->delete_node = delete_node;
46 | linked_list->append_elem = append_elem;
47 | linked_list->insert_elem = insert_elem;
48 | linked_list->pop_elem = pop_elem;
49 | return linked_list;
50 | }
51 |
52 | //销毁
53 | void destroy_double_circular_linked_list(struct double_circular_linked_list *linked_list){
54 | linked_list->clear(linked_list);
55 | free(linked_list->head);
56 | free(linked_list);
57 | linked_list = NULL;
58 | }
59 |
60 | //清除链表
61 | //会清除链表中的所有元素
62 | static void clear(struct double_circular_linked_list *linked_list){
63 | struct double_circular_linked_list_node *head = linked_list->head;
64 | struct double_circular_linked_list_node *next = linked_list->head->next;
65 | struct double_circular_linked_list_node *temp = NULL;
66 | while(next != head){
67 | temp = next;
68 | next = next->next;
69 | free(temp);
70 | }
71 | head->prior = head;
72 | head->next = head;
73 | }
74 |
75 | //判断链表是不是为空
76 | //返回0为空,1非空
77 | static int is_empty(struct double_circular_linked_list *linked_list){
78 | struct double_circular_linked_list_node *head = linked_list->head;
79 | if(head->next == head){
80 | return 0;
81 | }else{
82 | return 1;
83 | }
84 | }
85 |
86 | //获取链表长度
87 | static int length(struct double_circular_linked_list *linked_list){
88 | int j = 0;
89 | struct double_circular_linked_list_node *head = linked_list->head;
90 | struct double_circular_linked_list_node *next = linked_list->head->next;
91 | while(next != head){
92 | j++;
93 | next = next->next;
94 | }
95 | return j;
96 | }
97 |
98 | //打印链表中的所有元素
99 | static void print(struct double_circular_linked_list *linked_list){
100 | struct double_circular_linked_list_node *head = linked_list->head;
101 | struct double_circular_linked_list_node *next = linked_list->head->next;
102 | while(next != head){
103 | printf("%d ", next->elem);
104 | next = next->next;
105 | }
106 | printf("\n");
107 | }
108 |
109 | //循环打印times次
110 | static void circle_print(struct double_circular_linked_list *linked_list,int times){
111 | struct double_circular_linked_list_node *head = linked_list->head;
112 | int i = 0;
113 | struct double_circular_linked_list_node *next = linked_list->head->next;
114 | for(i = 0; ielem);
119 | }
120 | next = next->next;
121 | }
122 | printf("\n");
123 | }
124 |
125 | //返回元素再链表中的位置
126 | //返回-1 没有该元素
127 | static int index_elem(struct double_circular_linked_list *linked_list, double_circular_elem_type* elem){
128 | struct double_circular_linked_list_node *head = linked_list->head;
129 | struct double_circular_linked_list_node *next = linked_list->head->next;
130 | int pos = -1;
131 | int j = 0;
132 | while(next != head){
133 | if(*elem == next->elem){
134 | pos = j;
135 | }
136 | next = next->next;
137 | j++;
138 | }
139 | return pos;
140 | }
141 |
142 | //返回node在链表中的位置
143 | static int index_node(struct double_circular_linked_list *linked_list, struct double_circular_linked_list_node* node){
144 | struct double_circular_linked_list_node *head = linked_list->head;
145 | struct double_circular_linked_list_node *next = linked_list->head->next;
146 | int pos = -1;
147 | int j = 0;
148 | while(next != head){
149 | if(node == next){
150 | pos = j;
151 | }
152 | next = next->next;
153 | j++;
154 | }
155 | return pos;
156 | }
157 |
158 | //获取 index 位置的元素
159 | static int get_elem(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type *elem){
160 | struct double_circular_linked_list_node *head = linked_list->head;
161 | struct double_circular_linked_list_node *next = linked_list->head->next;
162 | int j = 0;
163 | while(next != head && j < index){
164 | next = next->next;
165 | j++;
166 | }
167 | if(next == head || j > index){
168 | return -1;
169 | }
170 | *elem = next->elem;
171 | return 0;
172 | }
173 |
174 | //返回 index 位置的 node
175 | static struct double_circular_linked_list_node *get_node(struct double_circular_linked_list *linked_list, int index){
176 | struct double_circular_linked_list_node *head = linked_list->head;
177 | struct double_circular_linked_list_node *next = linked_list->head->next;
178 | int j = 0;
179 | while(next != head && j < index){
180 | next = next->next;
181 | j++;
182 | }
183 | if(next == head || j > index) return NULL;
184 | return next;
185 | }
186 |
187 | //返回上一个节点
188 | static struct double_circular_linked_list_node *get_prior_node(struct double_circular_linked_list_node *node){
189 | return node->prior;
190 | }
191 |
192 | //返回 node 的下一个节点
193 | static struct double_circular_linked_list_node *get_next_node(struct double_circular_linked_list_node *node){
194 | return node->next;
195 | }
196 |
197 | //修改 index 位置的元素值
198 | static int modify_elem(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type* elem){
199 | struct double_circular_linked_list_node *head = linked_list->head;
200 | struct double_circular_linked_list_node *next = linked_list->head->next;
201 | int j = 0;
202 | while(next != head && j < index){
203 | next = next->next;
204 | j++;
205 | }
206 | if(next == head || j > index) return -1;
207 | next->elem = *elem;
208 | return 0;
209 | }
210 |
211 | //在 index 位置插入元素
212 | static int insert_elem(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type *elem){
213 | struct double_circular_linked_list_node *head = linked_list->head;
214 | struct double_circular_linked_list_node *next = linked_list->head->next;
215 | int j = 0;
216 | //创造一个中间节点
217 | struct double_circular_linked_list_node *temp =
218 | (struct double_circular_linked_list_node *)malloc(sizeof(struct double_circular_linked_list_node));
219 | if(!temp){
220 | return -1;
221 | }
222 | //找到 index 的位置
223 | while(next->next != head && j < index){
224 | next = next->next;
225 | j++;
226 | }
227 | if(next->next == head || j > index){
228 | return -1;
229 | }
230 | temp->elem = *elem;
231 | next->next->prior = temp;
232 | temp->prior = next;
233 | temp->next = next->next;
234 | next->next = temp;
235 | return 0;
236 | }
237 |
238 | //删除 node 节点
239 | static int delete_node(struct double_circular_linked_list *linked_list, struct double_circular_linked_list_node *node){
240 | if(index_node(linked_list, node)>=0){
241 | node->prior->next = node->next;
242 | node->next->prior = node->prior;
243 | free(node);
244 | }
245 | return 0;
246 | }
247 |
248 | static int delete_elem(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type* elem){
249 | struct double_circular_linked_list_node *head = linked_list->head;
250 | struct double_circular_linked_list_node *next = linked_list->head->next;
251 | struct double_circular_linked_list_node *temp = NULL;
252 | int j = 0;
253 | while(next->next != head && j < index){
254 | next = next->next;
255 | j++;
256 | }
257 | if(next->next == head || j > index) return -1;
258 | temp = next->next;
259 | next->next = temp->next;
260 | temp->next->prior = next;
261 | *elem = temp->elem;
262 | free(temp);
263 | return 0;
264 | }
265 |
266 | //在尾部添加元素
267 | static int append_elem(struct double_circular_linked_list *linked_list, double_circular_elem_type *elem){
268 | struct double_circular_linked_list_node *head = linked_list->head;
269 | struct double_circular_linked_list_node *next = linked_list->head->next;
270 | struct double_circular_linked_list_node *temp =
271 | (struct double_circular_linked_list_node *)malloc(sizeof(struct double_circular_linked_list_node));
272 | if(!temp) return -1;
273 | while(next->next != head){
274 | next = next->next;
275 | }
276 | temp->elem = *elem;
277 | temp->prior = next;
278 | temp->next = head;
279 | next->next = temp;
280 | head->prior = temp;
281 | return 0;
282 | }
283 |
284 | static int pop_elem(struct double_circular_linked_list *linked_list, double_circular_elem_type *elem){
285 | struct double_circular_linked_list_node *head = linked_list->head;
286 | struct double_circular_linked_list_node *next = linked_list->head->next;
287 | struct double_circular_linked_list_node *temp = NULL;
288 | while(next->next->next != head){
289 | next = next->next;
290 | }
291 | temp = next->next;
292 | if(temp == head){
293 | return -1;
294 | }
295 | *elem = temp->elem;
296 | free(temp);
297 | next->next = head;
298 | head->prior = next;
299 | return 0;
300 | }
301 |
302 |
--------------------------------------------------------------------------------
/double_circular_linked_list/double_circular_linked_list.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __DOUBLE_CIRCULAR_LINKED_LIST_H
3 | #define __DOUBLE_CIRCULAR_LINKED_LIST_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef int double_circular_elem_type; //数据元素的类型,假设是int型的,这个应该在使用的文件中定义
7 |
8 | struct double_circular_linked_list_node{ //这个应该在使用的文件中定义
9 | struct double_circular_linked_list_node *prior;
10 | struct double_circular_linked_list_node *next;
11 | double_circular_elem_type elem; //存储空间
12 | };
13 |
14 | struct double_circular_linked_list{
15 | struct double_circular_linked_list_node *head;
16 | void (*clear)(struct double_circular_linked_list *linked_list);
17 | int (*is_empty)(struct double_circular_linked_list *linked_list);
18 | int (*length)(struct double_circular_linked_list *linked_list);
19 | void (*print)(struct double_circular_linked_list *linked_list);
20 | void (*circle_print)(struct double_circular_linked_list *linked_list,int times);
21 | int (*index_elem)(struct double_circular_linked_list *linked_list, double_circular_elem_type* elem);
22 | int (*index_node)(struct double_circular_linked_list *linked_list, struct double_circular_linked_list_node* node);
23 | int (*get_elem)(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type *elem);
24 | struct double_circular_linked_list_node *(*get_node)(struct double_circular_linked_list *linked_list, int index);
25 | struct double_circular_linked_list_node *(*get_prior_node)(struct double_circular_linked_list_node *node);
26 | struct double_circular_linked_list_node *(*get_next_node)(struct double_circular_linked_list_node *node);
27 | int (*modify_elem)(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type* elem);
28 | int (*insert_elem)(struct double_circular_linked_list *linked_list, int index, double_circular_elem_type *elem);
29 | int (*delete_node)(struct double_circular_linked_list *linked_list, struct double_circular_linked_list_node *node);
30 | int (*append_elem)(struct double_circular_linked_list *linked_list, double_circular_elem_type *elem);
31 | int (*pop_elem)(struct double_circular_linked_list *linked_list, double_circular_elem_type *elem);
32 | };
33 |
34 | /* Exported macro ------------------------------------------------------------*/
35 | struct double_circular_linked_list* init_double_circular_linked_list();
36 | void destroy_double_circular_linked_list(struct double_circular_linked_list *linked_list);
37 |
38 | #endif
--------------------------------------------------------------------------------
/double_circular_linked_list/test_double_circular_linked_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "double_circular_linked_list.h"
4 |
5 | int main(void){
6 | int i;
7 | double_circular_elem_type elem,elem1;
8 | struct double_circular_linked_list_node *tempn = NULL;
9 | struct double_circular_linked_list_node *tempm = NULL;
10 | struct double_circular_linked_list *list = init_double_circular_linked_list();
11 | printf("list is empty:%d\n",list->is_empty(list));
12 | for(i=0;i<10;i++){
13 | list->append_elem(list,&i);
14 | }
15 | list->print(list);
16 | printf("list is empty:%d\n",list->is_empty(list));
17 | printf("list length:%d\n",list->length(list));
18 | list->clear(list);
19 | for (i = 10; i < 20; i++){
20 | list->append_elem(list,&i);
21 | }
22 | list->print(list);
23 | list->get_elem(list,3,&elem1);
24 | printf("the elem of index 3 is %d\n",elem1);
25 | elem = 31;
26 | list->modify_elem(list,3,&elem);
27 | list->get_elem(list,3,&elem1);
28 | printf("modify the elem of index 3 to %d\n",elem1);
29 | list->print(list);
30 | elem = 25;
31 | list->insert_elem(list,5,&elem);
32 | printf("insert elem %d to index 5\n",elem);
33 | list->print(list);
34 | elem = 14;
35 | printf("the index of 14 is %d\n",list->index_elem(list,&elem));
36 | list->pop_elem(list,&elem);
37 | printf("pop elem %d\n",elem);
38 | list->print(list);
39 | printf("circle print 3 times:\n");
40 | list->circle_print(list,3);
41 | tempn = list->get_node(list,5);
42 | printf("get node of index 5: node elem = %d\n",tempn->elem);
43 | printf("the index of node: %d\n",list->index_node(list, tempn));
44 | tempm = list->get_prior_node(tempn);
45 | printf("get Prior node of index 5: Prior node elem = %d\n",tempm->elem);
46 | tempm = list->get_next_node(tempn);
47 | printf("get Next node of index 5: Next node elem = %d\n",tempm->elem);
48 | list->delete_node(list,tempn);
49 | printf("delete node of index 5\n");
50 | list->print(list);
51 | destroy_double_circular_linked_list(list);
52 | return 0;
53 | }
--------------------------------------------------------------------------------
/double_linked_list/double_linked_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "DoubleLinkedList.h"
4 |
5 | static void clear(DoubleLinkedList *This);
6 | static int isEmpty(DoubleLinkedList *This);
7 | static int length(DoubleLinkedList *This);
8 | static void print(DoubleLinkedList *This);
9 | static int indexElem(DoubleLinkedList *This, ElemType* x);
10 | static int getElem(DoubleLinkedList *This, int index, ElemType *e);
11 | static int modifyElem(DoubleLinkedList *This, int index, ElemType* e);
12 | static int deleteElem(DoubleLinkedList *This, int index, ElemType* e);
13 | static int appendElem(DoubleLinkedList *This, ElemType *e);
14 | static int insertElem(DoubleLinkedList *This, int index, ElemType *e);
15 | static int popElem(DoubleLinkedList *This, ElemType* e);
16 |
17 | DoubleLinkedList *InitDoubleLinkedList(){
18 | DoubleLinkedList *L = (DoubleLinkedList *)malloc(sizeof(DoubleLinkedList));
19 | Node *p = (Node *)malloc(sizeof(Node));
20 | L->This = p;
21 | p->prior = NULL;
22 | p->next = NULL;
23 | L->clear = clear;
24 | L->isEmpty = isEmpty;
25 | L->length = length;
26 | L->print = print;
27 | L->indexElem = indexElem;
28 | L->getElem = getElem;
29 | L->modifyElem = modifyElem;
30 | L->deleteElem = deleteElem;
31 | L->appendElem = appendElem;
32 | L->insertElem = insertElem;
33 | L->popElem = popElem;
34 | return L;
35 | }
36 |
37 | void DestroyDoubleLinkedList(DoubleLinkedList *L){
38 | L->clear(L);
39 | free(L->This);
40 | free(L);
41 | L = NULL;
42 | }
43 |
44 | static void clear(DoubleLinkedList *This){
45 | Node *p = This->This->next;
46 | Node *temp = NULL;
47 | while(p){
48 | temp = p;
49 | p = p->next;
50 | free(temp);
51 | }
52 | p = This->This;
53 | p->next = NULL;
54 | }
55 |
56 | static int isEmpty(DoubleLinkedList *This){
57 | Node *p = This->This;
58 | if(p->next){
59 | return 0;
60 | }else{
61 | return 1;
62 | }
63 | }
64 |
65 | static int length(DoubleLinkedList *This){
66 | int j = 0;
67 | Node *p = This->This->next;
68 | while(p){
69 | j++;
70 | p = p->next;
71 | }
72 | return j;
73 | }
74 |
75 | static void print(DoubleLinkedList *This){
76 | Node *p = This->This->next;
77 | while(p){
78 | printf("%d ", p->elem);
79 | p = p->next;
80 | }
81 | printf("\n");
82 | }
83 |
84 | static int indexElem(DoubleLinkedList *This, ElemType* e){
85 | Node *p = This->This->next;
86 | int pos = -1;
87 | int j = 0;
88 | while(p){
89 | if(*e == p->elem){
90 | pos = j;
91 | }
92 | p = p->next;
93 | j++;
94 | }
95 | return pos;
96 | }
97 |
98 | static int getElem(DoubleLinkedList *This, int index, ElemType *e){
99 | Node *p = This->This->next;
100 | int j = 0;
101 | while(p && j < index){
102 | p = p->next;
103 | j++;
104 | }
105 | if(!p || j > index) return -1;
106 | *e = p->elem;
107 | return 0;
108 | }
109 |
110 | static int modifyElem(DoubleLinkedList *This, int index, ElemType* e){
111 | Node *p = This->This->next;
112 | int j = 0;
113 | while(p && j < index){
114 | p = p->next;
115 | j++;
116 | }
117 | if(!p || j > index) return -1;
118 | p->elem = *e;
119 | return 0;
120 | }
121 |
122 | static int insertElem(DoubleLinkedList *This, int index, ElemType *e){
123 | Node *p = This->This;
124 | int j = 0;
125 | Node *temp = (Node *)malloc(sizeof(Node));
126 | if(!temp) return -1;
127 | while(p && j < index){
128 | p = p->next;
129 | j++;
130 | }
131 | if(!p || j > index) return -1;
132 | temp->elem = *e;
133 | p->next->prior = temp;
134 | temp->prior = p;
135 | temp->next = p->next;
136 | p->next = temp;
137 | return 0;
138 | }
139 |
140 | static int deleteElem(DoubleLinkedList *This, int index, ElemType* e){
141 | Node *p = This->This;
142 | Node *temp = NULL;
143 | int j = 0;
144 | while(p->next && j < index){
145 | p = p->next;
146 | j++;
147 | }
148 | if(!p->next || j > index) return -1;
149 | temp = p->next;
150 | p->next = temp->next;
151 | temp->next->prior = p;
152 | *e = temp->elem;
153 | free(temp);
154 | return 0;
155 | }
156 |
157 | static int appendElem(DoubleLinkedList *This, ElemType *e){
158 | Node *p = This->This;
159 | Node *temp = (Node *)malloc(sizeof(Node));
160 | if(!temp) return -1;
161 | while(p){
162 | if(NULL == p->next){
163 | temp->elem = *e;
164 | p->next = temp;
165 | temp->prior = p;
166 | temp->next = NULL;
167 | }
168 | p = p->next;
169 | }
170 | return 0;
171 | }
172 |
173 | static int popElem(DoubleLinkedList *This, ElemType* e){
174 | Node *p = This->This;
175 | Node *temp = NULL;
176 | while(p->next->next){
177 | p = p->next;
178 | }
179 | temp = p->next;
180 | if(!temp) return -1;
181 | *e = temp->elem;
182 | free(temp);
183 | p->next = NULL;
184 | return 0;
185 | }
186 |
187 |
--------------------------------------------------------------------------------
/double_linked_list/double_linked_list.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __DOUBLELINKEDLIST_H
3 | #define __DOUBLELINKEDLIST_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef int ElemType; //数据元素的类型,假设是int型的
7 |
8 | typedef struct Node{
9 | ElemType elem; //存储空间
10 | struct Node *prior;
11 | struct Node *next;
12 | }Node;
13 |
14 | typedef struct DoubleLinkedList{
15 | Node *This;
16 | void (*clear)(struct DoubleLinkedList *This);
17 | int (*isEmpty)(struct DoubleLinkedList *This);
18 | int (*length)(struct DoubleLinkedList *This);
19 | void (*print)(struct DoubleLinkedList *This);
20 | int (*indexElem)(struct DoubleLinkedList *This, ElemType* x);
21 | int (*getElem)(struct DoubleLinkedList *This, int index, ElemType *e);
22 | int (*modifyElem)(struct DoubleLinkedList *This, int index, ElemType* e);
23 | int (*deleteElem)(struct DoubleLinkedList *This, int index, ElemType* e);
24 | int (*appendElem)(struct DoubleLinkedList *This, ElemType *e);
25 | int (*insertElem)(struct DoubleLinkedList *This, int index, ElemType *e);
26 | int (*popElem)(struct DoubleLinkedList *This, ElemType* e);
27 | }DoubleLinkedList;
28 |
29 | /* Exported macro ------------------------------------------------------------*/
30 | DoubleLinkedList *InitDoubleLinkedList();
31 | void DestroyDoubleLinkedList(DoubleLinkedList *L);
32 |
33 | #endif
--------------------------------------------------------------------------------
/double_linked_list/test_double_linked_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "DoubleLinkedList.h"
4 |
5 | int main(void){
6 | int i;
7 | ElemType elem,elem1;
8 | DoubleLinkedList *list = InitDoubleLinkedList();
9 | printf("list is empty:%d\n",list->isEmpty(list));
10 | for(i=0;i<10;i++){
11 | list->appendElem(list,&i);
12 | }
13 | list->print(list);
14 | printf("list is empty:%d\n",list->isEmpty(list));
15 | printf("list length:%d\n",list->length(list));
16 | list->clear(list);
17 | for (i = 10; i < 20; i++){
18 | list->appendElem(list,&i);
19 | }
20 | list->print(list);
21 | list->getElem(list,3,&elem1);
22 | printf("the elem of index 3 is %d\n",elem1);
23 | elem = 31;
24 | list->modifyElem(list,3,&elem);
25 | list->getElem(list,3,&elem1);
26 | printf("modify the elem of index 3 to %d\n",elem1);
27 | list->print(list);
28 | elem = 25;
29 | list->insertElem(list,5,&elem);
30 | printf("insert elem %d to index 5\n",elem);
31 | list->print(list);
32 | list->deleteElem(list,7,&elem);
33 | printf("delete elem %d of index 7\n",elem);
34 | list->print(list);
35 | elem = 14;
36 | printf("the index of 14 is %d\n",list->indexElem(list,&elem));
37 | list->popElem(list,&elem);
38 | printf("pop elem %d\n",elem);
39 | list->print(list);
40 | DestroyDoubleLinkedList(list);
41 | return 0;
42 | }
--------------------------------------------------------------------------------
/double_linked_list_queue/double_linked_list_queue.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "DLLQueue.h"
4 |
5 | static void clear(DLLQueue *This);
6 | static int isEmpty(DLLQueue *This);
7 | static int length(DLLQueue *This);
8 | static void riseTraverse(DLLQueue *This,int (*visit)(void **e));
9 | static void downTraverse(DLLQueue *This,int (*visit)(void **e));
10 | static int getHead(DLLQueue *This, void **e);
11 | static int enQueue(DLLQueue *This, void *e);
12 | static int deQueue(DLLQueue *This, void **e);
13 |
14 | DLLQueue *InitDLLQueue(){
15 | DLLQueue *Q = (DLLQueue *)malloc(sizeof(DLLQueue));
16 | if(!Q) return NULL;
17 | DLLQNode *p = (DLLQNode *)malloc(sizeof(DLLQNode));
18 | if(!p){
19 | free(Q);
20 | return NULL;
21 | }
22 | Q->front = p;
23 | Q->tear = Q->front;
24 | p->prior = NULL;
25 | p->next = NULL;
26 | Q->clear = clear;
27 | Q->isEmpty = isEmpty;
28 | Q->length = length;
29 | Q->riseTraverse = riseTraverse;
30 | Q->downTraverse = downTraverse;
31 | Q->getHead = getHead;
32 | Q->enQueue = enQueue;
33 | Q->deQueue = deQueue;
34 | return Q;
35 | }
36 |
37 | void DestroyDLLQueue(DLLQueue *Q){
38 | if(Q){
39 | if(Q->front){
40 | Q->clear(Q);
41 | free(Q->front);
42 | }
43 | free(Q);
44 | Q = NULL;
45 | }
46 | }
47 |
48 | static void clear(DLLQueue *This){
49 | DLLQNode *p = This->front->next;
50 | DLLQNode *temp = NULL;
51 | while(p){
52 | temp = p;
53 | p = p->next;
54 | free(temp);
55 | }
56 | p = This->front;
57 | p->next = NULL;
58 | This->tear = This->front;
59 | }
60 |
61 | static int isEmpty(DLLQueue *This){
62 | DLLQNode *p = This->front;
63 | if(p->next){
64 | return 0;
65 | }else{
66 | return 1;
67 | }
68 | }
69 |
70 | static int length(DLLQueue *This){
71 | int j = 0;
72 | DLLQNode *p = This->front->next;
73 | while(p){
74 | j++;
75 | p = p->next;
76 | }
77 | return j;
78 | }
79 |
80 | static void riseTraverse(DLLQueue *This,int (*visit)(void **e)){
81 | DLLQNode *p = This->front->next;
82 | while(p){
83 | if(visit(&(p->elem)) != 0) break;
84 | p = p->next;
85 | }
86 | }
87 |
88 | static void downTraverse(DLLQueue *This,int (*visit)(void **e)){
89 | DLLQNode *p = This->tear;
90 | while(p != This->front){
91 | if(visit(&(p->elem)) != 0) break;
92 | p = p->prior;
93 | }
94 | }
95 |
96 |
97 | static int getHead(DLLQueue *This, void **e){
98 | if(isEmpty(This)) return -1;
99 | *e = This->front->next->elem;
100 | return 0;
101 | }
102 |
103 | static int enQueue(DLLQueue *This, void *e){
104 | DLLQNode *p = This->tear;
105 | DLLQNode *temp = (DLLQNode *)malloc(sizeof(DLLQNode));
106 | if(!temp) return -1;
107 | temp->elem = e;
108 | p->next = temp;
109 | temp->prior = p;
110 | This->tear = temp;
111 | temp->next = NULL;
112 | return 0;
113 | }
114 |
115 | static int deQueue(DLLQueue *This, void **e){
116 | if(This->front == This->tear){
117 | *e = NULL;
118 | return -1;
119 | }
120 | DLLQNode *p = This->front->next;
121 | *e = p->elem;
122 | This->front->next = p->next;
123 | if(This->tear == p){
124 | This->tear = This->front;
125 | }else{
126 | p->next->prior = This->front;
127 | }
128 | free(p);
129 | return 0;
130 | }
131 |
132 |
--------------------------------------------------------------------------------
/double_linked_list_queue/double_linked_list_queue.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _DLLQUEUE_H
3 | #define _DLLQUEUE_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef struct DLLQNode{
7 | void *elem;
8 | struct DLLQNode *prior;
9 | struct DLLQNode *next;
10 | }DLLQNode;
11 |
12 | typedef struct DLLQueue{
13 | DLLQNode *front;
14 | DLLQNode *tear;
15 | void (*clear)(struct DLLQueue *This);
16 | int (*isEmpty)(struct DLLQueue *This);
17 | int (*length)(struct DLLQueue *This);
18 | void (*riseTraverse)(struct DLLQueue *This,int (*visit)(void **e));
19 | void (*downTraverse)(struct DLLQueue *This,int (*visit)(void **e));
20 | int (*getHead)(struct DLLQueue *This, void **e);
21 | int (*enQueue)(struct DLLQueue *This, void *e);
22 | int (*deQueue)(struct DLLQueue *This, void **e);
23 | }DLLQueue;
24 |
25 | /* Exported macro ------------------------------------------------------------*/
26 | DLLQueue *InitDLLQueue();
27 | void DestroyDLLQueue(DLLQueue *Q);
28 |
29 | #endif
--------------------------------------------------------------------------------
/double_linked_list_queue/test_double_linked_list_queue.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "DLLQueue.h"
4 |
5 | int printElem(void **e){
6 | printf("%d",*((int *)*e));
7 | return 0;
8 | }
9 |
10 | int main(void){
11 | int i = 0,*elem;
12 | int *num = (int *)malloc(10*sizeof(int));
13 | if(!num) return 0;
14 | for(i=0;i<10;i++){
15 | *(num + i) = i;
16 | }
17 | DLLQueue *Q = InitDLLQueue();
18 | printf("is DLLQueue empty:%d\n",Q->isEmpty(Q));
19 | printf("DLLQueue length:%d\n",Q->length(Q));
20 | for(i=0;i<10;i++){
21 | Q->enQueue(Q,num + i);
22 | }
23 | printf("is DLLQueue empty:%d\n",Q->isEmpty(Q));
24 | printf("DLLQueue length:%d\n",Q->length(Q));
25 | Q->riseTraverse(Q,printElem);
26 | printf("\n");
27 | Q->clear(Q);
28 | printf("is DLLQueue empty:%d\n",Q->isEmpty(Q));
29 | printf("DLLQueue length:%d\n",Q->length(Q));
30 | for(i=0;i<10;i++){
31 | *(num + i) = i*2;
32 | Q->enQueue(Q,num + i);
33 | }
34 | Q->downTraverse(Q,printElem);
35 | printf("\n");
36 | for(i=0;i<10;i++){
37 | Q->getHead(Q,(void **)&elem);
38 | printf("Head elem:%d\n",*elem);
39 | Q->deQueue(Q,(void **)&elem);
40 | printf("deQueue elem:%d\n",*elem);
41 | }
42 | printf("is DLLQueue empty:%d\n",Q->isEmpty(Q));
43 | printf("DLLQueue length:%d\n",Q->length(Q));
44 | DestroyDLLQueue(Q);
45 | free(num);
46 | return 0;
47 | }
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/double_linked_list_stack/double_linked_list_stack.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "DLLStack.h"
4 |
5 | static void clear(DLLStack *This);
6 | static int isEmpty(DLLStack *This);
7 | static int length(DLLStack *This);
8 | static void riseTraverse(DLLStack *This,int (*visit)(void **e));
9 | static void downTraverse(DLLStack *This,int (*visit)(void **e));
10 | static int getTopElem(DLLStack *This, void **e);
11 | static int pushElem(DLLStack *This, void *e);
12 | static int popElem(DLLStack *This, void **e);
13 |
14 | DLLStack *InitDLLStack(){
15 | DLLStack *S = (DLLStack *)malloc(sizeof(DLLStack));
16 | if(!S) return NULL;
17 | DLLSNode *p = (DLLSNode *)malloc(sizeof(DLLSNode));
18 | if(!p){
19 | free(S);
20 | return NULL;
21 | }
22 | S->front = p;
23 | S->tear = S->front;
24 | p->prior = NULL;
25 | p->next = NULL;
26 | S->clear = clear;
27 | S->isEmpty = isEmpty;
28 | S->length = length;
29 | S->riseTraverse = riseTraverse;
30 | S->downTraverse = downTraverse;
31 | S->getTopElem = getTopElem;
32 | S->pushElem = pushElem;
33 | S->popElem = popElem;
34 | return S;
35 | }
36 |
37 | void DestroyDLLStack(DLLStack *S){
38 | if(S){
39 | if(S->front){
40 | S->clear(S);
41 | free(S->front);
42 | }
43 | free(S);
44 | S = NULL;
45 | }
46 | }
47 |
48 | static void clear(DLLStack *This){
49 | DLLSNode *p = This->front->next;
50 | DLLSNode *temp = NULL;
51 | while(p){
52 | temp = p;
53 | p = p->next;
54 | free(temp);
55 | }
56 | p = This->front;
57 | p->next = NULL;
58 | This->tear = This->front;
59 | }
60 |
61 | static int isEmpty(DLLStack *This){
62 | DLLSNode *p = This->front;
63 | if(p->next){
64 | return 0;
65 | }else{
66 | return 1;
67 | }
68 | }
69 |
70 | static int length(DLLStack *This){
71 | int j = 0;
72 | DLLSNode *p = This->front->next;
73 | while(p){
74 | j++;
75 | p = p->next;
76 | }
77 | return j;
78 | }
79 |
80 | static void riseTraverse(DLLStack *This,int (*visit)(void **e)){
81 | DLLSNode *p = This->front->next;
82 | while(p){
83 | if(visit(&(p->elem)) != 0) break;
84 | p = p->next;
85 | }
86 | }
87 |
88 | static void downTraverse(DLLStack *This,int (*visit)(void **e)){
89 | DLLSNode *p = This->tear;
90 | while(p != This->front){
91 | if(visit(&(p->elem)) != 0) break;
92 | p = p->prior;
93 | }
94 | }
95 |
96 |
97 | static int getTopElem(DLLStack *This, void **e){
98 | if(isEmpty(This)) return -1;
99 | *e = This->tear->elem;
100 | return 0;
101 | }
102 |
103 | static int pushElem(DLLStack *This, void *e){
104 | DLLSNode *p = This->tear;
105 | DLLSNode *temp = (DLLSNode *)malloc(sizeof(DLLSNode));
106 | if(!temp) return -1;
107 | temp->elem = e;
108 | p->next = temp;
109 | temp->prior = p;
110 | This->tear = temp;
111 | temp->next = NULL;
112 | return 0;
113 | }
114 |
115 | static int popElem(DLLStack *This, void **e){
116 | if(This->front == This->tear){
117 | *e = NULL;
118 | return -1;
119 | }
120 | DLLSNode *p = This->tear;
121 | DLLSNode *temp = NULL;
122 | temp = p->prior;
123 | *e = p->elem;
124 | free(p);
125 | This->tear = temp;
126 | temp->next = NULL;
127 | return 0;
128 | }
129 |
130 |
--------------------------------------------------------------------------------
/double_linked_list_stack/double_linked_list_stack.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _DLLSTACK_H
3 | #define _DLLSTACK_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef struct DLLSNode{
7 | void *elem;
8 | struct DLLSNode *prior;
9 | struct DLLSNode *next;
10 | }DLLSNode;
11 |
12 | typedef struct DLLStack{
13 | DLLSNode *front;
14 | DLLSNode *tear;
15 | void (*clear)(struct DLLStack *This);
16 | int (*isEmpty)(struct DLLStack *This);
17 | int (*length)(struct DLLStack *This);
18 | void (*riseTraverse)(struct DLLStack *This,int (*visit)(void **e));
19 | void (*downTraverse)(struct DLLStack *This,int (*visit)(void **e));
20 | int (*getTopElem)(struct DLLStack *This, void **e);
21 | int (*pushElem)(struct DLLStack *This, void *e);
22 | int (*popElem)(struct DLLStack *This, void **e);
23 | }DLLStack;
24 |
25 | /* Exported macro ------------------------------------------------------------*/
26 | DLLStack *InitDLLStack();
27 | void DestroyDLLStack(DLLStack *S);
28 |
29 | #endif
--------------------------------------------------------------------------------
/double_linked_list_stack/test_double_linked_list_stack.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "DLLStack.h"
4 |
5 | int printElem(void **e){
6 | printf("%d",*((int *)*e));
7 | return 0;
8 | }
9 |
10 | int main(void){
11 | int i = 0,*elem;
12 | int *num = (int *)malloc(10*sizeof(int));
13 | if(!num) return 0;
14 | for(i=0;i<10;i++){
15 | *(num + i) = i;
16 | }
17 | DLLStack *S = InitDLLStack();
18 | printf("is DLLStack empty:%d\n",S->isEmpty(S));
19 | printf("DLLStack length:%d\n",S->length(S));
20 | for(i=0;i<10;i++){
21 | S->pushElem(S,num + i);
22 | }
23 | printf("is DLLStack empty:%d\n",S->isEmpty(S));
24 | printf("DLLStack length:%d\n",S->length(S));
25 | S->riseTraverse(S,printElem);
26 | printf("\n");
27 | S->clear(S);
28 | printf("is DLLStack empty:%d\n",S->isEmpty(S));
29 | printf("DLLStack length:%d\n",S->length(S));
30 | for(i=0;i<10;i++){
31 | *(num + i) = i*2;
32 | S->pushElem(S,num + i);
33 | }
34 | S->downTraverse(S,printElem);
35 | printf("\n");
36 | for(i=0;i<10;i++){
37 | S->getTopElem(S,(void **)(&elem));
38 | printf("top elem:%d\n",*elem);
39 | S->popElem(S,(void **)(&elem));
40 | printf("pop elem:%d\n",*elem);
41 | }
42 | printf("is DLLStack empty:%d\n",S->isEmpty(S));
43 | printf("DLLStack length:%d\n",S->length(S));
44 | DestroyDLLStack(S);
45 | free(num);
46 | return 0;
47 | }
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/g_list/g_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Glist.h"
4 |
5 | static int isEmpty(GList *This);
6 | static int length(GList *This);
7 | static void print(GList *This);
8 | static int getDepth(GList *This);
9 | static int getHead(GList *This,GLNode **n);
10 | static int getTear(GList *This,GLNode **n);
11 | static int getGLNode(GList *This,int index,GLNode **n);
12 | static int insertGLNode(GList *This, int index, GLNode *n);
13 | static int insertGLNodeFront(GList *This, GLNode *n);
14 | static int deleteGLNodeFront(GList *This);
15 | static int insertGLNodeTear(GList *This, GLNode *n);
16 | static int deleteGLNodeTear(GList *This);
17 | static void traversal(GList *This,int (*visit)(GLNodeElemType **elem));
18 |
19 | static GLNode *createGListFromString(GLNode **p,MyString *gListStr){
20 | int i;
21 | GLNode **create_temp = p;
22 | GLNode *glnode_temp = NULL;
23 | GLNode *glnode_root = NULL;
24 | MyString *elemstr = NULL;
25 | MyString *leftstr = NULL;
26 | MyString *substr = substrMyString(gListStr,1,gListStr->length-1);//脱去括号
27 | destroyMyString(gListStr);
28 | if(!substr){ //括号内是一个空表
29 | *create_temp = (GLNode*)malloc(sizeof(GLNode)); //添加原子结点
30 | if(*create_temp){
31 | (*create_temp)->tag = ATOM;
32 | (*create_temp)->content.elem = elemstr;
33 | (*create_temp)->prior = NULL;
34 | (*create_temp)->next = NULL;
35 | }
36 | glnode_root = *create_temp;
37 | }else{
38 | do{
39 | if(substr){
40 | if(*(substr->str) != '['){
41 | i = myStringIndexChar(substr,',',0);
42 | if(i != -1){
43 | elemstr = substrMyString(substr,0,i);//提取表头
44 | leftstr = substrMyString(substr,i+1,substr->length);//提取剩余部分
45 | destroyMyString(substr);
46 | substr = leftstr;
47 | }else{
48 | elemstr = substrMyString(substr,0,substr->length);
49 | destroyMyString(substr);
50 | }
51 | glnode_temp = *create_temp;
52 | *create_temp = (GLNode*)malloc(sizeof(GLNode)); //添加原子结点
53 | if(*create_temp){
54 | (*create_temp)->tag = ATOM;
55 | (*create_temp)->content.elem = elemstr;
56 | if(glnode_temp){
57 | (*create_temp)->prior = glnode_temp;
58 | glnode_temp->next = *create_temp;
59 | }else{
60 | (*create_temp)->prior = NULL;
61 | glnode_root = *create_temp;
62 | }
63 | (*create_temp)->next = NULL;
64 | }
65 | }else{
66 | i = getMatchingBracketIndex(substr,'[',0);
67 | if(i != -1){
68 | elemstr = substrMyString(substr,0,i+1);//提取表头,完整的一个Glist
69 | leftstr = substrMyString(substr,i+2,substr->length);//提取剩余部分
70 | destroyMyString(substr);
71 | substr = leftstr;
72 | }
73 | glnode_temp = *create_temp;
74 | *create_temp = (GLNode*)malloc(sizeof(GLNode)); //添加列表结点
75 | if(*create_temp){
76 | (*create_temp)->tag = LIST;
77 | if(glnode_temp){
78 | (*create_temp)->prior = glnode_temp;
79 | glnode_temp->next = *create_temp;
80 | }else{
81 | (*create_temp)->prior = NULL;
82 | glnode_root = *create_temp;
83 | }
84 | (*create_temp)->next = NULL;
85 | (*create_temp)->content.hp = NULL;
86 | (*create_temp)->content.hp = createGListFromString(&((*create_temp)->content.hp),elemstr);
87 | }
88 | }
89 | }else{
90 | break;
91 | }
92 | }while(i != -1);
93 | }
94 | return glnode_root;
95 | }
96 |
97 | GList *initGListFromString(char *listStr){
98 | MyString *gListStr = NULL;
99 | GLNode *p = NULL;
100 | GList *L = (GList *)malloc(sizeof(GList));
101 | if(!L) return NULL;
102 | gListStr = myStringAssign(listStr);
103 | if(!gListStr){
104 | free(L);
105 | return NULL;
106 | }
107 | if(*(gListStr->str) != '['){
108 | printf("ListStr format error\n");
109 | destroyMyString(gListStr);
110 | free(L);
111 | return NULL;
112 | }
113 | if(isBracketMatching(gListStr,'[') == 0){
114 | printf("ListStr Bracke not match\n");
115 | destroyMyString(gListStr);
116 | free(L);
117 | return NULL;
118 | }
119 | L->root = NULL;
120 | L->root = createGListFromString(&(L->root),gListStr);
121 | p = L->root;
122 | while(p->next){
123 | p = p->next;
124 | }
125 | L->tear = p;
126 | L->isEmpty = isEmpty;
127 | L->length = length;
128 | L->print = print;
129 | L->getDepth = getDepth;
130 | L->getHead = getHead;
131 | L->getTear = getTear;
132 | L->getGLNode = getGLNode;
133 | L->insertGLNode = insertGLNode;
134 | L->insertGLNodeFront = insertGLNodeFront;
135 | L->deleteGLNodeFront = deleteGLNodeFront;
136 | L->insertGLNodeTear = insertGLNodeTear;
137 | L->deleteGLNodeTear = deleteGLNodeTear;
138 | L->traversal = traversal;
139 | return L;
140 | }
141 |
142 | GList *initGListFromGLNode(GLNode *n){
143 | GLNode *p = NULL;
144 | GList *L = (GList *)malloc(sizeof(GList));
145 | if(!L) return NULL;
146 | L->root = NULL;
147 | p = copyGLNode(n);
148 | if(p){
149 | if(p->tag == ATOM){
150 | L->root = p;
151 | }else{ //脱下外层的list
152 | L->root = p->content.hp;
153 | free(p);
154 | }
155 | }
156 | p = L->root;
157 | while(p->next){
158 | p = p->next;
159 | }
160 | L->tear = p;
161 | L->isEmpty = isEmpty;
162 | L->length = length;
163 | L->print = print;
164 | L->getDepth = getDepth;
165 | L->getHead = getHead;
166 | L->getTear = getTear;
167 | L->getGLNode = getGLNode;
168 | L->insertGLNode = insertGLNode;
169 | L->insertGLNodeFront = insertGLNodeFront;
170 | L->deleteGLNodeFront = deleteGLNodeFront;
171 | L->insertGLNodeTear = insertGLNodeTear;
172 | L->deleteGLNodeTear = deleteGLNodeTear;
173 | L->traversal = traversal;
174 | return L;
175 | }
176 |
177 | static void printGList(GLNode *n){
178 | GLNode *temp = n;
179 | if(temp){
180 | if(temp->tag == ATOM){
181 | if(temp->content.hp){
182 | printf("%s",temp->content.elem->str);
183 | }
184 | }else{
185 | printf("[");
186 | printGList(temp->content.hp);
187 | printf("]");
188 | }
189 | }
190 | temp = temp->next;
191 | while(temp){
192 | if(temp->tag == ATOM){
193 | if(temp->content.elem){
194 | printf(",%s",temp->content.elem->str);
195 | }
196 | }else{
197 | printf(",[");
198 | printGList(temp->content.hp);
199 | printf("]");
200 | }
201 | temp = temp->next;
202 | }
203 | }
204 |
205 | static void print(GList *This){
206 | if(This){
207 | if(This->root){
208 | printf("[");
209 | printGList(This->root);
210 | printf("]\n");
211 | }
212 | }
213 | }
214 |
215 | void DestroyGList(GList *L){
216 | if(L){
217 | if(L->root){
218 | destroyGLNode(L->root);
219 | }
220 | free(L);
221 | L = NULL;
222 | }
223 | }
224 |
225 | static int isEmpty(GList *This){
226 | if(!This) return 1;
227 | if(This->root){
228 | return 0;
229 | }else{
230 | return 1;
231 | }
232 | }
233 |
234 | static int length(GList *This){
235 | int i = 0;
236 | if(!This) return 0;
237 | if(!This->root) return 0;
238 | GLNode *p = This->root;
239 | while(p){
240 | i++;
241 | p = p->next;
242 | }
243 | return i;
244 | }
245 |
246 | static int getGListDepth(GLNode *n){
247 | int max = 0;
248 | if(!n) return 1; // 空表深度为1
249 | GLNode *p = n;
250 | while(p){
251 | int dep = 0;
252 | if(p->tag == ATOM){
253 | dep = 0; //原子深度为0
254 | }else{
255 | dep = getGListDepth(p->content.hp);
256 | }
257 | if(dep > max) max = dep;
258 | p = p->next;
259 | }
260 | return max + 1; //非空表的深度是各类元素的深度的最大值加1
261 | }
262 |
263 | static int getDepth(GList *This){
264 | if(!This) return 0;
265 | return getGListDepth(This->root);
266 | }
267 |
268 | static GLNode *copyGListOfGLNode(GLNode *n){
269 | if(!n) return NULL;
270 | GLNode *p = n;
271 | GLNode *copyn = NULL;
272 | GLNode *copyroot = NULL;
273 | GLNode *temp = NULL;
274 | while(p){
275 | temp = (GLNode*)malloc(sizeof(GLNode));
276 | if(!temp) break;
277 | temp->tag = p->tag;
278 | temp->prior = copyn;
279 | if(!copyn){
280 | copyroot = temp;
281 | }else{
282 | copyn->next = temp;
283 | }
284 | temp->next = NULL;
285 | copyn = temp;
286 | if(p->tag == ATOM){
287 | if(p->content.elem){
288 | temp->content.elem = copyMyString(p->content.elem);
289 | }else{
290 | temp->content.elem = NULL;
291 | }
292 | }else{
293 | if(p->content.hp){
294 | temp->content.hp = copyGListOfGLNode(p->content.hp);
295 | }else{
296 | temp->content.hp = NULL;
297 | }
298 | }
299 | p = p->next;
300 | }
301 | return copyroot;
302 | }
303 |
304 | GList *copyGList(GList *L){
305 | if(!L) return NULL;
306 | GLNode *p = NULL;
307 | GList *temp = (GList *)malloc(sizeof(GList));
308 | if(!temp) return NULL;
309 | temp->root = copyGListOfGLNode(L->root);
310 | p = temp->root;
311 | while(p->next){
312 | p = p->next;
313 | }
314 | temp->tear = p;
315 | temp->isEmpty = isEmpty;
316 | temp->length = length;
317 | temp->print = print;
318 | temp->getDepth = getDepth;
319 | temp->getHead = getHead;
320 | temp->getTear = getTear;
321 | temp->getGLNode = getGLNode;
322 | temp->insertGLNode = insertGLNode;
323 | temp->insertGLNodeFront = insertGLNodeFront;
324 | temp->deleteGLNodeFront = deleteGLNodeFront;
325 | temp->insertGLNodeTear = insertGLNodeTear;
326 | temp->deleteGLNodeTear = deleteGLNodeTear;
327 | temp->traversal = traversal;
328 | return temp;
329 | }
330 |
331 | static int getHead(GList *This,GLNode **n){
332 | if(!This) return -1;
333 | *n = This->root;
334 | return 0;
335 | }
336 |
337 | static int getTear(GList *This,GLNode **n){
338 | if(!This) return -1;
339 | *n = This->tear;
340 | return 0;
341 | }
342 |
343 | static int getGLNode(GList *This,int index,GLNode **n){
344 | if(!This){
345 | *n = NULL;
346 | return -1;
347 | }
348 | int i = 0;
349 | GLNode *p = This->root;
350 | while(p && i < index){
351 | p = p->next;
352 | i++;
353 | }
354 | if(!p || i > index){
355 | *n = NULL;
356 | return -1;
357 | }
358 | *n = p;
359 | return 0;
360 | }
361 |
362 | static int insertGLNode(GList *This, int index, GLNode *n){
363 | if(!This) return -1;
364 | GLNode *newp = copyGLNode(n);
365 | GLNode *p = This->root;
366 | GLNode *temp = NULL;
367 | int j = 0;
368 | while(p && j < index){
369 | p = p->next;
370 | j++;
371 | }
372 | if(!p || j > index){
373 | destroyGLNode(temp);
374 | return -1;
375 | }
376 | if(p->prior){
377 | temp = p->prior;
378 | p->prior = newp;
379 | temp->next = newp;
380 | newp->prior = temp;
381 | newp->next = p;
382 | }else{
383 | newp->prior = NULL;
384 | newp->next = p;
385 | p->prior = newp;
386 | This->root = newp;
387 | }
388 | return 0;
389 | }
390 |
391 | static int insertGLNodeFront(GList *This, GLNode *n){
392 | if(!This) return -1;
393 | GLNode *temp = copyGLNode(n);
394 | if(temp){
395 | GLNode *p = This->root;
396 | This->root = temp;
397 | temp->prior = NULL;
398 | temp->next = p;
399 | p->prior = temp;
400 | }
401 | return 0;
402 | }
403 |
404 | static int deleteGLNodeFront(GList *This){
405 | if(!This || !This->root) return -1;
406 | GLNode *p = This->root;
407 | This->root = p->next;
408 | if(p->next){
409 | p->next->prior = NULL;
410 | }else{
411 | This->tear = NULL;
412 | }
413 | if(p->tag == ATOM){
414 | if(p->content.elem){
415 | destroyMyString(p->content.elem);
416 | }
417 | }else{
418 | destroyGLNode(p->content.hp);
419 | }
420 | free(p);
421 | return 0;
422 | }
423 |
424 | static int insertGLNodeTear(GList *This, GLNode *n){
425 | if(!This) return -1;
426 | GLNode *temp = copyGLNode(n);
427 | if(temp){
428 | This->tear->next = temp;
429 | temp->prior = This->tear;
430 | temp->next = NULL;
431 | This->tear = temp;
432 | }
433 | return 0;
434 | }
435 |
436 | static int deleteGLNodeTear(GList *This){
437 | if(!This || !This->tear) return -1;
438 | GLNode *p = This->tear->prior;
439 | if(This->tear->tag == ATOM){
440 | if(This->tear->content.elem){
441 | destroyMyString(This->tear->content.elem);
442 | }
443 | }else{
444 | destroyGLNode(This->tear->content.hp);
445 | }
446 | free(This->tear);
447 | if(p){
448 | p->next = NULL;
449 | This->tear = p;
450 | }else{
451 | This->tear = NULL;
452 | This->root = NULL;
453 | }
454 | return 0;
455 | }
456 |
457 | static void traversalGList(GLNode *n,int (*visit)(GLNodeElemType **elem)){
458 | if(!n) return;
459 | GLNode *p = n;
460 | while(p){
461 | if(p->tag == ATOM){
462 | if(p->content.elem){
463 | if(visit(&p->content.elem) != 0) break;
464 | }
465 | }else{
466 | traversalGList(p->content.hp,visit);
467 | }
468 | p = p->next;
469 | }
470 | }
471 |
472 | static void traversal(GList *This,int (*visit)(GLNodeElemType **elem)){
473 | if(!This) return;
474 | traversalGList(This->root,visit);
475 | }
476 |
477 | //*************************************************************************************************//
478 | //单独的GLNode可用函数
479 |
480 | void printGLNode(GLNode *n){
481 | if(n){
482 | if(n->tag == ATOM){
483 | if(n->content.elem){
484 | printf("%s\n",n->content.elem->str);
485 | }
486 | }else{
487 | printf("[");
488 | printGList(n->content.hp);
489 | printf("]\n");
490 | }
491 | }
492 | }
493 |
494 | int getGLNodeDepth(GLNode *n){
495 | if(!n) return 1; // 空表深度为1
496 | if(n->tag == ATOM){
497 | return 0; //原子深度为0
498 | }else{
499 | return getGListDepth(n->content.hp);
500 | }
501 | }
502 |
503 | void destroyGLNode(GLNode *n){
504 | GLNode *p = n;
505 | GLNode *temp = NULL;
506 | while(p){
507 | temp = p;
508 | p = p->next;
509 | if(temp->tag == ATOM){
510 | if(temp->content.elem){
511 | destroyMyString(temp->content.elem);
512 | }
513 | }else{
514 | destroyGLNode(temp->content.hp);
515 | }
516 | free(temp);
517 | }
518 | }
519 |
520 | void traversalGLNode(GLNode *n,int (*visit)(GLNodeElemType **elem)){
521 | if(!n) return;
522 | if(n){
523 | if(n->tag == ATOM){
524 | if(n->content.elem){
525 | visit(&n->content.elem);
526 | }
527 | }else{
528 | traversalGList(n->content.hp,visit);
529 | }
530 | }
531 | }
532 |
533 | GLNode *copyGLNode(GLNode *n){
534 | if(!n) return NULL;
535 | GLNode *temp = (GLNode*)malloc(sizeof(GLNode));
536 | if(!temp) return NULL;
537 | temp->tag = n->tag;
538 | temp->prior = NULL;
539 | temp->next = NULL;
540 | if(n->tag == ATOM){
541 | if(n->content.elem){
542 | temp->content.elem = copyMyString(n->content.elem);
543 | }else{
544 | temp->content.elem = NULL;
545 | }
546 | }else{
547 | if(n->content.hp){
548 | temp->content.hp = copyGListOfGLNode(n->content.hp);
549 | }
550 | }
551 | return temp;
552 | }
--------------------------------------------------------------------------------
/g_list/g_list.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _GLIST_H
3 | #define _GLIST_H
4 |
5 | #include "MyString.h"
6 |
7 | typedef MyString GLNodeElemType;
8 |
9 | typedef enum GLNodeElemTag{
10 | ATOM, //原子结点
11 | LIST //表结点
12 | }GLNodeElemTag;
13 |
14 | typedef struct GLNode{
15 | GLNodeElemTag tag; //tag用于区分原子结点和表结点
16 | union content{ //原子结点和表结点的联合
17 | GLNodeElemType *elem; //原子结点的指针
18 | struct GLNode *hp; //表结点的表头指针
19 | }content;
20 | struct GLNode *prior; //指向前一个元素的结点
21 | struct GLNode *next; //指向下一个元素的结点
22 | }GLNode;
23 |
24 | typedef struct GList{
25 | GLNode *root;
26 | GLNode *tear;
27 | int (*isEmpty)(struct GList *This);
28 | int (*length)(struct GList *This);
29 | void (*print)(struct GList *This);
30 | int (*getDepth)(struct GList *This);
31 | int (*getHead)(struct GList *This,GLNode **n);
32 | int (*getTear)(struct GList *This,GLNode **n);
33 | int (*getGLNode)(struct GList *This,int index,GLNode **n);
34 | int (*insertGLNode)(struct GList *This, int index, GLNode *n);
35 | int (*insertGLNodeFront)(struct GList *This, GLNode *n);
36 | int (*deleteGLNodeFront)(struct GList *This);
37 | int (*insertGLNodeTear)(struct GList *This, GLNode *n);
38 | int (*deleteGLNodeTear)(struct GList *This);
39 | void (*traversal)(struct GList *This,int (*visit)(GLNodeElemType **elem));
40 | }GList;
41 |
42 | GList *initGListFromString(char *listStr);
43 | GList *initGListFromGLNode(GLNode *n);
44 | GList *copyGList(GList *L);
45 | void DestroyGList(GList *L);
46 |
47 | void printGLNode(GLNode *n);
48 | int getGLNodeDepth(GLNode *n);
49 | GLNode *copyGLNode(GLNode *n);
50 | void traversalGLNode(GLNode *n,int (*visit)(GLNodeElemType **elem));
51 | void destroyGLNode(GLNode *n);
52 |
53 |
54 | #endif
--------------------------------------------------------------------------------
/g_list/my_string.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "MyString.h"
4 |
5 | MyString *myStringAssign(char *str){
6 | int i,str_length = 0;
7 | MyString *S = (MyString *)malloc(sizeof(MyString));
8 | if(!S) return NULL;
9 | while(*(str+str_length) != '\0'){
10 | str_length++;
11 | }
12 | str_length++;
13 | S->str = (char *)malloc(str_length*sizeof(char));
14 | S->size = str_length;
15 | if(!S->str){
16 | free(S);
17 | return NULL;
18 | }
19 | S->length = str_length - 1;
20 | for(i=0;istr + i) = *(str + i);
22 | }
23 | return S;
24 | }
25 |
26 | int myStringLength(MyString *S){
27 | if(S){
28 | return S->length;
29 | }else{
30 | return 0;
31 | }
32 | }
33 |
34 | int myStringRemainSpace(MyString *S){
35 | if(S){
36 | return S->size - S->length -1;
37 | }else{
38 | return 0;
39 | }
40 | }
41 |
42 | int isMyStringEmpty(MyString *S){
43 | if(!S) return 0;
44 | if(S->length){
45 | return 0;
46 | }else{
47 | return 1;
48 | }
49 | }
50 |
51 | int clearMyString(MyString *S){
52 | if(S){
53 | if(S->str){
54 | *S->str = '\0';
55 | }
56 | S->length = 0;
57 | }
58 | }
59 |
60 | void destroyMyString(MyString *S){
61 | if(S){
62 | if(S->str){
63 | free(S->str);
64 | }
65 | free(S);
66 | S = NULL;
67 | }
68 | }
69 |
70 | int printMyString(MyString *S){
71 | if(S){
72 | if(S->str){
73 | printf("%s, ",S->str);
74 | }
75 | printf("length :%d\n",S->length);
76 | }
77 | return 0;
78 | }
79 |
80 | int compareMyString(MyString *S1,MyString *S2){
81 | int i;
82 | int result = 0;
83 | if(!S1 || !S2) return result;
84 | for(i=0;ilength && ilength;i++){
85 | if(*(S1->str+i) != *(S2->str+i)){
86 | result = *(S1->str+i) - *(S2->str+i);
87 | break;
88 | }
89 | }
90 | if(result == 0){
91 | result = S1->length - S2->length;
92 | }
93 | return result;
94 | }
95 |
96 | MyString *copyMyString(MyString *S){
97 | int i;
98 | if(!S || !S->str) return NULL;
99 | MyString *temp = (MyString *)malloc(sizeof(MyString));
100 | if(!temp) return NULL;
101 | temp->size = S->length + 1;
102 | temp->str = (char *)malloc(temp->size*sizeof(char));
103 | if(!temp->str){
104 | free(temp);
105 | return NULL;
106 | }
107 | temp->length = S->length;
108 | for(i=0;ilength+1;i++){
109 | *(temp->str + i) = *(S->str + i);
110 | }
111 | return temp;
112 | }
113 |
114 | int myStringIndexChar(MyString *S,char indexElem,int pos){
115 | int index = -1;
116 | int i;
117 | if(!S) return -1;
118 | for(i=pos;ilength;i++){
119 | if(*(S->str + i) == indexElem){
120 | index = i;
121 | break;
122 | }
123 | }
124 | return index;
125 | }
126 |
127 | int insertMyString(MyString *S1,MyString *S2,int pos){
128 | int i;
129 | if(!S1 || !S1->str || !S2 || !S2->str) return -1;
130 | if(pos < 0 || pos > S1->length) return -1;
131 | if(myStringRemainSpace(S1) < S2->length){
132 | S1->size += S2->length - myStringRemainSpace(S1);
133 | S1->str = (char *)realloc(S1->str,S1->size*sizeof(char));
134 | if(!S1->str) return -1;
135 | }
136 | for(i=S1->length;i>=pos;i--){
137 | *(S1->str + S2->length + i) = *(S1->str + i);
138 | }
139 | for(i=0;ilength;i++){
140 | *(S1->str + pos + i) = *(S2->str + i);
141 | }
142 | S1->length += S2->length;
143 | return 0;
144 | }
145 |
146 | MyString *substrMyString(MyString *S,int start,int end){
147 | int i,length;
148 | if(start < 0 || start >= S->length || end <= 0 || end > S->length || end <= start) return NULL;
149 | MyString *temp = (MyString *)malloc(sizeof(MyString));
150 | if(!temp) return NULL;
151 | length = end - start;
152 | temp->size = length+1;
153 | temp->str = (char *)malloc(temp->size*sizeof(char));
154 | if(!temp->str){
155 | free(temp);
156 | return NULL;
157 | }
158 | for(i=0;istr + i) = *(S->str + start + i);
160 | }
161 | *(temp->str + length) = '\0';
162 | temp->length = length;
163 | return temp;
164 | }
165 |
166 | int deleteMyString(MyString *S,int start,int end){
167 | int i,length;
168 | if(start < 0 || start >= S->length || end <= 0 || end > S->length || end <= start) return -1;
169 | length = S->length - end + 1;
170 | for(i=0;i<=length;i++){
171 | *(S->str + start + i) = *(S->str + end + i);
172 | }
173 | S->length -= (end - start);
174 | return 0;
175 | }
176 |
177 | int concatMyString(MyString *S1,MyString *S2){
178 | int i;
179 | if(!S2->str || !S1) return -1;
180 | if(myStringRemainSpace(S1) < S2->length){
181 | S1->size += (S2->length - myStringRemainSpace(S1));
182 | S1->str = (char *)realloc(S1->str,S1->size*sizeof(char));
183 | if(!S1->str) return -1;
184 | }
185 | for(i=0;i<=S2->length;i++){
186 | *(S1->str + S1->length + i) = *(S2->str + i);
187 | }
188 | S1->length += S2->length;
189 | return 0;
190 | }
191 |
192 | int replaceMyString(MyString *S1,MyString *S2,int start,int end){
193 | if(start < 0 || start >= S1->length || end <= 0 || end > S1->length || end < start) return -1;
194 | if(end>start){
195 | if(deleteMyString(S1,start,end) == -1){
196 | return -1;
197 | }
198 | }
199 | if(insertMyString(S1,S2,start) == -1){
200 | return -1;
201 | }else{
202 | return 0;
203 | }
204 | }
205 |
206 | MyStringArray *splitMyString(MyString *S,char splitElem){
207 | int start = 0,end = 0,index = 0;
208 | MyStringArray *strarray = NULL;
209 | MyString *strtemp = NULL;
210 | index = myStringIndexChar(S,splitElem,0);
211 | if(index == -1) return NULL;
212 | strarray = InitMyStringArray();
213 | end = index;
214 | if(end != start){
215 | strtemp = substrMyString(S,start,end);
216 | strarray->tearPush(strarray,strtemp);
217 | destroyMyString(strtemp);
218 | if(end == S->length){
219 | return strarray;
220 | }
221 | }
222 | index++;
223 | start = index;
224 | while(index > 0){
225 | index = myStringIndexChar(S,splitElem,index);
226 | if(index != -1){
227 | end = index;
228 | strtemp = substrMyString(S,start,end);
229 | strarray->tearPush(strarray,strtemp);
230 | destroyMyString(strtemp);
231 | if(end == S->length){
232 | break;
233 | }
234 | index++;
235 | start = index;
236 | }
237 | }
238 | if(end != S->length){
239 | end = S->length;
240 | strtemp = substrMyString(S,start,end);
241 | strarray->tearPush(strarray,strtemp);
242 | destroyMyString(strtemp);
243 | }
244 | return strarray;
245 | }
246 |
247 | static int *getKMPNext(MyString *substr){
248 | int *nextval = (int *)malloc((substr->length)*sizeof(int));
249 | int j=0,k=-1;
250 | if(nextval){
251 | *nextval = -1;
252 | while(jlength){
253 | if(k == -1 || *(substr->str+j) == *(substr->str+k)){
254 | if(*(substr->str+(++j)) == *(substr->str+(++k))){ //两个字符相等时跳过
255 | *(nextval+j) = *(nextval+k);
256 | }else{
257 | *(nextval+j) = k;
258 | }
259 | }else{
260 | k = *(nextval+k);
261 | }
262 | }
263 | return nextval;
264 | }else{
265 | return NULL;
266 | }
267 | }
268 |
269 | int myStringIndexSubString(MyString *S,MyString *substr,int pos){ //KMP算法
270 | int i = pos,j = 0;
271 | if(!S || !substr || pos > S->length) return -1;
272 | int *nextval = getKMPNext(substr);
273 | if(!nextval) return -1;
274 | while(i < S->length && j < substr->length){
275 | if(*(S->str + i) == *(substr->str + j)){
276 | i++;
277 | j++;
278 | }else{
279 | j = *(nextval+j); //i不需要回溯,j回到指定位置
280 | if(j == -1){
281 | i++; //当j为-1时,要移动的是i
282 | j++; //j归0
283 | }
284 | }
285 | }
286 | free(nextval);
287 | if(j == substr->length){
288 | return i - j;
289 | }else{
290 | return -1;
291 | }
292 | }
293 |
294 | static int parenthesesMatching(MyString *S){
295 | if(!S) return 0;
296 | int bracket_count = 0,str_index = 0;
297 | int bracket_contained = 0;
298 | if(*(S->str+str_index) != '(') return 0;
299 | while(*(S->str+str_index) != '\0'){
300 | if(*(S->str+str_index) == '('){
301 | bracket_contained = 1;
302 | bracket_count++;
303 | }else if(*(S->str+str_index) == ')'){
304 | if(bracket_count){
305 | bracket_count --;
306 | }else{
307 | bracket_contained = 0;
308 | break;
309 | }
310 | }
311 | str_index++;
312 | }
313 | if(bracket_contained){
314 | if(bracket_count == 0) return 1;
315 | }
316 | return 0;
317 | }
318 |
319 | static int squareBracketMatching(MyString *S){
320 | if(!S) return 0;
321 | int bracket_count = 0,str_index = 0;
322 | int bracket_contained = 0;
323 | if(*(S->str+str_index) != '[') return 0;
324 | while(*(S->str+str_index) != '\0'){
325 | if(*(S->str+str_index) == '['){
326 | bracket_contained = 1;
327 | bracket_count++;
328 | }else if(*(S->str+str_index) == ']'){
329 | if(bracket_count){
330 | bracket_count --;
331 | }else{
332 | bracket_contained = 0;
333 | break;
334 | }
335 | }
336 | str_index++;
337 | }
338 | if(bracket_contained){
339 | if(bracket_count == 0) return 1;
340 | }
341 | return 0;
342 | }
343 |
344 | static int braceMatching(MyString *S){
345 | if(!S) return 0;
346 | int bracket_count = 0,str_index = 0;
347 | int bracket_contained = 0;
348 | if(*(S->str+str_index) != '{') return 0;
349 | while(*(S->str+str_index) != '\0'){
350 | if(*(S->str+str_index) == '{'){
351 | bracket_contained = 1;
352 | bracket_count++;
353 | }else if(*(S->str+str_index) == '}'){
354 | if(bracket_count){
355 | bracket_count --;
356 | }else{
357 | bracket_contained = 0;
358 | break;
359 | }
360 | }
361 | str_index++;
362 | }
363 | if(bracket_contained){
364 | if(bracket_count == 0) return 1;
365 | }
366 | return 0;
367 | }
368 |
369 | int isBracketMatching(MyString *S,char leftBracketChar){
370 | int isMatching = 0;
371 | switch(leftBracketChar){
372 | case '(':
373 | isMatching = parenthesesMatching(S);
374 | break;
375 | case '[':
376 | isMatching = squareBracketMatching(S);
377 | break;
378 | case '{':
379 | isMatching = braceMatching(S);
380 | break;
381 | }
382 | return isMatching;
383 | }
384 |
385 | static int getMatchingParenthesisIndex(MyString *S,int leftbracketIndex){
386 | if(!S) return -1;
387 | int bracket_count = 0,str_index = leftbracketIndex;
388 | int bracket_contained = 0;
389 | if(*(S->str+str_index) != '(') return -1;
390 | while(*(S->str+str_index) != '\0'){
391 | if(*(S->str+str_index) == '('){
392 | bracket_contained = 1;
393 | bracket_count++;
394 | }else if(*(S->str+str_index) == ')'){
395 | bracket_count --;
396 | if(bracket_count == 0) break;
397 | }
398 | str_index++;
399 | }
400 | if(str_index > S->length) return -1;
401 | return str_index;
402 | }
403 |
404 | static int getMatchingSquareBracketIndex(MyString *S,int leftbracketIndex){
405 | if(!S) return -1;
406 | int bracket_count = 0,str_index = leftbracketIndex;
407 | int bracket_contained = 0;
408 | if(*(S->str+str_index) != '[') return -1;
409 | while(*(S->str+str_index) != '\0'){
410 | if(*(S->str+str_index) == '['){
411 | bracket_contained = 1;
412 | bracket_count++;
413 | }else if(*(S->str+str_index) == ']'){
414 | bracket_count --;
415 | if(bracket_count == 0) break;
416 | }
417 | str_index++;
418 | }
419 | if(str_index > S->length) return -1;
420 | return str_index;
421 | }
422 |
423 | static int getMatchingBraceIndex(MyString *S,int leftbracketIndex){
424 | if(!S) return -1;
425 | int bracket_count = 0,str_index = leftbracketIndex;
426 | int bracket_contained = 0;
427 | if(*(S->str+str_index) != '{') return -1;
428 | while(*(S->str+str_index) != '\0'){
429 | if(*(S->str+str_index) == '{'){
430 | bracket_contained = 1;
431 | bracket_count++;
432 | }else if(*(S->str+str_index) == '}'){
433 | bracket_count --;
434 | if(bracket_count == 0) break;
435 | }
436 | str_index++;
437 | }
438 | if(str_index > S->length) return -1;
439 | return str_index;
440 | }
441 |
442 | int getMatchingBracketIndex(MyString *S,char leftBracketChar,int leftbracketIndex){
443 | int index = 0;
444 | switch(leftBracketChar){
445 | case '(':
446 | index = getMatchingParenthesisIndex(S,leftbracketIndex);
447 | break;
448 | case '[':
449 | index = getMatchingSquareBracketIndex(S,leftbracketIndex);
450 | break;
451 | case '{':
452 | index = getMatchingBraceIndex(S,leftbracketIndex);
453 | break;
454 | }
455 | return index;
456 | }
--------------------------------------------------------------------------------
/g_list/my_string.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _MYSTRING_H
3 | #define _MYSTRING_H
4 | /* Exported types ------------------------------------------------------------*/
5 |
6 | typedef struct MyString{
7 | char *str;
8 | int length; //字符串长度
9 | int size; //空间大小
10 | }MyString;
11 |
12 | typedef struct MyStringArray_P
13 | {
14 | MyString *mystring;
15 | struct MyStringArray_P *next;
16 | }MyStringArray_P;
17 |
18 | typedef struct MyStringArray{
19 | MyStringArray_P *This;
20 | MyStringArray_P *front;
21 | MyStringArray_P *tear;
22 | void (*clear)(struct MyStringArray *This);
23 | int (*isEmpty)(struct MyStringArray *This);
24 | int (*length)(struct MyStringArray *This);
25 | int (*index)(struct MyStringArray *This, MyString *e);
26 | int (*get)(struct MyStringArray *This, int index, MyString **e);
27 | int (*getFront)(struct MyStringArray *This, MyString **e);
28 | int (*getTear)(struct MyStringArray *This, MyString **e);
29 | int (*modify)(struct MyStringArray *This, int index, MyString *e);
30 | int (*insert)(struct MyStringArray *This, int index, MyString *e);
31 | int (*delete)(struct MyStringArray *This, int index, MyString **e);
32 | int (*tearPush)(struct MyStringArray *This, MyString *e);
33 | int (*tearPop)(struct MyStringArray *This, MyString **e);
34 | int (*frontPush)(struct MyStringArray *This, MyString *e);
35 | int (*frontPop)(struct MyStringArray *This, MyString **e);
36 | int (*traverse)(struct MyStringArray *This,int (*visit)(MyString **e));
37 | }MyStringArray;
38 |
39 | /* Includes ------------------------------------------------------------------*/
40 | #include "MyStringArray.h"
41 |
42 |
43 | MyString *myStringAssign(char *str);
44 | int myStringLength(MyString *S);
45 | int isMyStringEmpty(MyString *S);
46 | int clearMyString(MyString *S);
47 | void destroyMyString(MyString *S);
48 | int printMyString(MyString *str);
49 | int compareMyString(MyString *S1,MyString *S2);
50 | MyString *copyMyString(MyString *S);
51 | int myStringIndexChar(MyString *S,char indexElem,int pos);
52 | int insertMyString(MyString *S1,MyString *S2,int pos);
53 | int deleteMyString(MyString *S,int start,int end);
54 | int concatMyString(MyString *S1,MyString *S2);
55 | int replaceMyString(MyString *S1,MyString *S2,int start,int end);
56 | MyString *substrMyString(MyString *S,int start,int end);
57 | MyStringArray *splitMyString(MyString *S,char splitElem);
58 | int myStringIndexSubString(MyString *S,MyString *substr,int pos);
59 | int isBracketMatching(MyString *S,char leftBracketChar);
60 | int getMatchingBracketIndex(MyString *S,char leftBracketChar,int leftbracketIndex);
61 | #endif
62 |
--------------------------------------------------------------------------------
/g_list/my_string_array.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "MyStringArray.h"
4 |
5 | static void clear(MyStringArray *This);
6 | static int isEmpty(MyStringArray *This);
7 | static int length(MyStringArray *This);
8 | static int index(MyStringArray *This, MyString *e);
9 | static int get(MyStringArray *This, int index, MyString **e);
10 | static int getFront(MyStringArray *This, MyString **e);
11 | static int getTear(MyStringArray *This, MyString **e);
12 | static int modify(MyStringArray *This, int index, MyString *e);
13 | static int insert(MyStringArray *This, int index, MyString *e);
14 | static int delete(MyStringArray *This, int index, MyString **e);
15 | static int tearPush(MyStringArray *This, MyString *e);
16 | static int tearPop(MyStringArray *This, MyString **e);
17 | static int frontPush(MyStringArray *This, MyString *e);
18 | static int frontPop(MyStringArray *This, MyString **e);
19 | static int traverse(MyStringArray *This,int (*visit)(MyString **e));
20 |
21 | MyStringArray *InitMyStringArray(){
22 | MyStringArray *strArray = (MyStringArray *)malloc(sizeof(MyStringArray));
23 | MyStringArray_P *p = (MyStringArray_P *)malloc(sizeof(MyStringArray_P));
24 | strArray->This = p;
25 | p->next = NULL;
26 | strArray->front = p;
27 | strArray->tear = strArray->front;
28 | strArray->clear = clear;
29 | strArray->isEmpty = isEmpty;
30 | strArray->length = length;
31 | strArray->index = index;
32 | strArray->get = get;
33 | strArray->getFront = getFront;
34 | strArray->getTear = getTear;
35 | strArray->modify = modify;
36 | strArray->insert = insert;
37 | strArray->delete = delete;
38 | strArray->tearPush = tearPush;
39 | strArray->tearPop = tearPop;
40 | strArray->frontPush = frontPush;
41 | strArray->frontPop = frontPop;
42 | strArray->traverse = traverse;
43 | return strArray;
44 | }
45 |
46 | void DestroyMyStringArray(MyStringArray *strArray){
47 | strArray->clear(strArray);
48 | free(strArray->This);
49 | strArray->This = NULL;
50 | free(strArray);
51 | strArray = NULL;
52 | }
53 |
54 | static void clear(MyStringArray *This){
55 | MyStringArray_P *p = This->This->next;
56 | MyStringArray_P *temp = NULL;
57 | while(p){
58 | temp = p;
59 | p = p->next;
60 | free(temp->mystring->str);
61 | temp->mystring->str = NULL;
62 | free(temp);
63 | temp = NULL;
64 | }
65 | p = This->This;
66 | p->next = NULL;
67 | This->front = p;
68 | This->tear = This->front;
69 | }
70 |
71 | static int isEmpty(MyStringArray *This){
72 | MyStringArray_P *p = This->This;
73 | if(p->next){
74 | return 0;
75 | }else{
76 | return 1;
77 | }
78 | }
79 |
80 | static int length(MyStringArray *This){
81 | int j = 0;
82 | MyStringArray_P *p = This->This->next;
83 | while(p){
84 | j++;
85 | p = p->next;
86 | }
87 | return j;
88 | }
89 |
90 | static int index(MyStringArray *This, MyString *e){
91 | MyStringArray_P *p = This->This->next;
92 | int pos = -1;
93 | int j = 0;
94 | if(!e) return -1;
95 | while(p){
96 | if(compareMyString(p->mystring,e) == 0){
97 | pos = j;
98 | }
99 | p = p->next;
100 | j++;
101 | }
102 | return pos;
103 | }
104 |
105 | static int get(MyStringArray *This, int index, MyString **e){
106 | MyStringArray_P *p = This->This->next;
107 | int j = 0;
108 | while(p && j < index){
109 | p = p->next;
110 | j++;
111 | }
112 | if(!p || j > index) return -1;
113 | *e = copyMyString(p->mystring);
114 | return 0;
115 | }
116 |
117 | static int getFront(MyStringArray *This, MyString **e){
118 | MyStringArray_P *p = This->front->next;
119 | *e = copyMyString(p->mystring);
120 | return 0;
121 | }
122 |
123 | static int getTear(MyStringArray *This, MyString **e){
124 | MyStringArray_P *p = This->tear;
125 | *e = copyMyString(p->mystring);
126 | return 0;
127 | }
128 |
129 | static int modify(MyStringArray *This, int index, MyString *e){
130 | MyStringArray_P *p = This->This->next;
131 | if(!e) return -1;
132 | int j = 0;
133 | while(p && j < index){
134 | p = p->next;
135 | j++;
136 | }
137 | if(!p || j > index) return -1;
138 | free(p->mystring);
139 | p->mystring = copyMyString(e);
140 | if(p->mystring){
141 | return 0;
142 | }else{
143 | return -1;
144 | }
145 | }
146 |
147 | static int insert(MyStringArray *This, int index, MyString *e){
148 | MyStringArray_P *p = This->This;
149 | int j = 0;
150 | if(!e) return -1;
151 | MyStringArray_P *temp = (MyStringArray_P *)malloc(sizeof(MyStringArray_P));
152 | if(!temp) return -1;
153 | while(p && j < index){
154 | p = p->next;
155 | j++;
156 | }
157 | if(!p || j > index) return -1;
158 | temp->next = p->next;
159 | p->next = temp;
160 | temp->mystring = copyMyString(e);
161 | if(!temp->mystring){
162 | free(temp);
163 | return -1;
164 | }else{
165 | return 0;
166 | }
167 | }
168 |
169 | static int delete(MyStringArray *This, int index, MyString **e){
170 | MyStringArray_P *p = This->This;
171 | MyStringArray_P *temp = NULL;
172 | int j = 0;
173 | while(p->next && j < index){
174 | p = p->next;
175 | j++;
176 | }
177 | if(!p->next || j > index) return -1;
178 | temp = p->next;
179 | p->next = temp->next;
180 | *e = copyMyString(temp->mystring);
181 | free(temp);
182 | return 0;
183 | }
184 |
185 | static int tearPush(MyStringArray *This, MyString *e){
186 | MyStringArray_P *p = This->This;
187 | if(!e) return -1;
188 | MyStringArray_P *temp = (MyStringArray_P *)malloc(sizeof(MyStringArray_P));
189 | if(!temp) return -1;
190 | temp->mystring = copyMyString(e);
191 | if(temp->mystring){
192 | if(This->front == This->tear){
193 | p->next = temp;
194 | }else{
195 | This->tear->next = temp;
196 | }
197 | temp->next = NULL;
198 | This->tear = temp;
199 | return 0;
200 | }else{
201 | free(temp);
202 | return -1;
203 | }
204 | }
205 |
206 | static int tearPop(MyStringArray *This, MyString **e){
207 | MyStringArray_P *p = This->This;
208 | MyStringArray_P *temp = NULL;
209 | while(p->next->next){
210 | p = p->next;
211 | }
212 | temp = p->next;
213 | This->tear = p;
214 | if(!temp) return -1;
215 | *e = copyMyString(temp->mystring);
216 | free(temp);
217 | p->next = NULL;
218 | return 0;
219 | }
220 |
221 | static int frontPush(MyStringArray *This, MyString *e){
222 | MyStringArray_P *p = This->This;
223 | if(!e) return -1;
224 | MyStringArray_P *temp = (MyStringArray_P *)malloc(sizeof(MyStringArray_P));
225 | if(!temp) return -1;
226 | temp->mystring = copyMyString(e);
227 | if(temp->mystring){
228 | temp->next = p->next;
229 | p->next = temp;
230 | if(This->front == This->tear){
231 | This->tear = temp;
232 | }
233 | return 0;
234 | }else{
235 | free(temp);
236 | return -1;
237 | }
238 | }
239 |
240 | static int frontPop(MyStringArray *This, MyString **e){
241 | if(This->front == This->tear){
242 | e = NULL;
243 | return -1;
244 | }
245 | MyStringArray_P *p = This->front->next;
246 | *e = copyMyString(p->mystring);
247 | This->front->next = p->next;
248 | if(This->tear == p) This->tear = This->front;
249 | free(p);
250 | return 0;
251 | }
252 |
253 | static int traverse(MyStringArray *This,int (*visit)(MyString **e)){
254 | if(This->front == This->tear){
255 | return -1;
256 | }
257 | MyStringArray_P *temp = This->front->next;
258 | while(temp){
259 | if(visit(&(temp->mystring)) != 0) break;
260 | temp = temp->next;
261 | }
262 | return 0;
263 | }
264 |
265 |
--------------------------------------------------------------------------------
/g_list/my_string_array.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _MYSTRINGARRAY_H
3 | #define _MYSTRINGARRAY_H
4 | /* Includes ------------------------------------------------------------------*/
5 | #include "MyString.h"
6 | /* Exported types ------------------------------------------------------------*/
7 | /* Exported macro ------------------------------------------------------------*/
8 | MyStringArray *InitMyStringArray();
9 | void DestroyMyStringArray(MyStringArray *strArray);
10 |
11 | #endif
--------------------------------------------------------------------------------
/g_list/test_g_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Glist.h"
4 |
5 | int visitGLNode(GLNodeElemType **elem){
6 | printMyString(*elem);
7 | return 0;
8 | }
9 |
10 | int main(void){
11 | GLNode *node1 = NULL;
12 | GLNode *node2 = NULL;
13 | GList *L = initGListFromString("[1D,2AD,[3SFDS,[4FEF,[5GRG,[6HJJY,[]]],7TKU],8GRGRH],9YJRDFB]");
14 | GList *L2 = NULL;
15 | GList *L3 = NULL;
16 |
17 | printf("list L:\n");
18 | L->print(L);
19 | printf("is list L empty:%d\n",L->isEmpty(L));
20 | printf("list L length:%d\n",L->length(L));
21 | printf("list L depth:%d\n",L->getDepth(L));
22 |
23 | printf("the index 3 of list L is : ");
24 | L->getGLNode(L,3,&node1);
25 | printGLNode(node1);
26 | printf("\n");
27 |
28 | printf("get glnode of index 2 of L:\n");
29 | L->getGLNode(L,2,&node1);
30 | printGLNode(node1);
31 | printf("\n");
32 |
33 | L2 = initGListFromGLNode(node1);
34 | printf("list L2:\n");
35 | L2->print(L2);
36 | printf("is list L2 empty:%d\n",L2->isEmpty(L2));
37 | printf("list L2 length:%d\n",L2->length(L2));
38 | printf("list L2 depth:%d\n",L2->getDepth(L2));
39 | printf("the index 0 of list L2 is : ");
40 | L2->getGLNode(L2,0,&node2);
41 | printGLNode(node2);
42 | printf("\n");
43 |
44 | L2->insertGLNodeFront(L2,node1);
45 | printf("insertGLNodeFront list L2:\n");
46 | L2->print(L2);
47 | printf("\n");
48 |
49 | L2->insertGLNodeTear(L2,node1);
50 | printf("insertGLNodeTear list L2:\n");
51 | L2->print(L2);
52 | printf("\n");
53 |
54 | printf("get glnode of index 0 of L2:\n");
55 | L2->getGLNode(L2,0,&node2);
56 | printGLNode(node2);
57 | printf("\n");
58 |
59 | printf("get glnode of index 1 of L2:\n");
60 | L2->getGLNode(L2,1,&node2);
61 | printGLNode(node2);
62 | printf("\n");
63 |
64 | printf("get head of L2:\n");
65 | L2->getHead(L2,&node2);
66 | printGLNode(node2);
67 | printf("\n");
68 |
69 | printf("get tear of L2:\n");
70 | L2->getTear(L2,&node2);
71 | printGLNode(node2);
72 | printf("\n");
73 |
74 | L2->deleteGLNodeTear(L2);
75 | printf("list L2:\n");
76 | L2->print(L2);
77 | printf("get tear of L2:\n");
78 | L2->getTear(L2,&node2);
79 | printGLNode(node2);
80 | printf("\n");
81 |
82 | L2->deleteGLNodeFront(L2);
83 | L2->deleteGLNodeFront(L2);
84 | printf("list L2:\n");
85 | L2->print(L2);
86 | printf("get head of L2:\n");
87 | L2->getHead(L2,&node2);
88 | printGLNode(node2);
89 | printf("\n");
90 |
91 | L3 = copyGList(L2);
92 | printf("list L3:\n");
93 | L3->print(L3);
94 |
95 | L3->insertGLNodeTear(L3,node1);
96 | printf("insertGLNodeTear list L3:\n");
97 | L3->print(L3);
98 | printf("\n");
99 |
100 | L3->insertGLNodeFront(L3,node2);
101 | printf("insertGLNodeFront list L3:\n");
102 | L3->print(L3);
103 | printf("\n");
104 |
105 | while(L3->length(L3)){
106 | printf("get tear of L3:\n");
107 | L3->getTear(L3,&node2);
108 | printGLNode(node2);
109 | printf("\n");
110 |
111 | L3->deleteGLNodeTear(L3);
112 | printf("list L3:\n");
113 | L3->print(L3);
114 | printf("\n");
115 | }
116 |
117 | printf("is list L3 empty:%d\n",L3->isEmpty(L3));
118 | printf("\n");
119 |
120 | printf("list L2:\n");
121 | L2->print(L2);
122 | printf("\n");
123 |
124 | while(L2->length(L2)){
125 | printf("get head of L2:\n");
126 | L2->getHead(L2,&node2);
127 | printGLNode(node2);
128 | printf("\n");
129 |
130 | L2->deleteGLNodeFront(L2);
131 | printf("list L2:\n");
132 | L2->print(L2);
133 | printf("\n");
134 | }
135 |
136 | printf("is list L2 empty:%d\n",L2->isEmpty(L2));
137 | printf("\n");
138 |
139 | printf("node1 traversal\n");
140 | traversalGLNode(node1,visitGLNode);
141 | printf("\n");
142 |
143 | printf("list L traversal\n");
144 | L->traversal(L,visitGLNode);
145 | printf("\n");
146 |
147 | DestroyGList(L);
148 | DestroyGList(L2);
149 | DestroyGList(L3);
150 | return 0;
151 | }
--------------------------------------------------------------------------------
/linear_list/linear_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearList.h"
4 | //线性表
5 |
6 | static void clear(LinearList *This);
7 | static int isEmpty(LinearList *This);
8 | static int length(LinearList *This);
9 | static void print(LinearList *This);
10 | static int indexElem(LinearList *This, ElemType* x);
11 | static int priorElem(LinearList *This, ElemType* cur_elem, ElemType* pre_elem);
12 | static int getElem(LinearList *This, int index, ElemType* e);
13 | static int modifyElem(LinearList *This, int index, ElemType* e);
14 | static int nextElem(LinearList *This, ElemType* cur_elem, ElemType* next_elem);
15 | static int insertElem(LinearList *This, int index, ElemType* e);
16 | static int deleteElem(LinearList *This, int index, ElemType* e);
17 | static int appendElem(LinearList *This,ElemType* e);
18 | static int popElem(LinearList *This, ElemType* e);
19 |
20 | LinearList *InitLinearList(){
21 | LinearList *L = (LinearList *)malloc(sizeof(LinearList));
22 | LinearList_P *p = (LinearList_P *)malloc(sizeof(LinearList_P));
23 | p->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
24 | p->length = 0; //当前长度
25 | p->size = LIST_INIT_SIZE; //当前分配量
26 | L->This = p;
27 | L->clear = clear;
28 | L->isEmpty = isEmpty;
29 | L->length = length;
30 | L->print = print;
31 | L->indexElem = indexElem;
32 | L->priorElem = priorElem;
33 | L->getElem = getElem;
34 | L->modifyElem = modifyElem;
35 | L->nextElem = nextElem;
36 | L->insertElem = insertElem;
37 | L->deleteElem = deleteElem;
38 | L->appendElem = appendElem;
39 | L->popElem = popElem;
40 | return L;
41 | }
42 |
43 | void DestroyLinearList(LinearList *L){
44 | free(L->This);
45 | free(L);
46 | L = NULL;
47 | }
48 |
49 | static void clear(LinearList *This){
50 | LinearList_P *p = This->This;
51 | p->length = 0; //当前长度
52 | }
53 |
54 | static int isEmpty(LinearList *This){
55 | LinearList_P *p = This->This;
56 | return (p->length == 0);
57 | }
58 |
59 | static int length(LinearList *This){
60 | LinearList_P *p = This->This;
61 | return p->length;
62 | }
63 |
64 | static void print(LinearList *This){
65 | LinearList_P *p = This->This;
66 | int i;
67 | for (i=0; i < p->length; i++){
68 | printf("%d ", p->elem[i]);
69 | }
70 | printf("\n");
71 | }
72 |
73 | static int indexElem(LinearList *This, ElemType* x){
74 | LinearList_P *p = This->This;
75 | int pos = -1;
76 | for (int i = 0; i < p->length; i++){
77 | if (p->elem[i] == *x){
78 | pos = i;
79 | }
80 | }
81 | return pos;
82 | }
83 |
84 | static int priorElem(LinearList *This, ElemType* cur_elem, ElemType* pre_elem){
85 | LinearList_P *p = This->This;
86 | int pos = -1;
87 | pos = indexElem(This, cur_elem);
88 | if(pos <= 0) return -1;
89 | *pre_elem = p->elem[pos-1];
90 | return 0;
91 | }
92 |
93 | static int getElem(LinearList *This, int index, ElemType* e){
94 | LinearList_P *p = This->This;
95 | if (index<0 || index >= p->length) return -1;
96 | *e = p->elem[index];
97 | return 0;
98 | }
99 |
100 | static int modifyElem(LinearList *This, int index, ElemType* e){
101 | LinearList_P *p = This->This;
102 | if (index<0 || index >= p->length) return -1;
103 | p->elem[index] = *e;
104 | return 0;
105 | }
106 |
107 | static int nextElem(LinearList *This, ElemType* cur_elem, ElemType* next_elem){
108 | LinearList_P *p = This->This;
109 | int pos = -1;
110 | pos = indexElem(This, cur_elem);
111 | if(pos == -1 || pos == (p->length - 1)) return -1;
112 | *next_elem = p->elem[pos+1];
113 | return 0;
114 | }
115 |
116 | static int insertElem(LinearList *This, int index, ElemType* e){
117 | LinearList_P *p = This->This;
118 | if (index<0 || index >= p->length) return -1;
119 | if (p->length >= p->size){ //判断存储空间是否够用
120 | ElemType *newbase = (ElemType *)realloc(p->elem, (p->size + LISTINCREMENT)*sizeof(ElemType));
121 | if (!newbase) return -1;//存储空间分配失败
122 | p->elem = newbase;//新基址
123 | p->size += LISTINCREMENT;//增加存储容量
124 | }
125 | ElemType *elem_q = NULL;, *elem_p = NULL;;
126 | elem_q = &(p->elem[index]); //q为插入位置
127 | for (elem_p = &(p->elem[p->length - 1]); elem_p >= elem_q; --elem_p){ //从ai到an-1依次后移,注意后移操作要从后往前进行
128 | *(elem_p + 1) = *elem_p;
129 | }
130 | *elem_q = *e;
131 | ++p->length;
132 | return 0;
133 | }
134 |
135 | static int deleteElem(LinearList *This, int index, ElemType* e){
136 | LinearList_P *p = This->This;
137 | if (index<1 || index > p->length) return -1;
138 | ElemType *elem_q = NULL;, *elem_p = NULL;;
139 | elem_p = &(p->elem[index]);//elem_p为被删除元素的位置
140 | *e = *elem_p; //被删除的元素赋值给e
141 | elem_q = p->elem + p->length - 1;//elem_q指向表尾最后一个元素
142 | for (++elem_p; elem_p <= elem_q; ++elem_p){ //从elem_p的下一个元素开始依次前移
143 | *(elem_p - 1) = *elem_p;
144 | }
145 | --p->length;
146 | return 0;
147 | }
148 |
149 | static int appendElem(LinearList *This,ElemType* e){
150 | LinearList_P *p = This->This;
151 | if (p->length >= p->size){ //判断存储空间是否够用
152 | ElemType *newbase = (ElemType *)realloc(p->elem, (p->size + LISTINCREMENT)*sizeof(ElemType));
153 | if (!newbase) return -1;//存储空间分配失败
154 | p->elem = newbase;//新基址
155 | p->size += LISTINCREMENT;//增加存储容量
156 | }
157 | p->elem[p->length] = *e;
158 | ++p->length;
159 | return 0;
160 | }
161 |
162 | static int popElem(LinearList *This, ElemType* e){
163 | LinearList_P *p = This->This;
164 | if (isEmpty(This)) return -1;
165 | *e = p->elem[p->length - 1];
166 | --p->length;
167 | return 0;
168 | }
169 |
170 |
--------------------------------------------------------------------------------
/linear_list/linear_list.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __LINEAR_LIST_H
3 | #define __LINEAR_LIST_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef int ElemType; //数据元素的类型,假设是int型的
7 |
8 | typedef struct LinearList_P{
9 | ElemType *elem; //存储空间的基地址
10 | int length; //当前线性表的长度
11 | int size; //当前分配的存储容量
12 | }LinearList_P;
13 |
14 | typedef struct LinearList{
15 | LinearList_P *This;
16 | void (*clear)(struct LinearList *This);
17 | int (*isEmpty)(struct LinearList *This);
18 | int (*length)(struct LinearList *This);
19 | void (*print)(struct LinearList *This);
20 | int (*indexElem)(struct LinearList *This, ElemType* x);
21 | int (*priorElem)(struct LinearList *This, ElemType* cur_elem, ElemType* pre_elem);
22 | int (*getElem)(struct LinearList *This, int index, ElemType* e);
23 | int (*modifyElem)(struct LinearList *This, int index, ElemType* e);
24 | int (*nextElem)(struct LinearList *This, ElemType* cur_elem, ElemType* next_elem);
25 | int (*insertElem)(struct LinearList *This, int index, ElemType* e);
26 | int (*deleteElem)(struct LinearList *This, int index, ElemType* e);
27 | int (*appendElem)(struct LinearList *This,ElemType* e);
28 | int (*popElem)(struct LinearList *This, ElemType* e);
29 | }LinearList;
30 |
31 | /* Exported define -----------------------------------------------------------*/
32 | #define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
33 | #define LISTINCREMENT 10 //线性表存储空间的分配增量(当存储空间不够时要用到)
34 | /* Exported macro ------------------------------------------------------------*/
35 | LinearList *InitLinearList();
36 | void DestroyLinearList(LinearList *L);
37 |
38 | #endif
39 |
--------------------------------------------------------------------------------
/linear_list/test_linear_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearList.h"
4 |
5 | int main(void)
6 | {
7 | int i;
8 | ElemType elem,elem1;
9 | LinearList *list = InitLinearList();
10 | printf("list is empty:%d\n",list->isEmpty(list));
11 | for (i = 0; i < 10; i++){
12 | list->appendElem(list,&i);
13 | }
14 | list->print(list);
15 | printf("list is empty:%d\n",list->isEmpty(list));
16 | printf("list length:%d\n",list->length(list));
17 | list->clear(list);
18 | for (i = 10; i < 20; i++){
19 | list->appendElem(list,&i);
20 | }
21 | list->print(list);
22 | elem = 17;
23 | printf("the index of 17 is %d\n",list->indexElem(list,&elem));
24 | list->priorElem(list,&elem,&elem1);
25 | printf("the prior elem of 17 is %d\n",elem1);
26 | list->nextElem(list,&elem,&elem1);
27 | printf("the next elem of 17 is %d\n",elem1);
28 | list->getElem(list,3,&elem1);
29 | printf("the elem of index 3 is %d\n",elem1);
30 | list->modifyElem(list,3,&elem);
31 | list->getElem(list,3,&elem1);
32 | printf("modify the elem of index 3 to %d\n",elem1);
33 | list->print(list);
34 | elem = 25;
35 | list->insertElem(list,5,&elem);
36 | printf("insert elem %d to index 5\n",elem);
37 | list->deleteElem(list,7,&elem);
38 | printf("delete elem %d of index 7\n",elem);
39 | list->print(list);
40 | list->popElem(list,&elem);
41 | printf("pop elem %d\n",elem);
42 | list->print(list);
43 | DestroyLinearList(list);
44 | return 0;
45 | }
--------------------------------------------------------------------------------
/linear_list_stack/bracket_matching.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearListStack.h"
4 |
5 | int bracketMatching(char *str){
6 | char elem;
7 | int result = 0;
8 | int bracket_contained = 0;
9 | LinearListStack *stack = InitLinearListStack();
10 | while(*str != '\0'){
11 | if(*str == '{' || *str == '[' || *str == '('){
12 | bracket_contained = 1;
13 | stack->push(stack,str);
14 | }else if(*str == '}'){
15 | bracket_contained = 1;
16 | if(stack->length(stack)){
17 | stack->getTop(stack,&elem);
18 | if(elem == '{'){
19 | stack->pop(stack,&elem);
20 | }else if(elem == '('){
21 | printf("Bracket matching failed : \')\' expected! \n");
22 | DestroyLinearListStack(stack);
23 | return 0;
24 | }else if(elem == '['){
25 | printf("Bracket matching failed : \']\' expected! \n");
26 | DestroyLinearListStack(stack);
27 | return 0;
28 | }
29 | }else{
30 | printf("Bracket matching failed : \'{\' expected! \n");
31 | DestroyLinearListStack(stack);
32 | return 0;
33 | }
34 | }else if(*str == ']'){
35 | bracket_contained = 1;
36 | if(stack->length(stack)){
37 | stack->getTop(stack,&elem);
38 | if(elem == '['){
39 | stack->pop(stack,&elem);
40 | }else if(elem == '{'){
41 | printf("Bracket matching failed : \'}\' expected! \n");
42 | DestroyLinearListStack(stack);
43 | return 0;
44 | }else if(elem == '('){
45 | printf("Bracket matching failed : \')\' expected! \n");
46 | DestroyLinearListStack(stack);
47 | return 0;
48 | }
49 | }else{
50 | printf("Bracket matching failed : \'[\' expected! \n");
51 | DestroyLinearListStack(stack);
52 | return 0;
53 | }
54 | }else if(*str == ')'){
55 | bracket_contained = 1;
56 | if(stack->length(stack)){
57 | stack->getTop(stack,&elem);
58 | if(elem == '('){
59 | stack->pop(stack,&elem);
60 | }else if(elem == '['){
61 | printf("Bracket matching failed : \']\' expected! \n");
62 | DestroyLinearListStack(stack);
63 | return 0;
64 | }else if(elem == '{'){
65 | printf("Bracket matching failed : \'}\' expected! \n");
66 | DestroyLinearListStack(stack);
67 | return 0;
68 | }
69 | }else{
70 | printf("Bracket matching failed : \'(\' expected! \n");
71 | DestroyLinearListStack(stack);
72 | return 0;
73 | }
74 | }
75 | str++;
76 | }
77 | if(bracket_contained){
78 | if(stack->length(stack) == 0){
79 | printf("Bracket matching successed\n");
80 | result = 1;
81 | }else{
82 | stack->getTop(stack,&elem);
83 | switch(elem){
84 | case '{':
85 | printf("Bracket matching failed : \'}\' expected! \n");
86 | break;
87 | case '[':
88 | printf("Bracket matching failed : \']\' expected! \n");
89 | break;
90 | case '(':
91 | printf("Bracket matching failed : \')\' expected! \n");
92 | break;
93 | }
94 | result = 0;
95 | }
96 | }else{
97 | printf("String doesn't contain bracket!\n");
98 | result = 0;
99 | }
100 | DestroyLinearListStack(stack);
101 | return result;
102 | }
103 |
104 | int main(void)
105 | {
106 | int num;
107 | char str[100];
108 | printf("please enter a string!\n");
109 | gets(str);
110 | printf("\n");
111 | bracketMatching(str);
112 | return 0;
113 | }
--------------------------------------------------------------------------------
/linear_list_stack/evaluate_expression.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearListStack.h"
4 |
5 | char opetr_char[7]={'+','-','*','/','%','(',')'};
6 |
7 | //operational character priority (a,b),operational character b after a.
8 | // b: + - * / % ( )
9 | // a:
10 | // + > > < < < < >
11 | // - > > < < < < >
12 | // * > > > > > < >
13 | // / > > > > > < >
14 | // % > > > > > < >
15 | // ( < < < < < < =
16 | // ) > > > > > N N
17 |
18 | char opetr_priority[7][7]={
19 | {'>','>','<','<','<','<','>'},
20 | {'>','>','<','<','<','<','>'},
21 | {'>','>','>','>','>','<','>'},
22 | {'>','>','>','>','>','<','>'},
23 | {'>','>','>','>','>','<','>'},
24 | {'<','<','<','<','<','<','='},
25 | {'>','>','>','>','>','N','N'}
26 | };
27 |
28 | int isOpetrChar(char ch){
29 | int i;
30 | for(i=0;i<7;i++){
31 | if(ch == opetr_char[i]) return i;
32 | }
33 | return -1;
34 | }
35 |
36 | char getOpetrPrior(char a,char b){
37 | int i = isOpetrChar(a);
38 | int j = isOpetrChar(b);
39 | return opetr_priority[i][j];
40 | }
41 |
42 | double my_pow(double a,int b){
43 | int s = 0,i;
44 | double r = 1;
45 | if(b == 0) return 1;
46 | if(b<0){
47 | b*=-1;
48 | s = 1;
49 | }
50 | for(i = 0; i < b; i++){
51 | r *= a;
52 | }
53 | if(s) r = 1/r;
54 | return r;
55 | }
56 |
57 | int getOpndNumFromStack(LinearListStack *opnd_stack){
58 | int num = 0,i = 0;
59 | char elem;
60 | if(opnd_stack->length(opnd_stack)){
61 | opnd_stack->pop(opnd_stack,&elem);
62 | if(elem == ' '){
63 | while(elem == ' '){
64 | if(opnd_stack->length(opnd_stack) == 0) break;//确保不会pop空栈
65 | opnd_stack->pop(opnd_stack,&elem);
66 | }
67 | }
68 | if(elem != ' '){ //两个判断必须分开来写
69 | while(elem != ' ' && elem != '-'){
70 | num += my_pow(10,i)*(elem - 0x30);
71 | if(opnd_stack->length(opnd_stack) == 0) break; //确保不会pop空栈
72 | opnd_stack->pop(opnd_stack,&elem);
73 | i++;
74 | }
75 | if(elem == '-'){ //如果是负号
76 | num = -num;
77 | }else if(elem == ' '){ //将移出的空格间隔符再补进去
78 | opnd_stack->push(opnd_stack,&elem);
79 | }
80 | }
81 | }
82 | return num;
83 | }
84 |
85 | int operate(int number_a,char opetr_char,int number_b){
86 | int result = 0;
87 | switch(opetr_char){
88 | case '+':
89 | result = number_a + number_b;
90 | break;
91 | case '-':
92 | result = number_a - number_b;
93 | break;
94 | case '*':
95 | result = number_a * number_b;
96 | break;
97 | case '/':
98 | result = number_a / number_b;
99 | break;
100 | case '%':
101 | result = number_a % number_b;
102 | break;
103 | default:
104 | result = number_b;
105 | break;
106 | }
107 | return result;
108 | }
109 |
110 | void pushResultToStack(LinearListStack *opnd_stack, int result){
111 | char elem[10],n_elem;
112 | int i = 0,index = 0;
113 | if(result < 0){
114 | result = - result;
115 | n_elem = '-';
116 | opnd_stack->push(opnd_stack,&n_elem);
117 | }else if(result == 0){
118 | n_elem = '0';
119 | opnd_stack->push(opnd_stack,&n_elem);
120 | }
121 | while(result > 0){
122 | elem[index] = (result % 10) + 0x30;
123 | result /= 10;
124 | index++;
125 | }
126 | for(i=index;i>0;i--){
127 | opnd_stack->push(opnd_stack,&elem[i-1]);
128 | }
129 | }
130 |
131 | LinearListStack *evaluateExpression(char *str){
132 | char elem,opetr_prior,opetr_char;
133 | int cal_result = 0,number_a,number_b;
134 | int num_before_flag = 0;
135 | LinearListStack *optr_stack = InitLinearListStack(); //运算符栈
136 | LinearListStack *opnd_stack = InitLinearListStack(); //操作数栈
137 | while(*str != '\0'){
138 | if(isOpetrChar(*str) != -1){
139 | if(optr_stack->length(optr_stack)){
140 | optr_stack->getTop(optr_stack,&elem);
141 | opetr_prior = getOpetrPrior(elem,*str);
142 | switch(opetr_prior){
143 | case '<': //栈顶运算符优先级低
144 | if(num_before_flag == 0){ //前一个入栈的是符号
145 | if(*str == '-'){ //此时'-'号表示为负号
146 | opnd_stack->push(opnd_stack,str);//'-'号加入操作数栈
147 | num_before_flag = 1; //加入的是数字
148 | }else if(elem == '(' || *str == '('){ //多个括号与操作符相邻的情况
149 | optr_stack->push(optr_stack,str);
150 | elem = ' '; //数字字符加入空格间隔符
151 | opnd_stack->push(opnd_stack,&elem);
152 | num_before_flag = 0;//加入运算符
153 | }else{
154 | printf("Expression format error!");
155 | break;
156 | }
157 | }else{ //前面有数字入栈
158 | optr_stack->push(optr_stack,str);
159 | elem = ' '; //数字字符加入空格间隔符
160 | opnd_stack->push(opnd_stack,&elem);
161 | num_before_flag = 0;//加入运算符
162 | }
163 | break;
164 | case '=':
165 | optr_stack->pop(optr_stack,&elem);//脱括号
166 | num_before_flag = 1; //脱括号等效为加入的是数字
167 | break;
168 | case '>': //栈顶运算符优先级高
169 | if(num_before_flag == 0){ //前一个入栈的是符号
170 | if(*str == '-'){ //此时'-'号表示为负号
171 | opnd_stack->push(opnd_stack,str);//'-'号加入操作数栈
172 | num_before_flag = 1; //加入的是数字
173 | }else{
174 | printf("Expression format error!");
175 | break;
176 | }
177 | }else{ //前一个入栈的是数字
178 | optr_stack->pop(optr_stack,&opetr_char);//取运算符
179 | number_b = getOpndNumFromStack(opnd_stack);
180 | number_a = getOpndNumFromStack(opnd_stack);
181 | cal_result = operate(number_a,opetr_char,number_b);
182 | printf("%d",number_a);
183 | printf(" %c ",opetr_char);
184 | printf("%d = ",number_b);
185 | printf("%d\n",cal_result);
186 | pushResultToStack(opnd_stack, cal_result);
187 | num_before_flag = 1; //加入的是数字
188 | str--;
189 | }
190 | break;
191 | }
192 | }else{
193 | //第一个运算符,此时运算符栈是空的
194 | if(num_before_flag == 0){ //前面没有数字入栈
195 | if(*str == '-'){ //此时'-'号表示为负号
196 | opnd_stack->push(opnd_stack,str);//'-'号加入操作数栈
197 | num_before_flag = 1; //加入的是数字
198 | }else if(*str == '('){
199 | optr_stack->push(optr_stack,str);
200 | elem = ' '; //数字字符加入空格间隔符
201 | opnd_stack->push(opnd_stack,&elem);
202 | num_before_flag = 0;//加入运算符
203 | }else{
204 | printf("Expression format error!");
205 | break;
206 | }
207 | }else{ //前面有数字入栈
208 | optr_stack->push(optr_stack,str);
209 | elem = ' '; //数字字符加入空格间隔符
210 | opnd_stack->push(opnd_stack,&elem);
211 | num_before_flag = 0;//加入运算符
212 | }
213 | }
214 | }else{
215 | if(*str >= 0x30 && *str <= 0x39){
216 | opnd_stack->push(opnd_stack,str);
217 | num_before_flag = 1; //加入的是数字
218 | }
219 | }
220 | str++;
221 | }
222 | if(*str == '\0'){ //输入完成
223 | while(optr_stack->length(optr_stack)){
224 | optr_stack->pop(optr_stack,&opetr_char);//取运算符
225 | if(isOpetrChar(opetr_char)<5){
226 | number_b = getOpndNumFromStack(opnd_stack);
227 | number_a = getOpndNumFromStack(opnd_stack);
228 | cal_result = operate(number_a,opetr_char,number_b);
229 | printf("%d",number_a);
230 | printf(" %c ",opetr_char);
231 | printf("%d = ",number_b);
232 | printf("%d\n",cal_result);
233 | pushResultToStack(opnd_stack, cal_result);
234 | }
235 | }
236 | }
237 | DestroyLinearListStack(optr_stack);
238 | return opnd_stack;
239 | }
240 |
241 | int main(void)
242 | {
243 | int i;
244 | char string[1000];
245 | LinearListStack *optr_result = NULL;
246 | printf("please enter a expression:");
247 | gets(string);
248 | optr_result = evaluateExpression(string);
249 | printf("%s = ",string);
250 | optr_result->risePrint(optr_result);
251 | DestroyLinearListStack(optr_result);
252 | return 0;
253 | }
--------------------------------------------------------------------------------
/linear_list_stack/labyrinth_analyze.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearListStack.h"
4 |
5 | int labyrinth[10][10]={
6 | {1,1,1,1,1,1,1,1,1,1},
7 | {1,0,0,1,0,0,0,1,0,1},
8 | {1,0,0,1,0,0,0,1,0,1},
9 | {1,0,0,0,0,1,1,0,0,1},
10 | {1,0,1,1,1,0,0,0,0,1},
11 | {1,0,0,0,1,0,0,0,0,1},
12 | {1,0,1,0,0,0,1,0,0,1},
13 | {1,0,1,1,1,0,1,1,0,1},
14 | {1,1,0,0,0,0,0,0,0,1},
15 | {1,1,1,1,1,1,1,1,1,1},
16 | };
17 |
18 | void copyArray(int a[][10],int b[][10]){
19 | int i,j;
20 | for(i=0;i<10;i++){
21 | for(j=0;j<10;j++){
22 | a[i][j] = b[i][j];
23 | }
24 | }
25 | }
26 |
27 | void printLabyrinth(int labyrinth[][10]){
28 | int i,j;
29 | printf("The labyrinth is:\n\n");
30 | for(i=0;i<10;i++){
31 | for(j=0;j<10;j++){
32 | if((i == 1 && j == 1)||(i == 8 && j == 8)){
33 | printf("**");
34 | }else{
35 | if(labyrinth[i][j] == 1){
36 | printf("%c%c",0xa8, 0x80);
37 | }else if(labyrinth[i][j] == 0){
38 | printf(" ");
39 | }else if(labyrinth[i][j] == 2){
40 | printf("oo");
41 | }else if(labyrinth[i][j] == 3){
42 | printf("xx");
43 | }
44 | }
45 | }
46 | printf("\n");
47 | }
48 | }
49 |
50 | void labyrinthPath(int labyrinth[][10]){
51 | int labyrinthpath[10][10]; //轨迹图
52 | int current_step = 0; //当前步数
53 | int start_i = 1,start_j = 1; //开始位置
54 | int end_i = 8,end_j = 8; //结束位置
55 | int current_i = start_i,current_j = start_j; //当前位置
56 | int current_direction = 0; //0:向下、1:向右、2:向上、3:向左 逆时针旋转方向,数值小的优先级高
57 | char stack_temp;
58 | char stack_history_i,stack_history_j;
59 | LinearListStack *stack_i = InitLinearListStack();
60 | LinearListStack *stack_j = InitLinearListStack();
61 | copyArray(labyrinthpath,labyrinth);
62 | do{
63 | if(labyrinthpath[current_i][current_j] == 0){ //未走过,且可以走
64 | labyrinthpath[current_i][current_j] = 2; //留下足迹
65 | stack_temp = (char)(current_i+0x30);
66 | stack_i->push(stack_i,&stack_temp);
67 | stack_temp = (char)(current_j+0x30);
68 | stack_j->push(stack_j,&stack_temp);//加入路径
69 | current_step ++;
70 | printLabyrinth(labyrinthpath);
71 | if(current_i == end_i && current_j == end_j) break;//到达终点
72 | }else{
73 | if(stack_i->length(stack_i)){
74 | switch(current_direction){
75 | case 0:
76 | if(labyrinthpath[current_i + 1][current_j] == 0){
77 | stack_i->getTop(stack_i,&stack_history_i);
78 | stack_j->getTop(stack_j,&stack_history_j);
79 | if((stack_history_i != current_i+0x30)||(stack_history_j != current_j+0x30)){
80 | stack_temp = (char)(current_i+0x30);
81 | stack_i->push(stack_i,&stack_temp);
82 | stack_temp = (char)(current_j+0x30);
83 | stack_j->push(stack_j,&stack_temp);//将移出的重新加入路径
84 | }
85 | current_i++;//向下找
86 | current_direction = 0;
87 | }else{
88 | current_direction = 1;
89 | }
90 | break;
91 | case 1:
92 | if(labyrinthpath[current_i][current_j + 1] == 0){
93 | stack_i->getTop(stack_i,&stack_history_i);
94 | stack_j->getTop(stack_j,&stack_history_j);
95 | if((stack_history_i != current_i+0x30)||(stack_history_j != current_j+0x30)){
96 | stack_temp = (char)(current_i+0x30);
97 | stack_i->push(stack_i,&stack_temp);
98 | stack_temp = (char)(current_j+0x30);
99 | stack_j->push(stack_j,&stack_temp);//将移出的重新加入路径
100 | }
101 | current_j++; //向右找
102 | current_direction = 0;
103 | }else{
104 | current_direction = 2;
105 | }
106 | break;
107 | case 2:
108 | if(labyrinthpath[current_i - 1][current_j] == 0){
109 | stack_i->getTop(stack_i,&stack_history_i);
110 | stack_j->getTop(stack_j,&stack_history_j);
111 | if((stack_history_i != current_i+0x30)||(stack_history_j != current_j+0x30)){
112 | stack_temp = (char)(current_i+0x30);
113 | stack_i->push(stack_i,&stack_temp);
114 | stack_temp = (char)(current_j+0x30);
115 | stack_j->push(stack_j,&stack_temp);//将移出的重新加入路径
116 | }
117 | current_i--;//向上找
118 | current_direction = 0;
119 | }else{
120 | current_direction = 3;
121 | }
122 | break;
123 | case 3:
124 | if(labyrinthpath[current_i][current_j - 1] == 0){
125 | stack_i->getTop(stack_i,&stack_history_i);
126 | stack_j->getTop(stack_j,&stack_history_j);
127 | if((stack_history_i != current_i+0x30)||(stack_history_j != current_j+0x30)){
128 | stack_temp = (char)(current_i+0x30);
129 | stack_i->push(stack_i,&stack_temp);
130 | stack_temp = (char)(current_j+0x30);
131 | stack_j->push(stack_j,&stack_temp);//将移出的重新加入路径
132 | }
133 | current_j--;//向左找
134 | current_direction = 0;
135 | }else{
136 | current_direction = 4;
137 | }
138 | break;
139 | case 4://四周均不通
140 | if(labyrinthpath[current_i][current_j] != 1){
141 | labyrinthpath[current_i][current_j] = 3; //死路标记
142 | printLabyrinth(labyrinthpath);
143 | }
144 | stack_i->pop(stack_i,&stack_temp);
145 | current_i = (int)stack_temp-0x30;
146 | stack_j->pop(stack_j,&stack_temp);
147 | current_j = (int)stack_temp-0x30;
148 | current_direction = 0;
149 | break;
150 | }
151 | }
152 | }
153 | }while(stack_i->length(stack_i));
154 | if(stack_i->length(stack_i)){
155 | printf("Maze path:\n");
156 | printf("i:");
157 | stack_i->risePrint(stack_i);
158 | printf("j:");
159 | stack_j->risePrint(stack_j);
160 | }else{
161 | printf("No path found!\n");
162 | }
163 | DestroyLinearListStack(stack_i);
164 | DestroyLinearListStack(stack_j);
165 | }
166 |
167 | int main(void)
168 | {
169 | labyrinthPath(labyrinth);
170 | return 0;
171 | }
--------------------------------------------------------------------------------
/linear_list_stack/line_edit.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearListStack.h"
4 |
5 | void lineEdit(){
6 | char elem,ch;
7 | LinearListStack *stack = InitLinearListStack();
8 | ch = getchar();
9 | while(ch != EOF){
10 | while(ch != EOF && ch != '\n'){
11 | switch(ch){
12 | case '#':
13 | if(stack->length(stack)){
14 | stack->pop(stack,&elem);
15 | }
16 | break;
17 | case '@':
18 | stack->clear(stack);
19 | break;
20 | default:
21 | stack->push(stack,&ch);
22 | break;
23 | }
24 | ch = getchar();
25 | }
26 | stack->risePrint(stack);
27 | stack->clear(stack);
28 | if(ch != EOF) ch = getchar();
29 | }
30 | DestroyLinearListStack(stack);
31 | }
32 |
33 | int main(void)
34 | {
35 | lineEdit();
36 | return 0;
37 | }
--------------------------------------------------------------------------------
/linear_list_stack/linear_list_stack.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearListStack.h"
4 | //线性表栈
5 |
6 | static void clear(LinearListStack *This);
7 | static int isEmpty(LinearListStack *This);
8 | static int length(LinearListStack *This);
9 | static void risePrint(LinearListStack *This);
10 | static void downPrint(LinearListStack *This);
11 | static int getTop(LinearListStack *This,ElemType* e);
12 | static int push(LinearListStack *This,ElemType* e);
13 | static int pop(LinearListStack *This, ElemType* e);
14 |
15 | LinearListStack *InitLinearListStack(){
16 | LinearListStack *L = (LinearListStack *)malloc(sizeof(LinearListStack));
17 | LinearListStack_P *p = (LinearListStack_P *)malloc(sizeof(LinearListStack_P));
18 | p->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
19 | p->top = p->base;
20 | p->length = 0; //当前长度
21 | p->size = STACK_INIT_SIZE; //当前分配量
22 | L->This = p;
23 | L->clear = clear;
24 | L->isEmpty = isEmpty;
25 | L->length = length;
26 | L->risePrint = risePrint;
27 | L->downPrint = downPrint;
28 | L->getTop = getTop;
29 | L->push = push;
30 | L->pop = pop;
31 | return L;
32 | }
33 |
34 | void DestroyLinearListStack(LinearListStack *L){
35 | free(L->This);
36 | free(L);
37 | L = NULL;
38 | }
39 |
40 | static void clear(LinearListStack *This){
41 | LinearListStack_P *p = This->This;
42 | p->top = p->base;
43 | p->length = 0; //当前长度
44 | }
45 |
46 | static int isEmpty(LinearListStack *This){
47 | LinearListStack_P *p = This->This;
48 | return (p->length == 0);
49 | }
50 |
51 | static int length(LinearListStack *This){
52 | LinearListStack_P *p = This->This;
53 | return p->length;
54 | }
55 |
56 | static void risePrint(LinearListStack *This){
57 | LinearListStack_P *p = This->This;
58 | int i;
59 | for (i=0; i < p->length; i++){
60 | printf("%c", *(p->base + i));
61 | }
62 | printf("\n");
63 | }
64 |
65 | static void downPrint(LinearListStack *This){
66 | LinearListStack_P *p = This->This;
67 | int i;
68 | for (i=0; i < p->length; i++){
69 | printf("%c", *(p->top - 1 - i));
70 | }
71 | printf("\n");
72 | }
73 |
74 | static int getTop(LinearListStack *This,ElemType* e){
75 | LinearListStack_P *p = This->This;
76 | if (p->top == p->base) return -1;
77 | *e = *(p->top-1);
78 | return 0;
79 | }
80 |
81 | static int push(LinearListStack *This,ElemType* e){
82 | LinearListStack_P *p = This->This;
83 | if (p->top - p->base >= p->size){ //判断存储空间是否够用
84 | ElemType *newbase = (ElemType *)realloc(p->base, (p->size + STACKINCREMENT)*sizeof(ElemType));
85 | if (!newbase) return -1;//存储空间分配失败
86 | p->base = newbase;//新基址
87 | p->top = p->base + p->size;
88 | p->size += STACKINCREMENT;//增加存储容量
89 | }
90 | *((p->top)++) = *e;
91 | ++p->length;
92 | return 0;
93 | }
94 |
95 | static int pop(LinearListStack *This, ElemType* e){
96 | LinearListStack_P *p = This->This;
97 | if (p->top == p->base) return -1;
98 | *e = *(p->top-1);
99 | p->top--;
100 | p->length--;
101 | return 0;
102 | }
103 |
104 |
--------------------------------------------------------------------------------
/linear_list_stack/linear_list_stack.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __LINEARLISTSTACK_H
3 | #define __LINEARLISTSTACK_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef char ElemType;
7 |
8 | typedef struct LinearListStack_P{
9 | ElemType *base;
10 | ElemType *top; //栈顶指针
11 | int length; //当前线性表栈的长度
12 | int size; //当前分配的存储容量
13 | }LinearListStack_P;
14 |
15 | typedef struct LinearListStack{
16 | LinearListStack_P *This;
17 | void (*clear)(struct LinearListStack *This);
18 | int (*isEmpty)(struct LinearListStack *This);
19 | int (*length)(struct LinearListStack *This);
20 | void (*risePrint)(struct LinearListStack *This);
21 | void (*downPrint)(struct LinearListStack *This);
22 | int (*getTop)(struct LinearListStack *This,ElemType* e);
23 | int (*push)(struct LinearListStack *This,ElemType* e);
24 | int (*pop)(struct LinearListStack *This, ElemType* e);
25 | }LinearListStack;
26 |
27 | /* Exported define -----------------------------------------------------------*/
28 | #define STACK_INIT_SIZE 100 //线性表栈存储空间的初始分配量
29 | #define STACKINCREMENT 10 //线性表栈存储空间的分配增量(当存储空间不够时要用到)
30 | /* Exported macro ------------------------------------------------------------*/
31 | LinearListStack *InitLinearListStack();
32 | void DestroyLinearListStack(LinearListStack *L);
33 |
34 | #endif
35 |
--------------------------------------------------------------------------------
/linear_list_stack/number_conversion.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearListStack.h"
4 |
5 | int strlen(char *str){
6 | int i = 0;
7 | while(*(str+i) != '\0'){
8 | i++;
9 | }
10 | return i;
11 | }
12 |
13 | double my_pow(double a,int b){
14 | int s = 0,i;
15 | double r = 1;
16 | if(b == 0) return 1;
17 | if(b<0){
18 | b*=-1;
19 | s = 1;
20 | }
21 | for(i = 0; i < b; i++){
22 | r *= a;
23 | }
24 | if(s) r = 1/r;
25 | return r;
26 | }
27 |
28 | LinearListStack *intToHex(int num){
29 | LinearListStack *stack = InitLinearListStack();
30 | char item;
31 | if(num < 0){
32 | num = -num;
33 | }else if(num == 0){
34 | item = 0x30;
35 | stack->push(stack,&item);
36 | }
37 | while(num){
38 | item = num % 16;
39 | if(item <= 9){
40 | item += 0x30;
41 | }else{
42 | item += 0x37;
43 | }
44 | stack->push(stack,&item);
45 | num /= 16;
46 | }
47 | return stack;
48 | }
49 |
50 | LinearListStack *intToDec(int num){
51 | LinearListStack *stack = InitLinearListStack();
52 | char item;
53 | if(num < 0){
54 | num = -num;
55 | }else if(num == 0){
56 | item = 0x30;
57 | stack->push(stack,&item);
58 | }
59 | while(num){
60 | item = num % 10;
61 | item += 0x30;
62 | stack->push(stack,&item);
63 | num /= 10;
64 | }
65 | return stack;
66 | }
67 |
68 | LinearListStack *intToOct(int num){
69 | LinearListStack *stack = InitLinearListStack();
70 | char item;
71 | if(num < 0){
72 | num = -num;
73 | }else if(num == 0){
74 | item = 0x30;
75 | stack->push(stack,&item);
76 | }
77 | while(num){
78 | item = num % 8;
79 | item += 0x30;
80 | stack->push(stack,&item);
81 | num /= 8;
82 | }
83 | return stack;
84 | }
85 |
86 | LinearListStack *intToBin(int num){
87 | LinearListStack *stack = InitLinearListStack();
88 | char item;
89 | if(num < 0){
90 | num = -num;
91 | }else if(num == 0){
92 | item = 0x30;
93 | stack->push(stack,&item);
94 | }
95 | while(num){
96 | item = num % 2;
97 | item += 0x30;
98 | stack->push(stack,&item);
99 | num /= 2;
100 | }
101 | return stack;
102 | }
103 |
104 | void displayConvert(int num){
105 | int negative = 0;
106 | LinearListStack *stack = NULL;;
107 | negative = num < 0 ? 1 : 0;
108 | stack = intToDec(num);
109 | negative ? printf("DEC: -") : printf("DEC: ");
110 | stack->downPrint(stack);
111 | DestroyLinearListStack(stack);
112 | stack = intToHex(num);
113 | negative ? printf("HEX: -0X") : printf("HEX: 0X");
114 | stack->downPrint(stack);
115 | DestroyLinearListStack(stack);
116 | if(negative){
117 | stack = intToHex(0XFFFF + num + 1);
118 | printf("HEX negative number saved form: 0X");
119 | stack->downPrint(stack);
120 | }
121 | DestroyLinearListStack(stack);
122 | stack = intToOct(num);
123 | negative ? printf("OCT: -o") : printf("OCT: o");
124 | stack->downPrint(stack);
125 | DestroyLinearListStack(stack);
126 | if(negative){
127 | stack = intToOct(0XFFFF + num + 1);
128 | printf("OCT negative number saved form: o");
129 | stack->downPrint(stack);
130 | }
131 | DestroyLinearListStack(stack);
132 | stack = intToBin(num);
133 | negative ? printf("BIN: -b") : printf("BIN: b");
134 | stack->downPrint(stack);
135 | DestroyLinearListStack(stack);
136 | if(negative){
137 | stack = intToBin(0XFFFF + num + 1);
138 | printf("BIN negative number saved form: b");
139 | stack->downPrint(stack);
140 | }
141 | DestroyLinearListStack(stack);
142 | }
143 |
144 | int stringToInt(char *str){
145 | int basenum = 0,i = 0,num = 0;
146 | int stack_length = 0;
147 | int start_save = 0;
148 | int negative = 0;
149 | char item;
150 | LinearListStack *stack = InitLinearListStack();
151 | while(*str != '\0'){
152 | if(start_save != 1){
153 | if(*str == '-'){
154 | negative = 1;
155 | }else if(*str == 'o' || *str == 'O'){
156 | start_save = 1;
157 | basenum = 8;
158 | }else if(*str == 'b' || *str == 'B'){
159 | start_save = 1;
160 | basenum = 2;
161 | }else if(*str == 'd' || *str == 'D'){
162 | start_save = 1;
163 | basenum = 10;
164 | }else if(*str == 'x' || *str == 'X'){
165 | if(start_save == 2){
166 | start_save = 1;
167 | basenum = 16;
168 | }
169 | }else if(*str == '0'){
170 | start_save = 2;
171 | }
172 | }else if(start_save == 1){
173 | stack->push(stack,str);
174 | }
175 | str++;
176 | }
177 | stack_length = stack->length(stack);
178 | for(i=0;ipop(stack,&item);
180 | if(item >= 0x30 && item <= 0x39){
181 | item -= 0x30;
182 | }else if(item >= 0x41 && item <= 0x46){
183 | if(basenum == 16){
184 | item -= 0x37;
185 | }else{
186 | printf("Num string formal error!\n");
187 | DestroyLinearListStack(stack);
188 | return 0;
189 | }
190 | }else if(item >= 0x61 && item <= 0x66){
191 | if(basenum == 16){
192 | item -= 0x57;
193 | }else{
194 | printf("Num string formal error!\n");
195 | DestroyLinearListStack(stack);
196 | return 0;
197 | }
198 | }else{
199 | printf("Num string formal error!\n");
200 | DestroyLinearListStack(stack);
201 | return 0;
202 | }
203 | num += my_pow(basenum,i)*item;
204 | }
205 | DestroyLinearListStack(stack);
206 | if(negative) num = -num;
207 | return num;
208 | }
209 |
210 | int main(void)
211 | {
212 | int num;
213 | char str[100];
214 | printf("please enter a num!\n");
215 | printf("num format just like these:\n");
216 | printf("HEX: -0X1F -0x1F 0x1F 0X1F\n");
217 | printf("DEC: -D11 -d11 D11 d11\n");
218 | printf("OCT: -O11 -o11 O11 o11\n");
219 | printf("BIN: -B10 -b10 B10 b10\n");
220 | printf("Enter:\n");
221 | gets(str);
222 | printf("\n");
223 | num = stringToInt(str);
224 | displayConvert(num);
225 | return 0;
226 | }
--------------------------------------------------------------------------------
/linear_list_stack/test_linear_list_stack.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearListStack.h"
4 |
5 | int strlen(char *str){
6 | int i = 0;
7 | while(*(str+i) != '\0'){
8 | i++;
9 | }
10 | return i;
11 | }
12 |
13 | int main(void)
14 | {
15 | int i;
16 | char string[] = "abcdefgh";
17 | int strlength = strlen(string);
18 | ElemType elem;
19 | LinearListStack *stack = InitLinearListStack();
20 | printf("string length = %d\n",strlength);
21 | printf("stack is empty:%d\n",stack->isEmpty(stack));
22 | for (i = 0; i < strlength; i++){
23 | stack->push(stack,string+i);
24 | }
25 | printf("base to top: \n");
26 | stack->risePrint(stack);
27 | printf("top to base: \n");
28 | stack->downPrint(stack);
29 | printf("stack is empty:%d\n",stack->isEmpty(stack));
30 | printf("stack length:%d\n",stack->length(stack));
31 | for(i=0;i < strlength; i++){
32 | stack->getTop(stack,&elem);
33 | printf("get top elem:%c\n",elem);
34 | stack->pop(stack,&elem);
35 | printf("pop elem:%c\n",elem);
36 | }
37 | printf("stack is empty:%d\n",stack->isEmpty(stack));
38 | printf("stack length:%d\n",stack->length(stack));
39 | stack->clear(stack);
40 | DestroyLinearListStack(stack);
41 | return 0;
42 | }
--------------------------------------------------------------------------------
/linear_list_stack/tower_of_hanoi.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "LinearListStack.h"
4 |
5 | void hanoi(int n,LinearListStack *x,LinearListStack *y,LinearListStack *z){
6 | char elem;
7 | if(n == 1){
8 | x->pop(x,&elem);
9 | z->push(z,&elem); //将编号为1的圆盘从x移到z
10 | //******************************打印步骤需要,非执行过程*********************
11 | printf("move %c from %c to %c\n",elem,*(x->This->base),*(z->This->base));
12 | x->risePrint(x);
13 | y->risePrint(y);
14 | z->risePrint(z);
15 | printf("\n");
16 | //***************************************************************************
17 | }else{
18 | hanoi(n-1,x,z,y);//将x上编号为1至n-1的圆盘移到y,z做辅助塔
19 | x->pop(x,&elem);
20 | z->push(z,&elem);//将编号为n的圆盘从x移到z
21 | //******************************打印步骤需要,非执行过程*********************
22 | printf("move %c from %c to %c\n",elem,*(x->This->base),*(z->This->base));
23 | x->risePrint(x);
24 | y->risePrint(y);
25 | z->risePrint(z);
26 | printf("\n");
27 | //***************************************************************************
28 | hanoi(n-1,y,x,z);//将y上编号为1至n-1的圆盘移到z,x做辅助塔
29 | }
30 | }
31 |
32 | int main(void)
33 | {
34 | int i;
35 | char elem;
36 | LinearListStack *x = InitLinearListStack();
37 | LinearListStack *y = InitLinearListStack();
38 | LinearListStack *z = InitLinearListStack();
39 | elem = 'x';
40 | x->push(x,&elem);
41 | elem = ':';
42 | x->push(x,&elem);
43 | elem = 'y';
44 | y->push(y,&elem);
45 | elem = ':';
46 | y->push(y,&elem);
47 | elem = 'z';
48 | z->push(z,&elem);
49 | elem = ':';
50 | z->push(z,&elem);
51 | for(i=9;i>0;i--){
52 | elem = i+0x30;
53 | x->push(x,&elem);
54 | }
55 | hanoi(9,x,y,z);
56 | DestroyLinearListStack(x);
57 | DestroyLinearListStack(y);
58 | DestroyLinearListStack(z);
59 | return 0;
60 | }
--------------------------------------------------------------------------------
/matrix/matrix.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _MATRIX_H
3 | #define _MATRIX_H
4 | /* Exported types ------------------------------------------------------------*/
5 |
6 | typedef struct Dshape{
7 | int shape[4]; //最多四维
8 | }Dshape;
9 |
10 | typedef struct Matrix{
11 | double *array;
12 | Dshape dshape; //数组结构
13 | int length; //长度
14 | int size; //空间大小
15 | }Matrix;
16 |
17 | typedef struct MatrixResult{
18 | double *array;
19 | int length; //长度
20 | }MatrixResult;
21 |
22 | Matrix *creatAsMatrixFromDatas(double *data,int data_len, Dshape dshape);
23 | Matrix *creatMatrixFromDatas(double *data,int data_len, Dshape dshape);
24 | Matrix *creatMatrixFromValue(double value, Dshape dshape);
25 | Matrix *creatMatrixFromArange(double startVal, double stepVal,Dshape dshape);
26 | Matrix *creatMatrixFromLinspace(double startVal, double endVal,Dshape dshape);
27 | Matrix *creatZerosMatrix(Dshape dshape);
28 | Matrix *creatOnesMatrix(Dshape dshape);
29 | Matrix *creatIdentitySecondOrderMatrix(Dshape dshape);
30 | void initDshape(Dshape *dshape,int *shapeval);
31 | int reshape(Matrix *m,Dshape dshape);
32 | void clearMatrix(Matrix *m);
33 | void printShape(Matrix *m);
34 | int getMatrixNdim(Matrix *m);
35 | void printarray(Matrix *m);
36 | double getSecondOrderMatrixTrace(Matrix *m);
37 | int getMatrixElem(Matrix *m,int dimen0,int dimen1,int dimen2,int dimen3,double *elem);
38 | int modifyMatrixElem(Matrix *m,int dimen0,int dimen1,int dimen2,int dimen3,double elem);
39 | Matrix *copyMatrix(Matrix *m);
40 | int compareMatrix(Matrix *m1, Matrix *m2);
41 | Matrix *getSecondOrderMatrixRows(Matrix *m,int startRow,int endRow);
42 | Matrix *getSecondOrderMatrixColumes(Matrix *m,int startColume,int endColume);
43 | Matrix *getSecondOrderSubMatrix(Matrix *m,int startRow,int startColume,int endRow,int endColume);
44 | Matrix *getSecondOrderLeftSubMatrix(Matrix *m,int row,int colume);
45 | int transposeSecondOrderMatrix(Matrix *m);
46 | int isSymmetricMatrix(Matrix *m);
47 | int swapSecondOrderMatrixRow(Matrix *m, int row1,int row2);
48 | int swapSecondOrderMatrixColume(Matrix *m, int colume1,int colume2);
49 | int kAddSecondOrderMatrixRow(Matrix *m, int row,double k);
50 | int kSubSecondOrderMatrixRow(Matrix *m, int row,double k);
51 | int kMulSecondOrderMatrixRow(Matrix *m, int row,double k);
52 | int kDivSecondOrderMatrixRow(Matrix *m, int row,double k);
53 | int addSecondOrderMatrixRows(Matrix *m, int row1,int row2);
54 | int subSecondOrderMatrixRows(Matrix *m, int row1,int row2);
55 | int mulSecondOrderMatrixRows(Matrix *m, int row1,int row2);
56 | int divSecondOrderMatrixRows(Matrix *m, int row1,int row2);
57 | int kAddSecondOrderMatrixColume(Matrix *m, int colume,double k);
58 | int kSubSecondOrderMatrixColume(Matrix *m, int colume,double k);
59 | int kMulSecondOrderMatrixColume(Matrix *m, int colume,double k);
60 | int kDivSecondOrderMatrixColume(Matrix *m, int colume,double k);
61 | int addSecondOrderMatrixColumes(Matrix *m, int colume1, int colume2);
62 | int subSecondOrderMatrixColumes(Matrix *m, int colume1, int colume2);
63 | int mulSecondOrderMatrixColumes(Matrix *m, int colume1, int colume2);
64 | int divSecondOrderMatrixColumes(Matrix *m, int colume1, int colume2);
65 | int deleteSecondOrderMatrixRows(Matrix *m,int startRow,int endRow);
66 | int deleteSecondOrderMatrixColumes(Matrix *m,int startColume,int endColume);
67 | int deleteSecondOrderMatrixRowAndColume(Matrix *m,int row,int colume);
68 | int spliceSecondOrderMatrixRow(Matrix *m1,Matrix *m2);
69 | int spliceSecondOrderMatrixColume(Matrix *m1,Matrix *m2);
70 | int kAddMatrix(Matrix *m,double k);
71 | int kSubMatrix(Matrix *m,double k);
72 | int kMulMatrix(Matrix *m,double k);
73 | int kDivMatrix(Matrix *m,double k);
74 | Matrix *addSecondOrderMatrixs(Matrix *m1,Matrix *m2);
75 | Matrix *subSecondOrderMatrixs(Matrix *m1,Matrix *m2);
76 | Matrix *dotSecondOrderMatrixs(Matrix *m1,Matrix *m2);
77 | Matrix *mulSecondOrderMatrixs(Matrix *m1,Matrix *m2);
78 | int detSquareMatrixs(Matrix *m,double *result);
79 | int getSquareMatrixElemAlgebraicComplement(Matrix *m,int row,int colume,double *result);
80 | Matrix *getSquareMatrixRawAlgebraicComplement(Matrix *m,int row);
81 | Matrix *getSquareMatrixAdjointMatrix(Matrix *m);
82 | Matrix *invSquareMatrix(Matrix *m);
83 | Matrix *getEchelonMatrix(Matrix *m);
84 | int getSecondOrderMatrixRank(Matrix *m ,int *rank);
85 | Matrix *solveHomoLinearEquations(Matrix *A);
86 | Matrix *solveNonHomoLinearEquations(Matrix *A, Matrix *B);
87 | Matrix *getSymmetricMatrixEigen(Matrix *m);
88 | double getMatrixInfNorm(Matrix *m);
89 | double getMatrixL0Norm(Matrix *m);
90 | double getMatrixL1Norm(Matrix *m);
91 | double getMatrixL2Norm(Matrix *m);
92 | double getMatrixL21Norm(Matrix *m);
93 | void destroyMatrix(Matrix *m);
94 |
95 | #endif
--------------------------------------------------------------------------------
/matrix/test_matrix.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Matrix.h"
4 |
5 | int main(void){
6 | Matrix *m = NULL;
7 | Matrix *m2 = NULL;
8 | Matrix *m3 = NULL;
9 | int rank = 0;
10 | double norm = 0;
11 | int a[]={0,0,3,3};
12 | int b[]={0,0,4,1};
13 |
14 | double data[] = {1,1,1,1,2,10,1,10,100};
15 | double data2[] = {3,2,-1,5};
16 | double elem;
17 | Dshape dshape;
18 | initDshape(&dshape,a);
19 |
20 | m = creatMatrixFromDatas(data,9,dshape);
21 | printarray(m);
22 | printf("\n");
23 |
24 | m2 = getSymmetricMatrixEigen(m);
25 | printarray(m2);
26 | printf("\n");
27 |
28 | destroyMatrix(m);
29 | destroyMatrix(m2);
30 | destroyMatrix(m3);
31 | return 0;
32 | }
33 |
--------------------------------------------------------------------------------
/polynomial/polynomial.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Polynomial.h"
4 |
5 | static void clear(Polynomial *This);
6 | static int isEmpty(Polynomial *This);
7 | static int length(Polynomial *This);
8 | static void print(Polynomial *This);
9 | static int appendElem(Polynomial *This, ElemType e);
10 |
11 | Polynomial *InitPolynomial(){
12 | Polynomial *L = (Polynomial *)malloc(sizeof(Polynomial));
13 | Node *p = (Node *)malloc(sizeof(Node));
14 | L->This = p;
15 | p->next = NULL;
16 | L->clear = clear;
17 | L->isEmpty = isEmpty;
18 | L->length = length;
19 | L->print = print;
20 | L->appendElem = appendElem;
21 | return L;
22 | }
23 |
24 | Polynomial *CreatePolynomial(ElemType *params,int length){
25 | Polynomial *L = InitPolynomial();
26 | int i;
27 | for(i=0;iappendElem(L, *(params+i));
29 | }
30 | return L;
31 | }
32 |
33 | void DestroyPolynomial(Polynomial *L){
34 | L->clear(L);
35 | free(L->This);
36 | free(L);
37 | L = NULL;
38 | }
39 |
40 | static void clear(Polynomial *This){
41 | Node *p = This->This->next;
42 | Node *temp = NULL;
43 | while(p){
44 | temp = p;
45 | p = p->next;
46 | free(temp);
47 | }
48 | p = This->This;
49 | p->next = NULL;
50 | }
51 |
52 | static int isEmpty(Polynomial *This){
53 | Node *p = This->This;
54 | if(p->next){
55 | return 0;
56 | }else{
57 | return 1;
58 | }
59 | }
60 |
61 | static int length(Polynomial *This){
62 | int j = 0;
63 | Node *p = This->This->next;
64 | while(p){
65 | j++;
66 | p = p->next;
67 | }
68 | return j;
69 | }
70 |
71 | static void print(Polynomial *This){
72 | Node *p = This->This->next;
73 | if(p){
74 | printf("%fx^%f", p->elem.coefficient,p->elem.exponent);
75 | p = p->next;
76 | }
77 | while(p){
78 | printf(" + %fx^%f", p->elem.coefficient,p->elem.exponent);
79 | p = p->next;
80 | }
81 | printf("\n");
82 | }
83 |
84 | static int appendElem(Polynomial *This, ElemType e){
85 | Node *p = This->This;
86 | Node *temp = (Node *)malloc(sizeof(Node));
87 | if(!temp) return -1;
88 | while(p){
89 | if(NULL == p->next){
90 | temp->elem.coefficient = e.coefficient;
91 | temp->elem.exponent = e.exponent;
92 | p->next = temp;
93 | temp->next = NULL;
94 | }
95 | p = p->next;
96 | }
97 | return 0;
98 | }
99 |
100 | Polynomial *addPolynomial(Polynomial *pa,Polynomial *pb){
101 | Polynomial *L = InitPolynomial();
102 | ElemType a,b,sum;
103 | Node *ha = pa->This->next;
104 | Node *hb = pb->This->next;
105 | while(ha&&hb){
106 | a = ha->elem;
107 | b = hb->elem;
108 | if(a.exponent > b.exponent){
109 | L->appendElem(L, b);
110 | hb = hb->next;
111 | }else if(a.exponent == b.exponent){
112 | sum.exponent = a.exponent;
113 | sum.coefficient = a.coefficient + b.coefficient;
114 | if(sum.coefficient != 0){
115 | L->appendElem(L, sum);
116 | }
117 | ha = ha->next;
118 | hb = hb->next;
119 | }else{
120 | L->appendElem(L, a);
121 | ha = ha->next;
122 | }
123 | }
124 | while(ha){
125 | a = ha->elem;
126 | L->appendElem(L, a);
127 | ha = ha->next;
128 | }
129 | while(hb){
130 | b = hb->elem;
131 | L->appendElem(L, b);
132 | hb = hb->next;
133 | }
134 | return L;
135 | }
136 |
137 | Polynomial *subPolynomial(Polynomial *pa,Polynomial *pb){
138 | Polynomial *L = InitPolynomial();
139 | ElemType a,b,sub;
140 | Node *ha = pa->This->next;
141 | Node *hb = pb->This->next;
142 | while(ha&&hb){
143 | a = ha->elem;
144 | b = hb->elem;
145 | if(a.exponent > b.exponent){
146 | sub.exponent = b.exponent;
147 | sub.coefficient = -b.coefficient;
148 | L->appendElem(L, sub);
149 | hb = hb->next;
150 | }else if(a.exponent == b.exponent){
151 | sub.exponent = a.exponent;
152 | sub.coefficient = a.coefficient - b.coefficient;
153 | if(sub.coefficient != 0){
154 | L->appendElem(L, sub);
155 | }
156 | ha = ha->next;
157 | hb = hb->next;
158 | }else{
159 | L->appendElem(L, a);
160 | ha = ha->next;
161 | }
162 | }
163 | while(ha){
164 | a = ha->elem;
165 | L->appendElem(L, a);
166 | ha = ha->next;
167 | }
168 | while(hb){
169 | b = hb->elem;
170 | sub.exponent = b.exponent;
171 | sub.coefficient = -b.coefficient;
172 | L->appendElem(L, sub);
173 | hb = hb->next;
174 | }
175 | return L;
176 | }
177 |
178 | Polynomial *kMulPolynomial(Polynomial *pa,ElemType a){
179 | Polynomial *L = InitPolynomial();
180 | Node *ha = pa->This->next;
181 | ElemType temp;
182 | while(ha){
183 | temp.exponent = ha->elem.exponent + a.exponent;
184 | temp.coefficient = ha->elem.coefficient * a.coefficient;
185 | L->appendElem(L, temp);
186 | ha = ha->next;
187 | }
188 | return L;
189 | }
190 |
191 | Polynomial *mulPolynomial(Polynomial *pa,Polynomial *pb){
192 | Polynomial *temp = InitPolynomial();
193 | Polynomial *temp1 = NULL,*temp2 = NULL;
194 | Node *hb = pb->This->next;
195 | while(hb){
196 | temp1 = kMulPolynomial(pa,hb->elem);
197 | temp2 = addPolynomial(temp1,temp);
198 | DestroyPolynomial(temp1);
199 | DestroyPolynomial(temp);
200 | temp = temp2;
201 | hb = hb->next;
202 | }
203 | return temp;
204 | }
205 |
--------------------------------------------------------------------------------
/polynomial/polynomial.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __POLYNOMIAL_H
3 | #define __POLYNOMIAL_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef struct ElemType{
7 | double coefficient; //系数
8 | double exponent;//指数
9 | }ElemType;
10 |
11 | typedef struct Node{
12 | ElemType elem; //存储空间
13 | struct Node *next;
14 | }Node;
15 |
16 | typedef struct Polynomial{
17 | Node *This;
18 | void (*clear)(struct Polynomial *This);
19 | int (*isEmpty)(struct Polynomial *This);
20 | int (*length)(struct Polynomial *This);
21 | void (*print)(struct Polynomial *This);
22 | int (*appendElem)(struct Polynomial *This, ElemType e);
23 | }Polynomial;
24 |
25 | /* Exported macro ------------------------------------------------------------*/
26 | Polynomial *CreatePolynomial(ElemType *params,int length);
27 | void DestroyPolynomial(Polynomial *L);
28 | Polynomial *addPolynomial(Polynomial *pa,Polynomial *pb);
29 | Polynomial *subPolynomial(Polynomial *pa,Polynomial *pb);
30 | Polynomial *kMulPolynomial(Polynomial *pa,ElemType a);
31 | Polynomial *mulPolynomial(Polynomial *pa,Polynomial *pb);
32 |
33 | #endif
--------------------------------------------------------------------------------
/polynomial/test_polynomial.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Polynomial.h"
4 |
5 | int main(void){
6 | //7+3x+9X^8+5x^17
7 | ElemType params_a[4]={{7,0},{3,1},{9,8},{5,17}};
8 | //8x+22x^7+-9x^8
9 | ElemType params_b[3]={{8,1},{22,7},{-9,8}};
10 | Polynomial *pa = CreatePolynomial(params_a,4);
11 | Polynomial *pb = CreatePolynomial(params_b,3);
12 | Polynomial *sum_ab = NULL;,*sub_ab = NULL;,*mul_ab = NULL;,*kmul_a = NULL;;
13 | printf("pa = ");
14 | pa->print(pa);
15 | printf("pb = ");
16 | pb->print(pb);
17 | sum_ab = addPolynomial(pa,pb);
18 | printf("pa + pb = ");
19 | sum_ab->print(sum_ab);
20 | sub_ab = subPolynomial(pa,pb);
21 | printf("pa - pb = ");
22 | sub_ab->print(sub_ab);
23 | mul_ab = mulPolynomial(pa,pb);
24 | printf("pa * pb = ");
25 | mul_ab->print(mul_ab);
26 | kmul_a = kMulPolynomial(pa,params_b[0]);
27 | printf("pa * 8x = ");
28 | kmul_a->print(kmul_a);
29 | DestroyPolynomial(pa);
30 | DestroyPolynomial(pb);
31 | DestroyPolynomial(sum_ab);
32 | DestroyPolynomial(mul_ab);
33 | DestroyPolynomial(kmul_a);
34 | return 0;
35 | }
--------------------------------------------------------------------------------
/single_circular_linked_list/single_circular_linked_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SingleCircularLinkedList.h"
4 |
5 | static void clear(SingleCircularLinkedList *This);
6 | static int isEmpty(SingleCircularLinkedList *This);
7 | static int length(SingleCircularLinkedList *This);
8 | static void print(SingleCircularLinkedList *This);
9 | static void circlePrint(SingleCircularLinkedList *This,int times);
10 | static int indexElem(SingleCircularLinkedList *This, ElemType* x);
11 | static int getElem(SingleCircularLinkedList *This, int index, ElemType *e);
12 | static int modifyElem(SingleCircularLinkedList *This, int index, ElemType* e);
13 | static int deleteElem(SingleCircularLinkedList *This, int index, ElemType* e);
14 | static int appendElem(SingleCircularLinkedList *This, ElemType *e);
15 | static int insertElem(SingleCircularLinkedList *This, int index, ElemType *e);
16 | static int popElem(SingleCircularLinkedList *This, ElemType* e);
17 |
18 | SingleCircularLinkedList *InitSingleCircularLinkedList(){
19 | SingleCircularLinkedList *L = (SingleCircularLinkedList *)malloc(sizeof(SingleCircularLinkedList));
20 | Node *p = (Node *)malloc(sizeof(Node));
21 | L->This = p;
22 | p->next = p;
23 | L->clear = clear;
24 | L->isEmpty = isEmpty;
25 | L->length = length;
26 | L->print = print;
27 | L->circlePrint = circlePrint;
28 | L->indexElem = indexElem;
29 | L->getElem = getElem;
30 | L->modifyElem = modifyElem;
31 | L->deleteElem = deleteElem;
32 | L->appendElem = appendElem;
33 | L->insertElem = insertElem;
34 | L->popElem = popElem;
35 | return L;
36 | }
37 |
38 | void DestroySingleCircularLinkedList(SingleCircularLinkedList *L){
39 | L->clear(L);
40 | free(L->This);
41 | free(L);
42 | L = NULL;
43 | }
44 |
45 | static void clear(SingleCircularLinkedList *This){
46 | Node *head = This->This;
47 | Node *p = This->This->next;
48 | Node *temp = NULL;
49 | while(p != head){
50 | temp = p;
51 | p = p->next;
52 | free(temp);
53 | }
54 | p->next = head;
55 | }
56 |
57 | static int isEmpty(SingleCircularLinkedList *This){
58 | Node *p = This->This;
59 | if(p->next == p){
60 | return 0;
61 | }else{
62 | return 1;
63 | }
64 | }
65 |
66 | static int length(SingleCircularLinkedList *This){
67 | int j = 0;
68 | Node *head = This->This;
69 | Node *p = This->This->next;
70 | while(p != head){
71 | j++;
72 | p = p->next;
73 | }
74 | return j;
75 | }
76 |
77 | static void print(SingleCircularLinkedList *This){
78 | Node *head = This->This;
79 | Node *p = This->This->next;
80 | while(p != head){
81 | printf("%d ", p->elem);
82 | p = p->next;
83 | }
84 | printf("\n");
85 | }
86 |
87 | static void circlePrint(SingleCircularLinkedList *This,int times){
88 | Node *head = This->This;
89 | int i = 0;
90 | Node *p = This->This->next;
91 | for(i = 0;ielem);
96 | }
97 | p = p->next;
98 | }
99 | printf("\n");
100 | }
101 |
102 | static int indexElem(SingleCircularLinkedList *This, ElemType* e){
103 | Node *head = This->This;
104 | Node *p = This->This->next;
105 | int pos = -1;
106 | int j = 0;
107 | while(p != head){
108 | if(*e == p->elem){
109 | pos = j;
110 | }
111 | p = p->next;
112 | j++;
113 | }
114 | return pos;
115 | }
116 |
117 | static int getElem(SingleCircularLinkedList *This, int index, ElemType *e){
118 | Node *head = This->This;
119 | Node *p = This->This->next;
120 | int j = 0;
121 | while(p != head && j < index){
122 | p = p->next;
123 | j++;
124 | }
125 | if(p == head || j > index) return -1;
126 | *e = p->elem;
127 | return 0;
128 | }
129 |
130 | static int modifyElem(SingleCircularLinkedList *This, int index, ElemType* e){
131 | Node *head = This->This;
132 | Node *p = This->This->next;
133 | int j = 0;
134 | while(p != head && j < index){
135 | p = p->next;
136 | j++;
137 | }
138 | if(p == head || j > index) return -1;
139 | p->elem = *e;
140 | return 0;
141 | }
142 |
143 | static int insertElem(SingleCircularLinkedList *This, int index, ElemType *e){
144 | Node *head = This->This;
145 | Node *p = This->This;
146 | int j = 0;
147 | Node *temp = (Node *)malloc(sizeof(Node));
148 | if(!temp) return -1;
149 | while(p->next != head && j < index){
150 | p = p->next;
151 | j++;
152 | }
153 | if(p->next == head || j > index) return -1;
154 | temp->elem = *e;
155 | temp->next = p->next;
156 | p->next = temp;
157 | return 0;
158 | }
159 |
160 | static int deleteElem(SingleCircularLinkedList *This, int index, ElemType* e){
161 | Node *head = This->This;
162 | Node *p = This->This;
163 | Node *temp = NULL;
164 | int j = 0;
165 | while(p->next != head && j < index){
166 | p = p->next;
167 | j++;
168 | }
169 | if(p->next == head || j > index) return -1;
170 | temp = p->next;
171 | p->next = temp->next;
172 | *e = temp->elem;
173 | free(temp);
174 | return 0;
175 | }
176 |
177 | static int appendElem(SingleCircularLinkedList *This, ElemType *e){
178 | Node *head = This->This;
179 | Node *p = This->This->next;
180 | Node *temp = (Node *)malloc(sizeof(Node));
181 | if(!temp) return -1;
182 | while(p->next != head){
183 | p = p->next;
184 | }
185 | temp->elem = *e;
186 | p->next = temp;
187 | temp->next = head;
188 | return 0;
189 | }
190 |
191 | static int popElem(SingleCircularLinkedList *This, ElemType* e){
192 | Node *head = This->This;
193 | Node *p = This->This;
194 | Node *temp = NULL;
195 | while(p->next->next != head){
196 | p = p->next;
197 | }
198 | temp = p->next;
199 | if(temp == head) return -1;
200 | *e = temp->elem;
201 | free(temp);
202 | p->next = head;
203 | return 0;
204 | }
205 |
206 |
--------------------------------------------------------------------------------
/single_circular_linked_list/single_circular_linked_list.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __SINGLECIRCULARLINKEDLIST_H
3 | #define __SINGLECIRCULARLINKEDLIST_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef int ElemType; //数据元素的类型,假设是int型的
7 |
8 | typedef struct Node{
9 | ElemType elem; //存储空间
10 | struct Node *next;
11 | }Node;
12 |
13 | typedef struct SingleCircularLinkedList{
14 | Node *This;
15 | void (*clear)(struct SingleCircularLinkedList *This);
16 | int (*isEmpty)(struct SingleCircularLinkedList *This);
17 | int (*length)(struct SingleCircularLinkedList *This);
18 | void (*print)(struct SingleCircularLinkedList *This);
19 | void (*circlePrint)(struct SingleCircularLinkedList *This,int times);
20 | int (*indexElem)(struct SingleCircularLinkedList *This, ElemType* x);
21 | int (*getElem)(struct SingleCircularLinkedList *This, int index, ElemType *e);
22 | int (*modifyElem)(struct SingleCircularLinkedList *This, int index, ElemType* e);
23 | int (*deleteElem)(struct SingleCircularLinkedList *This, int index, ElemType* e);
24 | int (*appendElem)(struct SingleCircularLinkedList *This, ElemType *e);
25 | int (*insertElem)(struct SingleCircularLinkedList *This, int index, ElemType *e);
26 | int (*popElem)(struct SingleCircularLinkedList *This, ElemType* e);
27 | }SingleCircularLinkedList;
28 |
29 | /* Exported macro ------------------------------------------------------------*/
30 | SingleCircularLinkedList *InitSingleCircularLinkedList();
31 | void DestroySingleCircularLinkedList(SingleCircularLinkedList *L);
32 |
33 | #endif
--------------------------------------------------------------------------------
/single_circular_linked_list/test_single_circular_linked_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SingleCircularLinkedList.h"
4 |
5 | int main(void){
6 | int i;
7 | ElemType elem,elem1;
8 | SingleCircularLinkedList *list = InitSingleCircularLinkedList();
9 | printf("list is empty:%d\n",list->isEmpty(list));
10 | for(i=0;i<10;i++){
11 | list->appendElem(list,&i);
12 | }
13 | list->print(list);
14 | printf("list is empty:%d\n",list->isEmpty(list));
15 | printf("list length:%d\n",list->length(list));
16 | list->clear(list);
17 | for (i = 10; i < 20; i++){
18 | list->appendElem(list,&i);
19 | }
20 | list->print(list);
21 | list->getElem(list,3,&elem1);
22 | printf("the elem of index 3 is %d\n",elem1);
23 | elem = 31;
24 | list->modifyElem(list,3,&elem);
25 | list->getElem(list,3,&elem1);
26 | printf("modify the elem of index 3 to %d\n",elem1);
27 | list->print(list);
28 | elem = 25;
29 | list->insertElem(list,5,&elem);
30 | printf("insert elem %d to index 5\n",elem);
31 | list->print(list);
32 | list->deleteElem(list,7,&elem);
33 | printf("delete elem %d of index 7\n",elem);
34 | list->print(list);
35 | elem = 14;
36 | printf("the index of 14 is %d\n",list->indexElem(list,&elem));
37 | list->popElem(list,&elem);
38 | printf("pop elem %d\n",elem);
39 | list->print(list);
40 | printf("circle print 3 times:\n");
41 | list->circlePrint(list,3);
42 | DestroySingleCircularLinkedList(list);
43 | return 0;
44 | }
--------------------------------------------------------------------------------
/single_circular_linked_list_queue/single_circular_linked_list_queue.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SingleCircularLinkedListQueue.h"
4 |
5 | static void clear(SingleCircularLinkedListQueue *This);
6 | static int isEmpty(SingleCircularLinkedListQueue *This);
7 | static int length(SingleCircularLinkedListQueue *This);
8 | static QNode *getHead(SingleCircularLinkedListQueue *This);
9 | static int enQueue(SingleCircularLinkedListQueue *This,QNode *n);
10 | static int deQueue(SingleCircularLinkedListQueue *This,QNode *n);
11 | static int traverse(SingleCircularLinkedListQueue *This,int (*visit)(QNode *n),int circular);
12 |
13 | SingleCircularLinkedListQueue *InitSingleCircularLinkedListQueue(){
14 | SingleCircularLinkedListQueue *Q = (SingleCircularLinkedListQueue *)malloc(sizeof(SingleCircularLinkedListQueue));
15 | QNode *p = (QNode *)malloc(sizeof(QNode));
16 | Q->This = p;
17 | Q->front = p;
18 | Q->tear = Q->front;
19 | p->next = p;
20 | Q->clear = clear;
21 | Q->isEmpty = isEmpty;
22 | Q->length = length;
23 | Q->getHead = getHead;
24 | Q->enQueue = enQueue;
25 | Q->deQueue = deQueue;
26 | Q->traverse = traverse;
27 | return Q;
28 | }
29 |
30 | void DestroySingleCircularLinkedListQueue(SingleCircularLinkedListQueue *Q){
31 | Q->clear(Q);
32 | free(Q->This);
33 | free(Q);
34 | Q = NULL;
35 | }
36 |
37 | static void clear(SingleCircularLinkedListQueue *This){
38 | QNode *head = This->This;
39 | QNode *p = This->This->next;
40 | QNode *temp = NULL;
41 | while(p != head){
42 | temp = p;
43 | p = p->next;
44 | free(temp);
45 | }
46 | p = This->This;
47 | p->next = head;
48 | This->front = p;
49 | This->tear = This->front;
50 | }
51 |
52 | static int isEmpty(SingleCircularLinkedListQueue *This){
53 | QNode *p = This->This;
54 | if(p->next == p){
55 | return 0;
56 | }else{
57 | return 1;
58 | }
59 | }
60 |
61 | static int length(SingleCircularLinkedListQueue *This){
62 | int j = 0;
63 | QNode *head = This->This;
64 | QNode *p = This->This->next;
65 | while(p != head){
66 | j++;
67 | p = p->next;
68 | }
69 | return j;
70 | }
71 |
72 | static QNode *getHead(SingleCircularLinkedListQueue *This){
73 | return This->front->next;
74 | }
75 |
76 | static int enQueue(SingleCircularLinkedListQueue *This,QNode *n){
77 | QNode *head = This->This;
78 | if(!n) return -1;
79 | This->tear->next = n;
80 | n->next = head;
81 | This->tear = n;
82 | return 0;
83 | }
84 |
85 | static int deQueue(SingleCircularLinkedListQueue *This,QNode *n){
86 | if(This->front == This->tear){
87 | n = NULL;
88 | return -1;
89 | }
90 | QNode *temp = This->front->next;
91 | *n = *(temp);
92 | This->front->next = temp->next;
93 | if(This->tear == temp) This->tear = This->front;
94 | free(temp);
95 | return 0;
96 | }
97 |
98 | static int traverse(SingleCircularLinkedListQueue *This,int (*visit)(QNode *n),int circular){
99 | if(This->front == This->tear){
100 | return -1;
101 | }
102 | QNode *head = This->front;
103 | QNode *temp = This->front->next;
104 | if(circular){
105 | while(temp){
106 | if(temp != head){
107 | if(visit(temp) != 0) break;
108 | }
109 | temp = temp->next;
110 | }
111 | }else{
112 | while(temp != head){
113 | if(visit(temp) != 0) break;
114 | temp = temp->next;
115 | }
116 | }
117 | return 0;
118 | }
--------------------------------------------------------------------------------
/single_circular_linked_list_queue/single_circular_linked_list_queue.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _SINGLECIRCULARLINKEDLISTQUEUE_H
3 | #define _SINGLECIRCULARLINKEDLISTQUEUE_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef struct QElemType{
7 | int id;
8 | char name[20];
9 | }QElemType;
10 |
11 | typedef struct QNode{
12 | QElemType elem; //存储空间
13 | struct QNode *next;
14 | }QNode,*Queueptr;
15 |
16 | typedef struct SingleCircularLinkedListQueue{
17 | QNode *This;
18 | Queueptr front; //队头
19 | Queueptr tear; //队尾
20 | void (*clear)(struct SingleCircularLinkedListQueue *This);
21 | int (*isEmpty)(struct SingleCircularLinkedListQueue *This);
22 | int (*length)(struct SingleCircularLinkedListQueue *This);
23 | QNode *(*getHead)(struct SingleCircularLinkedListQueue *This);
24 | int (*enQueue)(struct SingleCircularLinkedListQueue *This,QNode *n);
25 | int (*deQueue)(struct SingleCircularLinkedListQueue *This,QNode *n);
26 | int (*traverse)(struct SingleCircularLinkedListQueue *This,int (*visit)(QNode *n),int circular);
27 | }SingleCircularLinkedListQueue;
28 |
29 | /* Exported macro ------------------------------------------------------------*/
30 | SingleCircularLinkedListQueue *InitSingleCircularLinkedListQueue();
31 | void DestroySingleCircularLinkedListQueue(SingleCircularLinkedListQueue *Q);
32 |
33 | #endif
--------------------------------------------------------------------------------
/single_circular_linked_list_queue/test_single_circular_linked_list_queue.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SingleCircularLinkedListQueue.h"
4 |
5 | char name[][3] = {"xw","xh","xm","xg","xl","xz"};
6 |
7 | void strCopy(char *str_a,char *str_b){
8 | while(*str_b != '\0'){
9 | *str_a++ = *str_b++;
10 | }
11 | *str_a = '\0';
12 | }
13 |
14 | int printQnode(QNode *node){
15 | printf("id:%d,name:%s\n",node->elem.id,node->elem.name);
16 | return 0;
17 | }
18 |
19 | int main(void){
20 | int i;
21 | QNode *node = NULL;
22 | SingleCircularLinkedListQueue *queue = InitSingleCircularLinkedListQueue();
23 | printf("queue is empty:%d\n",queue->isEmpty(queue));
24 | for(i=0;i<6;i++){
25 | node = (QNode *)malloc(sizeof(QNode));
26 | node->elem.id = i;
27 | strCopy(node->elem.name,name[i]);
28 | queue->enQueue(queue,node);
29 | }
30 | queue->traverse(queue,printQnode,0);
31 | printf("queue is empty:%d\n",queue->isEmpty(queue));
32 | printf("queue length:%d\n",queue->length(queue));
33 | while(queue->length(queue)){
34 | node = queue->getHead(queue);
35 | printf("present client: id=%d, name=%s\n",node->elem.id,node->elem.name);
36 | node = (QNode *)malloc(sizeof(QNode));
37 | queue->deQueue(queue,node);
38 | printf("client :id=%d,name=%s finish!\n",node->elem.id,node->elem.name);
39 | free(node);
40 | node = NULL;
41 | }
42 | queue->clear(queue);
43 | for (i = 10; i < 16; i++){
44 | node = (QNode *)malloc(sizeof(QNode));
45 | node->elem.id = i;
46 | strCopy(node->elem.name,name[i-10]);
47 | queue->enQueue(queue,node);
48 | }
49 | queue->traverse(queue,printQnode,1);
50 | DestroySingleCircularLinkedListQueue(queue);
51 | return 0;
52 | }
--------------------------------------------------------------------------------
/single_linked_list/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.14)
2 | project(SingleLinkedList)
3 |
4 | set(CMAKE_CXX_STANDARD 14)
5 |
6 | add_executable(SingleLinkedList main.c SingleLinkedList.c)
--------------------------------------------------------------------------------
/single_linked_list/single_linked_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SingleLinkedList.h"
4 |
5 | static void clear(SingleLinkedList *This);
6 | static int isEmpty(SingleLinkedList *This);
7 | static int length(SingleLinkedList *This);
8 | static void print(SingleLinkedList *This);
9 | static int indexElem(SingleLinkedList *This, ElemType* x);
10 | static int getElem(SingleLinkedList *This, int index, ElemType *e);
11 | static int modifyElem(SingleLinkedList *This, int index, ElemType* e);
12 | static int deleteElem(SingleLinkedList *This, int index, ElemType* e);
13 | static int appendElem(SingleLinkedList *This, ElemType *e);
14 | static int insertElem(SingleLinkedList *This, int index, ElemType *e);
15 | static int popElem(SingleLinkedList *This, ElemType* e);
16 | static int reverseList1(SingleLinkedList *This);
17 | static int reverseList2(SingleLinkedList *This);
18 |
19 | SingleLinkedList *InitSingleLinkedList(){
20 | SingleLinkedList *L = (SingleLinkedList *)malloc(sizeof(SingleLinkedList));
21 | Node *p = (Node *)malloc(sizeof(Node));
22 | L->This = p;
23 | p->next = NULL;
24 | L->clear = clear;
25 | L->isEmpty = isEmpty;
26 | L->length = length;
27 | L->print = print;
28 | L->indexElem = indexElem;
29 | L->getElem = getElem;
30 | L->modifyElem = modifyElem;
31 | L->deleteElem = deleteElem;
32 | L->appendElem = appendElem;
33 | L->insertElem = insertElem;
34 | L->popElem = popElem;
35 | L->reverseList1 = reverseList1;
36 | L->reverseList2 = reverseList2;
37 | return L;
38 | }
39 |
40 | void DestroySingleLinkedList(SingleLinkedList *L){
41 | L->clear(L);
42 | free(L->This);
43 | free(L);
44 | L = NULL;
45 | }
46 |
47 | static void clear(SingleLinkedList *This){
48 | Node *p = This->This->next;
49 | Node *temp = NULL;
50 | while(p){
51 | temp = p;
52 | p = p->next;
53 | free(temp);
54 | }
55 | p = This->This;
56 | p->next = NULL;
57 | }
58 |
59 | static int isEmpty(SingleLinkedList *This){
60 | Node *p = This->This;
61 | if(p->next){
62 | return 0;
63 | }else{
64 | return 1;
65 | }
66 | }
67 |
68 | static int length(SingleLinkedList *This){
69 | int j = 0;
70 | Node *p = This->This->next;
71 | while(p){
72 | j++;
73 | p = p->next;
74 | }
75 | return j;
76 | }
77 |
78 | static void print(SingleLinkedList *This){
79 | Node *p = This->This->next;
80 | while(p){
81 | printf("%d ", p->elem);
82 | p = p->next;
83 | }
84 | printf("\n");
85 | }
86 |
87 | static int indexElem(SingleLinkedList *This, ElemType* e){
88 | Node *p = This->This->next;
89 | int pos = -1;
90 | int j = 0;
91 | while(p){
92 | if(*e == p->elem){
93 | pos = j;
94 | }
95 | p = p->next;
96 | j++;
97 | }
98 | return pos;
99 | }
100 |
101 | static int getElem(SingleLinkedList *This, int index, ElemType *e){
102 | Node *p = This->This->next;
103 | int j = 0;
104 | while(p && j < index){
105 | p = p->next;
106 | j++;
107 | }
108 | if(!p || j > index) return -1;
109 | *e = p->elem;
110 | return 0;
111 | }
112 |
113 | static int modifyElem(SingleLinkedList *This, int index, ElemType* e){
114 | Node *p = This->This->next;
115 | int j = 0;
116 | while(p && j < index){
117 | p = p->next;
118 | j++;
119 | }
120 | if(!p || j > index) return -1;
121 | p->elem = *e;
122 | return 0;
123 | }
124 |
125 | static int insertElem(SingleLinkedList *This, int index, ElemType *e){
126 | Node *p = This->This;
127 | int j = 0;
128 | Node *temp = (Node *)malloc(sizeof(Node));
129 | if(!temp) return -1;
130 | while(p && j < index){
131 | p = p->next;
132 | j++;
133 | }
134 | if(!p || j > index) return -1;
135 | temp->elem = *e;
136 | temp->next = p->next;
137 | p->next = temp;
138 | return 0;
139 | }
140 |
141 | static int deleteElem(SingleLinkedList *This, int index, ElemType* e){
142 | Node *p = This->This;
143 | Node *temp = NULL;
144 | int j = 0;
145 | while(p->next && j < index){
146 | p = p->next;
147 | j++;
148 | }
149 | if(!p->next || j > index) return -1;
150 | temp = p->next;
151 | p->next = temp->next;
152 | *e = temp->elem;
153 | free(temp);
154 | return 0;
155 | }
156 |
157 | static int appendElem(SingleLinkedList *This, ElemType *e){
158 | Node *p = This->This;
159 | Node *temp = (Node *)malloc(sizeof(Node));
160 | if(!temp) return -1;
161 | while(p){
162 | if(NULL == p->next){
163 | temp->elem = *e;
164 | p->next = temp;
165 | temp->next = NULL;
166 | }
167 | p = p->next;
168 | }
169 | return 0;
170 | }
171 |
172 | static int popElem(SingleLinkedList *This, ElemType* e){
173 | Node *p = This->This;
174 | Node *temp = NULL;
175 | while(p->next->next){
176 | p = p->next;
177 | }
178 | temp = p->next;
179 | if(!temp) return -1;
180 | *e = temp->elem;
181 | free(temp);
182 | p->next = NULL;
183 | return 0;
184 | }
185 |
186 | //迭代方式
187 | static int reverseList1(SingleLinkedList *This){
188 | Node *p = This->This->next;
189 | if (p == NULL || p->next == NULL) return -1;
190 | Node *temp = NULL, *newH = NULL;
191 | while (p != NULL){
192 | temp = p->next;
193 | p->next = newH;
194 | newH = p;
195 | p = temp;
196 | }
197 | This->This->next = newH;
198 | return 0;
199 | }
200 |
201 | //递归方式
202 | static Node *ReverseList2(Node *head){
203 | if (head == NULL || head->next == NULL){
204 | return head;
205 | }else{
206 | Node *temp = head->next, *newH = NULL;
207 | head->next = NULL;
208 | newH = ReverseList2(temp);
209 | temp->next = head;
210 | return newH;
211 | }
212 | }
213 |
214 | static int reverseList2(SingleLinkedList *This){
215 | Node *p = This->This->next;
216 | if (p == NULL || p->next == NULL) return -1;
217 | This->This->next = ReverseList2(p);
218 | return 0;
219 | }
--------------------------------------------------------------------------------
/single_linked_list/single_linked_list.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef __SINGLELINKEDLIST_H
3 | #define __SINGLELINKEDLIST_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef int single_linked_list_elem_type; //数据元素的类型,假设是int型的
7 |
8 | struct single_linked_list_node{
9 | struct single_linked_list_node *next;
10 | single_linked_list_elem_type elem; //存储空间
11 | };
12 |
13 | typedef struct single_linked_list{
14 | struct single_linked_list_node *this;
15 | void (*clear)(struct single_linked_list *this);
16 | int (*isEmpty)(struct single_linked_list *this);
17 | int (*length)(struct single_linked_list *this);
18 | void (*print)(struct single_linked_list *this);
19 | int (*indexElem)(struct single_linked_list *this, single_linked_list_elem_type* x);
20 | int (*getElem)(struct single_linked_list *this, int index, single_linked_list_elem_type *e);
21 | int (*modifyElem)(struct single_linked_list *this, int index, single_linked_list_elem_type* e);
22 | int (*deleteElem)(struct single_linked_list *this, int index, single_linked_list_elem_type* e);
23 | int (*appendElem)(struct single_linked_list *this, single_linked_list_elem_type *e);
24 | int (*insertElem)(struct single_linked_list *this, int index, single_linked_list_elem_type *e);
25 | int (*popElem)(struct single_linked_list *this, single_linked_list_elem_type* e);
26 | int (*reverseList1)(struct single_linked_list *this);
27 | int (*reverseList2)(struct single_linked_list *this);
28 | };
29 |
30 | /* Exported macro ------------------------------------------------------------*/
31 | struct single_linked_list *init_single_linked_list();
32 | void destroy_single_linked_list(struct single_linked_list *list);
33 |
34 | #endif
--------------------------------------------------------------------------------
/single_linked_list/test_single_linked_list.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SingleLinkedList.h"
4 |
5 | int main(void){
6 | int i;
7 | ElemType elem,elem1;
8 | SingleLinkedList *list = InitSingleLinkedList();
9 | printf("list is empty:%d\n",list->isEmpty(list));
10 | for(i=0;i<10;i++){
11 | list->appendElem(list,&i);
12 | }
13 | list->print(list);
14 | printf("list is empty:%d\n",list->isEmpty(list));
15 | printf("list length:%d\n",list->length(list));
16 | printf("reverse1 list :\n");
17 | list->reverseList1(list); //迭代方式
18 | list->print(list);
19 | printf("reverse2 list :\n");
20 | list->reverseList2(list); //迭代方式
21 | list->print(list);
22 | list->clear(list);
23 | for (i = 10; i < 20; i++){
24 | list->appendElem(list,&i);
25 | }
26 | list->print(list);
27 | list->getElem(list,3,&elem1);
28 | printf("the elem of index 3 is %d\n",elem1);
29 | elem = 31;
30 | list->modifyElem(list,3,&elem);
31 | list->getElem(list,3,&elem1);
32 | printf("modify the elem of index 3 to %d\n",elem1);
33 | list->print(list);
34 | elem = 25;
35 | list->insertElem(list,5,&elem);
36 | printf("insert elem %d to index 5\n",elem);
37 | list->print(list);
38 | list->deleteElem(list,7,&elem);
39 | printf("delete elem %d of index 7\n",elem);
40 | list->print(list);
41 | elem = 14;
42 | printf("the index of 14 is %d\n",list->indexElem(list,&elem));
43 | list->popElem(list,&elem);
44 | printf("pop elem %d\n",elem);
45 | list->print(list);
46 | DestroySingleLinkedList(list);
47 | return 0;
48 | }
--------------------------------------------------------------------------------
/single_linked_list_queue/single_linked_list_queue.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SingleLinkedListQueue.h"
4 |
5 | static void clear(SingleLinkedListQueue *This);
6 | static int isEmpty(SingleLinkedListQueue *This);
7 | static int length(SingleLinkedListQueue *This);
8 | static QNode *getHead(SingleLinkedListQueue *This);
9 | static int enQueue(SingleLinkedListQueue *This,QNode *n);
10 | static int deQueue(SingleLinkedListQueue *This,QNode *n);
11 | static int traverse(SingleLinkedListQueue *This,int (*visit)(QNode *n));
12 |
13 | SingleLinkedListQueue *InitSingleLinkedListQueue(){
14 | SingleLinkedListQueue *Q = (SingleLinkedListQueue *)malloc(sizeof(SingleLinkedListQueue));
15 | QNode *p = (QNode *)malloc(sizeof(QNode));
16 | Q->This = p;
17 | Q->front = p;
18 | Q->tear = Q->front;
19 | p->next = NULL;
20 | Q->clear = clear;
21 | Q->isEmpty = isEmpty;
22 | Q->length = length;
23 | Q->getHead = getHead;
24 | Q->enQueue = enQueue;
25 | Q->deQueue = deQueue;
26 | Q->traverse = traverse;
27 | return Q;
28 | }
29 |
30 | void DestroySingleLinkedListQueue(SingleLinkedListQueue *Q){
31 | Q->clear(Q);
32 | free(Q->This);
33 | free(Q);
34 | Q = NULL;
35 | }
36 |
37 | static void clear(SingleLinkedListQueue *This){
38 | QNode *p = This->This->next;
39 | QNode *temp = NULL;
40 | while(p){
41 | temp = p;
42 | p = p->next;
43 | free(temp);
44 | }
45 | p = This->This;
46 | p->next = NULL;
47 | This->front = p;
48 | This->tear = This->front;
49 | }
50 |
51 | static int isEmpty(SingleLinkedListQueue *This){
52 | QNode *p = This->This;
53 | if(p->next){
54 | return 0;
55 | }else{
56 | return 1;
57 | }
58 | }
59 |
60 | static int length(SingleLinkedListQueue *This){
61 | int j = 0;
62 | QNode *p = This->This->next;
63 | while(p){
64 | j++;
65 | p = p->next;
66 | }
67 | return j;
68 | }
69 |
70 | static QNode *getHead(SingleLinkedListQueue *This){
71 | return This->front->next;
72 | }
73 |
74 | static int enQueue(SingleLinkedListQueue *This,QNode *n){
75 | if(!n) return -1;
76 | This->tear->next = n;
77 | n->next = NULL;
78 | This->tear = n;
79 | return 0;
80 | }
81 |
82 | static int deQueue(SingleLinkedListQueue *This,QNode *n){
83 | if(This->front == This->tear){
84 | n = NULL;
85 | return -1;
86 | }
87 | QNode *temp = This->front->next;
88 | *n = *(temp);
89 | This->front->next = temp->next;
90 | if(This->tear == temp) This->tear = This->front;
91 | free(temp);
92 | return 0;
93 | }
94 |
95 | static int traverse(SingleLinkedListQueue *This,int (*visit)(QNode *n)){
96 | if(This->front == This->tear){
97 | return -1;
98 | }
99 | QNode *temp = This->front->next;
100 | while(temp){
101 | if(visit(temp) != 0) break;
102 | temp = temp->next;
103 | }
104 | return 0;
105 | }
--------------------------------------------------------------------------------
/single_linked_list_queue/single_linked_list_queue.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _SINGLELINKEDLISTQUEUE_H
3 | #define _SINGLELINKEDLISTQUEUE_H
4 | /* Includes ------------------------------------------------------------------*/
5 | /* Exported types ------------------------------------------------------------*/
6 | typedef struct QElemType{
7 | int id;
8 | char name[20];
9 | }QElemType;
10 |
11 | typedef struct QNode{
12 | QElemType elem; //存储空间
13 | struct QNode *next;
14 | }QNode,*Queueptr;
15 |
16 | typedef struct SingleLinkedListQueue{
17 | QNode *This;
18 | Queueptr front; //队头
19 | Queueptr tear; //队尾
20 | void (*clear)(struct SingleLinkedListQueue *This);
21 | int (*isEmpty)(struct SingleLinkedListQueue *This);
22 | int (*length)(struct SingleLinkedListQueue *This);
23 | QNode *(*getHead)(struct SingleLinkedListQueue *This);
24 | int (*enQueue)(struct SingleLinkedListQueue *This,QNode *n);
25 | int (*deQueue)(struct SingleLinkedListQueue *This,QNode *n);
26 | int (*traverse)(struct SingleLinkedListQueue *This,int (*visit)(QNode *n));
27 | }SingleLinkedListQueue;
28 |
29 | /* Exported macro ------------------------------------------------------------*/
30 | SingleLinkedListQueue *InitSingleLinkedListQueue();
31 | void DestroySingleLinkedListQueue(SingleLinkedListQueue *Q);
32 |
33 | #endif
--------------------------------------------------------------------------------
/single_linked_list_queue/test_single_linked_list_queue.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SingleLinkedListQueue.h"
4 |
5 | char name[][3] = {"xw","xh","xm","xg","xl","xz"};
6 |
7 | void strCopy(char *str_a,char *str_b){
8 | while(*str_b != '\0'){
9 | *str_a++ = *str_b++;
10 | }
11 | *str_a = '\0';
12 | }
13 |
14 | int printQnode(QNode *node){
15 | printf("id:%d,name:%s\n",node->elem.id,node->elem.name);
16 | return 0;
17 | }
18 |
19 | int main(void){
20 | int i;
21 | QNode *node = NULL;
22 | SingleLinkedListQueue *queue = InitSingleLinkedListQueue();
23 | printf("queue is empty:%d\n",queue->isEmpty(queue));
24 | for(i=0;i<6;i++){
25 | node = (QNode *)malloc(sizeof(QNode));
26 | node->elem.id = i;
27 | strCopy(node->elem.name,name[i]);
28 | queue->enQueue(queue,node);
29 | }
30 | queue->traverse(queue,printQnode);
31 | printf("queue is empty:%d\n",queue->isEmpty(queue));
32 | printf("queue length:%d\n",queue->length(queue));
33 | queue->clear(queue);
34 | for (i = 10; i < 16; i++){
35 | node = (QNode *)malloc(sizeof(QNode));
36 | node->elem.id = i;
37 | strCopy(node->elem.name,name[i-10]);
38 | queue->enQueue(queue,node);
39 | }
40 | queue->traverse(queue,printQnode);
41 | while(queue->length(queue)){
42 | node = queue->getHead(queue);
43 | printf("present client: id=%d, name=%s\n",node->elem.id,node->elem.name);
44 | node = (QNode *)malloc(sizeof(QNode));
45 | queue->deQueue(queue,node);
46 | printf("client :id=%d,name=%s finish!\n",node->elem.id,node->elem.name);
47 | free(node);
48 | node = NULL;
49 | }
50 | DestroySingleLinkedListQueue(queue);
51 | return 0;
52 | }
--------------------------------------------------------------------------------
/string/string.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "MyString.h"
4 |
5 | MyString *myStringAssign(char *str){
6 | int i,str_length = 0;
7 | MyString *S = (MyString *)malloc(sizeof(MyString));
8 | if(!S) return NULL;
9 | while(*(str+str_length) != '\0'){
10 | str_length++;
11 | }
12 | str_length++;
13 | S->str = (char *)malloc(str_length*sizeof(char));
14 | S->size = str_length;
15 | if(!S->str){
16 | free(S);
17 | return NULL;
18 | }
19 | S->length = str_length - 1;
20 | for(i=0;istr + i) = *(str + i);
22 | }
23 | return S;
24 | }
25 |
26 | int myStringLength(MyString *S){
27 | if(S){
28 | return S->length;
29 | }else{
30 | return 0;
31 | }
32 | }
33 |
34 | int myStringRemainSpace(MyString *S){
35 | if(S){
36 | return S->size - S->length -1;
37 | }else{
38 | return 0;
39 | }
40 | }
41 |
42 | int isMyStringEmpty(MyString *S){
43 | if(!S) return 0;
44 | if(S->length){
45 | return 0;
46 | }else{
47 | return 1;
48 | }
49 | }
50 |
51 | int clearMyString(MyString *S){
52 | if(S){
53 | if(S->str){
54 | *S->str = '\0';
55 | }
56 | S->length = 0;
57 | }
58 | }
59 |
60 | void destroyMyString(MyString *S){
61 | if(S){
62 | if(S->str){
63 | free(S->str);
64 | }
65 | free(S);
66 | S = NULL;
67 | }
68 | }
69 |
70 | int printMyString(MyString *S){
71 | if(S){
72 | if(S->str){
73 | printf("%s, ",S->str);
74 | }
75 | printf("length :%d\n",S->length);
76 | }
77 | return 0;
78 | }
79 |
80 | int compareMyString(MyString *S1,MyString *S2){
81 | int i;
82 | int result = 0;
83 | if(!S1 || !S2) return result;
84 | for(i=0;ilength && ilength;i++){
85 | if(*(S1->str+i) != *(S2->str+i)){
86 | result = *(S1->str+i) - *(S2->str+i);
87 | break;
88 | }
89 | }
90 | if(result == 0){
91 | result = S1->length - S2->length;
92 | }
93 | return result;
94 | }
95 |
96 | MyString *copyMyString(MyString *S){
97 | int i;
98 | if(!S || !S->str) return NULL;
99 | MyString *temp = (MyString *)malloc(sizeof(MyString));
100 | if(!temp) return NULL;
101 | temp->size = S->length + 1;
102 | temp->str = (char *)malloc(temp->size*sizeof(char));
103 | if(!temp->str){
104 | free(temp);
105 | return NULL;
106 | }
107 | temp->length = S->length;
108 | for(i=0;ilength+1;i++){
109 | *(temp->str + i) = *(S->str + i);
110 | }
111 | return temp;
112 | }
113 |
114 | int myStringIndexChar(MyString *S,char indexElem,int pos){
115 | int index = -1;
116 | int i;
117 | if(!S) return -1;
118 | for(i=pos;ilength;i++){
119 | if(*(S->str + i) == indexElem){
120 | index = i;
121 | break;
122 | }
123 | }
124 | return index;
125 | }
126 |
127 | int insertMyString(MyString *S1,MyString *S2,int pos){
128 | int i;
129 | if(!S1 || !S1->str || !S2 || !S2->str) return -1;
130 | if(pos < 0 || pos > S1->length) return -1;
131 | if(myStringRemainSpace(S1) < S2->length){
132 | S1->size += S2->length - myStringRemainSpace(S1);
133 | S1->str = (char *)realloc(S1->str,S1->size*sizeof(char));
134 | if(!S1->str) return -1;
135 | }
136 | for(i=S1->length;i>=pos;i--){
137 | *(S1->str + S2->length + i) = *(S1->str + i);
138 | }
139 | for(i=0;ilength;i++){
140 | *(S1->str + pos + i) = *(S2->str + i);
141 | }
142 | S1->length += S2->length;
143 | return 0;
144 | }
145 |
146 | MyString *substrMyString(MyString *S,int start,int end){
147 | int i,length;
148 | if(start < 0 || start >= S->length || end <= 0 || end > S->length || end <= start) return NULL;
149 | MyString *temp = (MyString *)malloc(sizeof(MyString));
150 | if(!temp) return NULL;
151 | length = end - start;
152 | temp->size = length+1;
153 | temp->str = (char *)malloc(temp->size*sizeof(char));
154 | if(!temp->str){
155 | free(temp);
156 | return NULL;
157 | }
158 | for(i=0;istr + i) = *(S->str + start + i);
160 | }
161 | *(temp->str + length) = '\0';
162 | temp->length = length;
163 | return temp;
164 | }
165 |
166 | int deleteMyString(MyString *S,int start,int end){
167 | int i,length;
168 | if(start < 0 || start >= S->length || end <= 0 || end > S->length || end <= start) return -1;
169 | length = S->length - end + 1;
170 | for(i=0;i<=length;i++){
171 | *(S->str + start + i) = *(S->str + end + i);
172 | }
173 | S->length -= (end - start);
174 | return 0;
175 | }
176 |
177 | int concatMyString(MyString *S1,MyString *S2){
178 | int i;
179 | if(!S2->str || !S1) return -1;
180 | if(myStringRemainSpace(S1) < S2->length){
181 | S1->size += (S2->length - myStringRemainSpace(S1));
182 | S1->str = (char *)realloc(S1->str,S1->size*sizeof(char));
183 | if(!S1->str) return -1;
184 | }
185 | for(i=0;i<=S2->length;i++){
186 | *(S1->str + S1->length + i) = *(S2->str + i);
187 | }
188 | S1->length += S2->length;
189 | return 0;
190 | }
191 |
192 | int replaceMyString(MyString *S1,MyString *S2,int start,int end){
193 | if(start < 0 || start >= S1->length || end <= 0 || end > S1->length || end < start) return -1;
194 | if(end>start){
195 | if(deleteMyString(S1,start,end) == -1){
196 | return -1;
197 | }
198 | }
199 | if(insertMyString(S1,S2,start) == -1){
200 | return -1;
201 | }else{
202 | return 0;
203 | }
204 | }
205 |
206 | MyStringArray *splitMyString(MyString *S,char splitElem){
207 | int start = 0,end = 0,index = 0;
208 | MyStringArray *strarray = NULL;
209 | MyString *strtemp = NULL;
210 | index = myStringIndexChar(S,splitElem,0);
211 | if(index == -1) return NULL;
212 | strarray = InitMyStringArray();
213 | end = index;
214 | if(end != start){
215 | strtemp = substrMyString(S,start,end);
216 | strarray->tearPush(strarray,strtemp);
217 | destroyMyString(strtemp);
218 | if(end == S->length){
219 | return strarray;
220 | }
221 | }
222 | index++;
223 | start = index;
224 | while(index > 0){
225 | index = myStringIndexChar(S,splitElem,index);
226 | if(index != -1){
227 | end = index;
228 | strtemp = substrMyString(S,start,end);
229 | strarray->tearPush(strarray,strtemp);
230 | destroyMyString(strtemp);
231 | if(end == S->length){
232 | break;
233 | }
234 | index++;
235 | start = index;
236 | }
237 | }
238 | if(end != S->length){
239 | end = S->length;
240 | strtemp = substrMyString(S,start,end);
241 | strarray->tearPush(strarray,strtemp);
242 | destroyMyString(strtemp);
243 | }
244 | return strarray;
245 | }
246 |
247 | static int *getKMPNext(MyString *substr){
248 | int *nextval = (int *)malloc((substr->length)*sizeof(int));
249 | int j=0,k=-1;
250 | if(nextval){
251 | *nextval = -1;
252 | while(jlength){
253 | if(k == -1 || *(substr->str+j) == *(substr->str+k)){
254 | if(*(substr->str+(++j)) == *(substr->str+(++k))){ //两个字符相等时跳过
255 | *(nextval+j) = *(nextval+k);
256 | }else{
257 | *(nextval+j) = k;
258 | }
259 | }else{
260 | k = *(nextval+k);
261 | }
262 | }
263 | return nextval;
264 | }else{
265 | return NULL;
266 | }
267 | }
268 |
269 | int myStringIndexSubString(MyString *S,MyString *substr,int pos){ //KMP算法
270 | int i = pos,j = 0;
271 | if(!S || !substr || pos > S->length) return -1;
272 | int *nextval = getKMPNext(substr);
273 | if(!nextval) return -1;
274 | while(i < S->length && j < substr->length){
275 | if(*(S->str + i) == *(substr->str + j)){
276 | i++;
277 | j++;
278 | }else{
279 | j = *(nextval+j); //i不需要回溯,j回到指定位置
280 | if(j == -1){
281 | i++; //当j为-1时,要移动的是i
282 | j++; //j归0
283 | }
284 | }
285 | }
286 | free(nextval);
287 | if(j == substr->length){
288 | return i - j;
289 | }else{
290 | return -1;
291 | }
292 | }
293 |
294 | static int parenthesesMatching(MyString *S){
295 | if(!S) return 0;
296 | int bracket_count = 0,str_index = 0;
297 | int bracket_contained = 0;
298 | if(*(S->str+str_index) != '(') return 0;
299 | while(*(S->str+str_index) != '\0'){
300 | if(*(S->str+str_index) == '('){
301 | bracket_contained = 1;
302 | bracket_count++;
303 | }else if(*(S->str+str_index) == ')'){
304 | if(bracket_count){
305 | bracket_count --;
306 | }else{
307 | bracket_contained = 0;
308 | break;
309 | }
310 | }
311 | str_index++;
312 | }
313 | if(bracket_contained){
314 | if(bracket_count == 0) return 1;
315 | }
316 | return 0;
317 | }
318 |
319 | static int squareBracketMatching(MyString *S){
320 | if(!S) return 0;
321 | int bracket_count = 0,str_index = 0;
322 | int bracket_contained = 0;
323 | if(*(S->str+str_index) != '[') return 0;
324 | while(*(S->str+str_index) != '\0'){
325 | if(*(S->str+str_index) == '['){
326 | bracket_contained = 1;
327 | bracket_count++;
328 | }else if(*(S->str+str_index) == ']'){
329 | if(bracket_count){
330 | bracket_count --;
331 | }else{
332 | bracket_contained = 0;
333 | break;
334 | }
335 | }
336 | str_index++;
337 | }
338 | if(bracket_contained){
339 | if(bracket_count == 0) return 1;
340 | }
341 | return 0;
342 | }
343 |
344 | static int braceMatching(MyString *S){
345 | if(!S) return 0;
346 | int bracket_count = 0,str_index = 0;
347 | int bracket_contained = 0;
348 | if(*(S->str+str_index) != '{') return 0;
349 | while(*(S->str+str_index) != '\0'){
350 | if(*(S->str+str_index) == '{'){
351 | bracket_contained = 1;
352 | bracket_count++;
353 | }else if(*(S->str+str_index) == '}'){
354 | if(bracket_count){
355 | bracket_count --;
356 | }else{
357 | bracket_contained = 0;
358 | break;
359 | }
360 | }
361 | str_index++;
362 | }
363 | if(bracket_contained){
364 | if(bracket_count == 0) return 1;
365 | }
366 | return 0;
367 | }
368 |
369 | int isBracketMatching(MyString *S,char leftBracketChar){
370 | int isMatching = 0;
371 | switch(leftBracketChar){
372 | case '(':
373 | isMatching = parenthesesMatching(S);
374 | break;
375 | case '[':
376 | isMatching = squareBracketMatching(S);
377 | break;
378 | case '{':
379 | isMatching = braceMatching(S);
380 | break;
381 | }
382 | return isMatching;
383 | }
384 |
385 | static int getMatchingParenthesisIndex(MyString *S,int leftbracketIndex){
386 | if(!S) return -1;
387 | int bracket_count = 0,str_index = leftbracketIndex;
388 | int bracket_contained = 0;
389 | if(*(S->str+str_index) != '(') return -1;
390 | while(*(S->str+str_index) != '\0'){
391 | if(*(S->str+str_index) == '('){
392 | bracket_contained = 1;
393 | bracket_count++;
394 | }else if(*(S->str+str_index) == ')'){
395 | bracket_count --;
396 | if(bracket_count == 0) break;
397 | }
398 | str_index++;
399 | }
400 | if(str_index > S->length) return -1;
401 | return str_index;
402 | }
403 |
404 | static int getMatchingSquareBracketIndex(MyString *S,int leftbracketIndex){
405 | if(!S) return -1;
406 | int bracket_count = 0,str_index = leftbracketIndex;
407 | int bracket_contained = 0;
408 | if(*(S->str+str_index) != '[') return -1;
409 | while(*(S->str+str_index) != '\0'){
410 | if(*(S->str+str_index) == '['){
411 | bracket_contained = 1;
412 | bracket_count++;
413 | }else if(*(S->str+str_index) == ']'){
414 | bracket_count --;
415 | if(bracket_count == 0) break;
416 | }
417 | str_index++;
418 | }
419 | if(str_index > S->length) return -1;
420 | return str_index;
421 | }
422 |
423 | static int getMatchingBraceIndex(MyString *S,int leftbracketIndex){
424 | if(!S) return -1;
425 | int bracket_count = 0,str_index = leftbracketIndex;
426 | int bracket_contained = 0;
427 | if(*(S->str+str_index) != '{') return -1;
428 | while(*(S->str+str_index) != '\0'){
429 | if(*(S->str+str_index) == '{'){
430 | bracket_contained = 1;
431 | bracket_count++;
432 | }else if(*(S->str+str_index) == '}'){
433 | bracket_count --;
434 | if(bracket_count == 0) break;
435 | }
436 | str_index++;
437 | }
438 | if(str_index > S->length) return -1;
439 | return str_index;
440 | }
441 |
442 | int getMatchingBracketIndex(MyString *S,char leftBracketChar,int leftbracketIndex){
443 | int index = 0;
444 | switch(leftBracketChar){
445 | case '(':
446 | index = getMatchingParenthesisIndex(S,leftbracketIndex);
447 | break;
448 | case '[':
449 | index = getMatchingSquareBracketIndex(S,leftbracketIndex);
450 | break;
451 | case '{':
452 | index = getMatchingBraceIndex(S,leftbracketIndex);
453 | break;
454 | }
455 | return index;
456 | }
--------------------------------------------------------------------------------
/string/string.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _MYSTRING_H
3 | #define _MYSTRING_H
4 | /* Exported types ------------------------------------------------------------*/
5 |
6 | typedef struct MyString{
7 | char *str;
8 | int length; //字符串长度
9 | int size; //空间大小
10 | }MyString;
11 |
12 | typedef struct MyStringArray_P
13 | {
14 | MyString *mystring;
15 | struct MyStringArray_P *next;
16 | }MyStringArray_P;
17 |
18 | typedef struct MyStringArray{
19 | MyStringArray_P *This;
20 | MyStringArray_P *front;
21 | MyStringArray_P *tear;
22 | void (*clear)(struct MyStringArray *This);
23 | int (*isEmpty)(struct MyStringArray *This);
24 | int (*length)(struct MyStringArray *This);
25 | int (*index)(struct MyStringArray *This, MyString *e);
26 | int (*get)(struct MyStringArray *This, int index, MyString **e);
27 | int (*getFront)(struct MyStringArray *This, MyString **e);
28 | int (*getTear)(struct MyStringArray *This, MyString **e);
29 | int (*modify)(struct MyStringArray *This, int index, MyString *e);
30 | int (*insert)(struct MyStringArray *This, int index, MyString *e);
31 | int (*delete)(struct MyStringArray *This, int index, MyString **e);
32 | int (*tearPush)(struct MyStringArray *This, MyString *e);
33 | int (*tearPop)(struct MyStringArray *This, MyString **e);
34 | int (*frontPush)(struct MyStringArray *This, MyString *e);
35 | int (*frontPop)(struct MyStringArray *This, MyString **e);
36 | int (*traverse)(struct MyStringArray *This,int (*visit)(MyString **e));
37 | }MyStringArray;
38 |
39 | /* Includes ------------------------------------------------------------------*/
40 | #include "MyStringArray.h"
41 |
42 |
43 | MyString *myStringAssign(char *str);
44 | int myStringLength(MyString *S);
45 | int isMyStringEmpty(MyString *S);
46 | int clearMyString(MyString *S);
47 | void destroyMyString(MyString *S);
48 | int printMyString(MyString *str);
49 | int compareMyString(MyString *S1,MyString *S2);
50 | MyString *copyMyString(MyString *S);
51 | int myStringIndexChar(MyString *S,char indexElem,int pos);
52 | int insertMyString(MyString *S1,MyString *S2,int pos);
53 | int deleteMyString(MyString *S,int start,int end);
54 | int concatMyString(MyString *S1,MyString *S2);
55 | int replaceMyString(MyString *S1,MyString *S2,int start,int end);
56 | MyString *substrMyString(MyString *S,int start,int end);
57 | MyStringArray *splitMyString(MyString *S,char splitElem);
58 | int myStringIndexSubString(MyString *S,MyString *substr,int pos);
59 | int isBracketMatching(MyString *S,char leftBracketChar);
60 | int getMatchingBracketIndex(MyString *S,char leftBracketChar,int leftbracketIndex);
61 | #endif
62 |
--------------------------------------------------------------------------------
/string/string_array.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "MyStringArray.h"
4 |
5 | static void clear(MyStringArray *This);
6 | static int isEmpty(MyStringArray *This);
7 | static int length(MyStringArray *This);
8 | static int index(MyStringArray *This, MyString *e);
9 | static int get(MyStringArray *This, int index, MyString **e);
10 | static int getFront(MyStringArray *This, MyString **e);
11 | static int getTear(MyStringArray *This, MyString **e);
12 | static int modify(MyStringArray *This, int index, MyString *e);
13 | static int insert(MyStringArray *This, int index, MyString *e);
14 | static int delete(MyStringArray *This, int index, MyString **e);
15 | static int tearPush(MyStringArray *This, MyString *e);
16 | static int tearPop(MyStringArray *This, MyString **e);
17 | static int frontPush(MyStringArray *This, MyString *e);
18 | static int frontPop(MyStringArray *This, MyString **e);
19 | static int traverse(MyStringArray *This,int (*visit)(MyString **e));
20 |
21 | MyStringArray *InitMyStringArray(){
22 | MyStringArray *strArray = (MyStringArray *)malloc(sizeof(MyStringArray));
23 | MyStringArray_P *p = (MyStringArray_P *)malloc(sizeof(MyStringArray_P));
24 | strArray->This = p;
25 | p->next = NULL;
26 | strArray->front = p;
27 | strArray->tear = strArray->front;
28 | strArray->clear = clear;
29 | strArray->isEmpty = isEmpty;
30 | strArray->length = length;
31 | strArray->index = index;
32 | strArray->get = get;
33 | strArray->getFront = getFront;
34 | strArray->getTear = getTear;
35 | strArray->modify = modify;
36 | strArray->insert = insert;
37 | strArray->delete = delete;
38 | strArray->tearPush = tearPush;
39 | strArray->tearPop = tearPop;
40 | strArray->frontPush = frontPush;
41 | strArray->frontPop = frontPop;
42 | strArray->traverse = traverse;
43 | return strArray;
44 | }
45 |
46 | void DestroyMyStringArray(MyStringArray *strArray){
47 | strArray->clear(strArray);
48 | free(strArray->This);
49 | strArray->This = NULL;
50 | free(strArray);
51 | strArray = NULL;
52 | }
53 |
54 | static void clear(MyStringArray *This){
55 | MyStringArray_P *p = This->This->next;
56 | MyStringArray_P *temp = NULL;
57 | while(p){
58 | temp = p;
59 | p = p->next;
60 | free(temp->mystring->str);
61 | temp->mystring->str = NULL;
62 | free(temp);
63 | temp = NULL;
64 | }
65 | p = This->This;
66 | p->next = NULL;
67 | This->front = p;
68 | This->tear = This->front;
69 | }
70 |
71 | static int isEmpty(MyStringArray *This){
72 | MyStringArray_P *p = This->This;
73 | if(p->next){
74 | return 0;
75 | }else{
76 | return 1;
77 | }
78 | }
79 |
80 | static int length(MyStringArray *This){
81 | int j = 0;
82 | MyStringArray_P *p = This->This->next;
83 | while(p){
84 | j++;
85 | p = p->next;
86 | }
87 | return j;
88 | }
89 |
90 | static int index(MyStringArray *This, MyString *e){
91 | MyStringArray_P *p = This->This->next;
92 | int pos = -1;
93 | int j = 0;
94 | if(!e) return -1;
95 | while(p){
96 | if(compareMyString(p->mystring,e) == 0){
97 | pos = j;
98 | }
99 | p = p->next;
100 | j++;
101 | }
102 | return pos;
103 | }
104 |
105 | static int get(MyStringArray *This, int index, MyString **e){
106 | MyStringArray_P *p = This->This->next;
107 | int j = 0;
108 | while(p && j < index){
109 | p = p->next;
110 | j++;
111 | }
112 | if(!p || j > index) return -1;
113 | *e = copyMyString(p->mystring);
114 | return 0;
115 | }
116 |
117 | static int getFront(MyStringArray *This, MyString **e){
118 | MyStringArray_P *p = This->front->next;
119 | *e = copyMyString(p->mystring);
120 | return 0;
121 | }
122 |
123 | static int getTear(MyStringArray *This, MyString **e){
124 | MyStringArray_P *p = This->tear;
125 | *e = copyMyString(p->mystring);
126 | return 0;
127 | }
128 |
129 | static int modify(MyStringArray *This, int index, MyString *e){
130 | MyStringArray_P *p = This->This->next;
131 | if(!e) return -1;
132 | int j = 0;
133 | while(p && j < index){
134 | p = p->next;
135 | j++;
136 | }
137 | if(!p || j > index) return -1;
138 | free(p->mystring);
139 | p->mystring = copyMyString(e);
140 | if(p->mystring){
141 | return 0;
142 | }else{
143 | return -1;
144 | }
145 | }
146 |
147 | static int insert(MyStringArray *This, int index, MyString *e){
148 | MyStringArray_P *p = This->This;
149 | int j = 0;
150 | if(!e) return -1;
151 | MyStringArray_P *temp = (MyStringArray_P *)malloc(sizeof(MyStringArray_P));
152 | if(!temp) return -1;
153 | while(p && j < index){
154 | p = p->next;
155 | j++;
156 | }
157 | if(!p || j > index) return -1;
158 | temp->next = p->next;
159 | p->next = temp;
160 | temp->mystring = copyMyString(e);
161 | if(!temp->mystring){
162 | free(temp);
163 | return -1;
164 | }else{
165 | return 0;
166 | }
167 | }
168 |
169 | static int delete(MyStringArray *This, int index, MyString **e){
170 | MyStringArray_P *p = This->This;
171 | MyStringArray_P *temp = NULL;
172 | int j = 0;
173 | while(p->next && j < index){
174 | p = p->next;
175 | j++;
176 | }
177 | if(!p->next || j > index) return -1;
178 | temp = p->next;
179 | p->next = temp->next;
180 | *e = copyMyString(temp->mystring);
181 | free(temp);
182 | return 0;
183 | }
184 |
185 | static int tearPush(MyStringArray *This, MyString *e){
186 | MyStringArray_P *p = This->This;
187 | if(!e) return -1;
188 | MyStringArray_P *temp = (MyStringArray_P *)malloc(sizeof(MyStringArray_P));
189 | if(!temp) return -1;
190 | temp->mystring = copyMyString(e);
191 | if(temp->mystring){
192 | if(This->front == This->tear){
193 | p->next = temp;
194 | }else{
195 | This->tear->next = temp;
196 | }
197 | temp->next = NULL;
198 | This->tear = temp;
199 | return 0;
200 | }else{
201 | free(temp);
202 | return -1;
203 | }
204 | }
205 |
206 | static int tearPop(MyStringArray *This, MyString **e){
207 | MyStringArray_P *p = This->This;
208 | MyStringArray_P *temp = NULL;
209 | while(p->next->next){
210 | p = p->next;
211 | }
212 | temp = p->next;
213 | This->tear = p;
214 | if(!temp) return -1;
215 | *e = copyMyString(temp->mystring);
216 | free(temp);
217 | p->next = NULL;
218 | return 0;
219 | }
220 |
221 | static int frontPush(MyStringArray *This, MyString *e){
222 | MyStringArray_P *p = This->This;
223 | if(!e) return -1;
224 | MyStringArray_P *temp = (MyStringArray_P *)malloc(sizeof(MyStringArray_P));
225 | if(!temp) return -1;
226 | temp->mystring = copyMyString(e);
227 | if(temp->mystring){
228 | temp->next = p->next;
229 | p->next = temp;
230 | if(This->front == This->tear){
231 | This->tear = temp;
232 | }
233 | return 0;
234 | }else{
235 | free(temp);
236 | return -1;
237 | }
238 | }
239 |
240 | static int frontPop(MyStringArray *This, MyString **e){
241 | if(This->front == This->tear){
242 | e = NULL;
243 | return -1;
244 | }
245 | MyStringArray_P *p = This->front->next;
246 | *e = copyMyString(p->mystring);
247 | This->front->next = p->next;
248 | if(This->tear == p) This->tear = This->front;
249 | free(p);
250 | return 0;
251 | }
252 |
253 | static int traverse(MyStringArray *This,int (*visit)(MyString **e)){
254 | if(This->front == This->tear){
255 | return -1;
256 | }
257 | MyStringArray_P *temp = This->front->next;
258 | while(temp){
259 | if(visit(&(temp->mystring)) != 0) break;
260 | temp = temp->next;
261 | }
262 | return 0;
263 | }
264 |
265 |
--------------------------------------------------------------------------------
/string/string_array.h:
--------------------------------------------------------------------------------
1 | /* Define to prevent recursive inclusion -------------------------------------*/
2 | #ifndef _MYSTRINGARRAY_H
3 | #define _MYSTRINGARRAY_H
4 | /* Includes ------------------------------------------------------------------*/
5 | #include "MyString.h"
6 | /* Exported types ------------------------------------------------------------*/
7 | /* Exported macro ------------------------------------------------------------*/
8 | MyStringArray *InitMyStringArray();
9 | void DestroyMyStringArray(MyStringArray *strArray);
10 |
11 | #endif
--------------------------------------------------------------------------------
/string/test_string.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "MyString.h"
4 |
5 | int printMyString(MyString *str){
6 | printf("%s, ",str->str);
7 | printf("length :%d\n",str->length);
8 | return 0;
9 | }
10 |
11 | int printMyStringElem(MyString **str){
12 | printf("%s ",(*str)->str);
13 | return 0;
14 | }
15 |
16 | int main(void){
17 | int i;
18 | char words[] = {"without new experiences, something inside of us sleeps."};
19 | char bracket[] = {"(A(B(E(K,L),F),C(G),D(H(M),I,J)))"};
20 | MyString *str_a = NULL;
21 | MyString *str_b = NULL;
22 | MyString *str_c = NULL;
23 | MyStringArray *str_array = NULL;
24 |
25 | str_a = myStringAssign("hello ");
26 | str_c = myStringAssign("hello ");
27 |
28 | printf("str_a :");
29 | printMyString(str_a);
30 |
31 | printf("is MyString empty: %d\n",isMyStringEmpty(str_a));
32 |
33 | if(compareMyString(str_a,str_c)==0){
34 | printf("str_a equals str_c\n");
35 | }else{
36 | printf("str_a is not equal str_c\n");
37 | }
38 |
39 | clearMyString(str_a);
40 |
41 | printf("is MyString empty: %d\n",isMyStringEmpty(str_a));
42 |
43 | if(compareMyString(str_a,str_c)==0){
44 | printf("str_a equals str_c\n");
45 | }else{
46 | printf("str_a is not equal str_c\n");
47 | }
48 |
49 | destroyMyString(str_a);
50 | str_a = copyMyString(str_c);
51 |
52 | str_b = myStringAssign("Mr Bluyee");
53 | printf("str_b :");
54 | printMyString(str_b);
55 |
56 | concatMyString(str_a,str_b);
57 | printf("str_a :");
58 | printMyString(str_a);
59 |
60 | printf("the MyString : Mr Bluyee index: ");
61 | i = myStringIndexSubString(str_a,str_b,0);
62 | printf("%d\n", i);
63 |
64 | printf("the char \'B\' index: %d\n", myStringIndexChar(str_a,'B',0));
65 |
66 | insertMyString(str_a,str_b,str_a->length);
67 | printf("str_a :");
68 | printMyString(str_a);
69 |
70 | replaceMyString(str_a,str_b,6,str_a->length);
71 | printf("str_a :");
72 | printMyString(str_a);
73 |
74 | deleteMyString(str_a,6,str_a->length);
75 | printf("str_a :");
76 | printMyString(str_a);
77 |
78 | destroyMyString(str_c);
79 | str_c = substrMyString(str_a,0,5);
80 | printf("str_c :");
81 | printMyString(str_c);
82 |
83 | destroyMyString(str_c);
84 | str_c = myStringAssign(words);
85 | str_array = splitMyString(str_c,' ');
86 | str_array->traverse(str_array,printMyStringElem);
87 |
88 | destroyMyString(str_a);
89 | destroyMyString(str_b);
90 | destroyMyString(str_c);
91 | DestroyMyStringArray(str_array);
92 |
93 | str_a = myStringAssign(bracket);
94 | printf("\n");
95 | printMyString(str_a);
96 | printf("bracketMatching :%d\n",isBracketMatching(str_a,'('));
97 | printf("MatchingBracketIndex :%d\n",getMatchingBracketIndex(str_a,'(',4));
98 | return 0;
99 | }
--------------------------------------------------------------------------------
/string/test_string_array.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "MyStringArray.h"
4 |
5 | char name[][14] = {"without ","new ","experiences, ","something ","inside ","of ","us ","sleeps."};
6 |
7 | int printMyString(MyString **str){
8 | printf("%s",(*str)->str);
9 | return 0;
10 | }
11 |
12 | int main(void){
13 | int i;
14 | MyStringArray *strarray = InitMyStringArray();
15 | MyString *string = NULL;
16 | printf("MyStringArray is empty:%d\n",strarray->isEmpty(strarray));
17 | for(i=0;i<8;i++){
18 | string = myStringAssign(name[i]);
19 | strarray->frontPush(strarray,string);
20 | destroyMyString(string);
21 | }
22 |
23 | printf("MyStringArray: ");
24 | strarray->traverse(strarray,printMyString);
25 | printf("\n");
26 | printf("MyStringArray is empty:%d\n",strarray->isEmpty(strarray));
27 | printf("MyStringArray length:%d\n",strarray->length(strarray));
28 |
29 | strarray->clear(strarray);
30 | printf("MyStringArray is empty:%d\n",strarray->isEmpty(strarray));
31 | printf("MyStringArray length:%d\n",strarray->length(strarray));
32 |
33 | for(i=0;i<8;i++){
34 | string = myStringAssign(name[i]);
35 | strarray->tearPush(strarray,string);
36 | destroyMyString(string);
37 | }
38 |
39 | printf("MyStringArray: ");
40 | strarray->traverse(strarray,printMyString);
41 | printf("\n");
42 |
43 | strarray->get(strarray,3,&string);
44 | printf("the MyString of index 3 is: ");
45 | printMyString(&string);
46 | printf("\n");
47 | destroyMyString(string);
48 |
49 | strarray->getTear(strarray,&string);
50 | printf("the tear MyString is: ");
51 | printMyString(&string);
52 | printf(" ,index is: %d\n",strarray->index(strarray,string));
53 | destroyMyString(string);
54 |
55 | strarray->getFront(strarray,&string);
56 | printf("the front MyString is: ");
57 | printMyString(&string);
58 | printf("\n");
59 | destroyMyString(string);
60 |
61 | string = myStringAssign("me ");
62 | strarray->modify(strarray,6,string);
63 | destroyMyString(string);
64 | printf("MyStringArray: ");
65 | strarray->traverse(strarray,printMyString);
66 | printf("\n");
67 |
68 | string = myStringAssign("you and ");
69 | strarray->insert(strarray,6,string);
70 | destroyMyString(string);
71 | printf("MyStringArray: ");
72 | strarray->traverse(strarray,printMyString);
73 | printf("\n");
74 |
75 | strarray->delete(strarray,6,&string);
76 | printf("MyStringArray: ");
77 | strarray->traverse(strarray,printMyString);
78 | printf("\n");
79 | printMyString(&string);
80 | printf(" deleted!\n");
81 | destroyMyString(string);
82 |
83 | strarray->tearPop(strarray,&string);
84 | printf("MyStringArray: ");
85 | strarray->traverse(strarray,printMyString);
86 | printf("\n");
87 | printMyString(&string);
88 | printf(" tear poped!\n");
89 | destroyMyString(string);
90 |
91 | strarray->frontPop(strarray,&string);
92 | printf("MyStringArray: ");
93 | strarray->traverse(strarray,printMyString);
94 | printf("\n");
95 | printMyString(&string);
96 | printf(" front poped!\n");
97 | destroyMyString(string);
98 |
99 | DestroyMyStringArray(strarray);
100 | return 0;
101 | }
--------------------------------------------------------------------------------