-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list.c
216 lines (193 loc) · 4.13 KB
/
linked_list.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
210
211
212
213
214
215
216
/*
* @Description:
* @LastEditors: hsjfans
* @Email: [email protected]
* @Date: 2019-02-28 14:22:00
* @LastEditTime: 2019-04-11 15:03:54
*/
#include "include/linked_list.h"
#include <stdio.h>
#include <stdlib.h>
// create an linked node
LinkedNode *linked_node_new(Element e, LinkedNode *next)
{
LinkedNode *node;
node = (LinkedNode *)malloc(sizeof(LinkedNode));
node->e = e;
node->next = next;
return node;
}
LinkedList *linked_list_new()
{
LinkedList *list;
list = (LinkedList *)malloc(sizeof(LinkedList));
list->length = 0;
list->root = linked_node_new(NULL, NULL);
return list;
}
// return the current length
int len(LinkedList *list)
{
return list->length;
}
// return true if the list is empty or false if not
boolean is_empty(LinkedList *list)
{
return list->length == 0 ? 1 : 0;
}
// return the index of e or -1 if not exists
int index_of_list(Element e, LinkedList *list, compare_func cmp)
{
if (is_empty(list))
{
return -1;
}
int index = 0;
LinkedNode *node = list->root->next;
while (node != NULL)
{
if (cmp(node->e, e) == 0)
{
return index;
}
else
{
index++;
node = node->next;
}
}
return -1;
}
// return the element whose index is index or return Null if not found
Element find(unsigned int index, LinkedList *list)
{
if (index < 0 || index >= list->length)
{
return NULL;
}
int i = 0;
LinkedNode *node = list->root->next;
while (i < index)
{
node = node->next;
i++;
}
return node->e;
}
// delete the element in array;
void delete_one(unsigned int index, LinkedList *list)
{
if (index < 0 || index >= list->length)
{
return;
}
LinkedNode *root = list->root;
int i = -1;
while (i < index - 1)
{
root = root->next;
i++;
}
LinkedNode *node = root->next;
root->next = node->next;
free(node); // release the delete node
}
// delete the element in array;
void delete_value(Element e, LinkedList *list, compare_func cmp)
{
if (is_empty(list))
{
return;
}
LinkedNode *root = list->root;
while (root->next != NULL)
{
if (cmp(root->next->e, e) == 0)
{
LinkedNode *node = root->next;
root->next = node->next;
free(node);
return;
}
else
{
root = root->next;
}
}
}
// insert the element in array; always in tail;
void push_back(Element e, LinkedList *list)
{
push_index(e, len(list), list);
}
// insert the element in array; always in front;
void push_front(Element e, LinkedList *list)
{
push_index(e, 0, list);
}
// insert the element in array;
void push_index(Element e, unsigned int index, LinkedList *list)
{
if (index > list->length)
{
return;
}
LinkedNode *head = list->root;
int i = 0;
while (i < index)
{
head = head->next;
i++;
}
LinkedNode *node = linked_node_new(e, head->next);
head->next = node;
}
// clear data and release the space ;
void clear(LinkedList *list)
{
if (is_empty(list))
{
free(list->root);
list->root = NULL;
list->length = 0;
free(list);
return;
}
LinkedNode *root = list->root;
while (root)
{
root->e = NULL;
LinkedNode *node = root->next;
root->next = NULL;
free(root);
root = node;
}
list->length = 0;
free(list);
}
// reverse the array
void reverse(LinkedList *list)
{
if (list->length <= 1)
{
return;
}
LinkedNode *root = list->root;
LinkedNode *first = root->next;
LinkedNode *node = first->next;
while (node)
{
LinkedNode *tmp = node->next;
node->next = first;
first->next = tmp;
root->next = node;
first = node;
node = tmp;
}
}
// void q_sort(LinkedList *list, unsigned int left, unsigned int right, boolean desc, compare_func cmp)
// {
// }
// void sort(LinkedList *list, boolean desc, compare_func cmp)
// {
// }