/
llist.c
288 lines (227 loc) · 6.12 KB
/
llist.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
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
/*
* listtest.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ldata.h"
#include "llist.h"
// ==============================================================
// Utility operations
// ==============================================================
struct LNode *create_new_node(struct LData *data){
struct LNode *fin = malloc(sizeof(struct LNode));
fin->data = data;
fin->next = NULL;
return fin;
}
struct LList *create_empty_list(){
struct LList *fin = malloc(sizeof(struct LList));
struct LData *data = create_empty_data();
struct LNode *root = create_new_node(data);
struct LNode *sentinal = create_new_node(data);
fin->root = root;
fin->root->next = sentinal;
fin->sentinal = sentinal;
fin->size = 0;
return fin;
}
void clear_list(struct LList *list){
struct LNode* itr_node = list->root;
struct LNode* hold;
int count = list->size;
while(itr_node != NULL || list->size == 0){
count--;
hold = itr_node->next;
free(itr_node);
itr_node = hold;
list->size--;
}
list->size++;
list = create_empty_list();
}
void swap_elems(struct LList *list, int idx1, int idx2){
struct LNode* hold1 = list->root;
struct LNode* hold2 = list->root;
int count = 0;
while(count < idx1){
hold1 = hold1->next;
count++;
}
count = 0;
while(count < idx2){
hold2 = hold2->next;
count++;
}
swap_data(hold1->data, hold2->data);
}
void print_list(struct LList *list){
int count;
int size = list->size;
if(size == 0){
printf("[ ]: %d\n", size);
return;
}
struct LNode* itr_node = list->root;
printf("[");
for(count = 0; count < size; count++){
print_data(itr_node->data);
itr_node = itr_node->next;
if(count != size-1){
printf(", ");
}
}
printf("]: %d\n", size);
}
// ==============================================================
// Query functions (no side effects)
// ==============================================================
// Checks if the list is empty
int is_empty(struct LList *list){
if(list->size == 0)
return 1;
return 0;
}
// Returns the size of the list
int size(struct LList *list){
return list->size;
}
// Checks of the list contains the specified data
// Uses the are_equal operation of LData
int contains(struct LList *list, struct LData *data){
struct LNode *itr_node = list->root;
int count;
for(count = 0; count < size(list); count++){
if(data == itr_node->data)
return 1;
itr_node = itr_node->next;
count++;
}
return 0;
}
// ==============================================================
// Add operations
// ==============================================================
// Adds the specified element to the specified position
void add_elem(struct LList *list, struct LData *data, int idx){
struct LNode *add = malloc(sizeof(struct LNode));
struct LNode *itr_node = list->root;
add->data = data;
if(idx == 0){
add->next = list->root;
list->root = add;
list->size++;
return;
}
int count;
for(count = 1; count < idx; count++){
itr_node = itr_node->next;
}
add->next = itr_node->next;
itr_node->next = add;
list->size++;
}
// Adds the specified data to the front of the list
void add_first(struct LList *list, struct LData *data){
struct LNode *add = malloc(sizeof(struct LNode));
add->data = data;
add->next = list->root;
list->root = add;
list->size++;
}
// Adds the specified data to the end of the list
void add_last(struct LList *list, struct LData *data){
print_data(data); printf("\n");
if(list->size == 0){
struct LNode *add = malloc(sizeof(struct LNode));
add->data = data;
add->next = list->root;
list->root = add;
list->size++;
return;
}
struct LNode *add = create_new_node(data);
add->data = data;
struct LNode *itr_node = list->root;
while(itr_node->next->next != list->sentinal){
itr_node = itr_node->next;
}
itr_node->next = add;
add->next = list->sentinal;
list->size++;
}
// ==============================================================
// Get operation
// ==============================================================
// Returns a handle (pointer) to the element of the list at the specified position
struct LData *get_elem(struct LList *list, int idx){
struct LNode* itr_node = list->root;
int count;
for(count = 0; count < idx; count++){
itr_node = itr_node->next;
}
return itr_node->data;
}
// ==============================================================
// Remove operations
// ==============================================================
// Removes and returns the specified element from the specified position
struct LData *rem_elem(struct LList *list, int idx){
if(idx == 0) return rem_first(list);
if(idx == list->size) return rem_last(list);
struct LNode *itr_node = list->root;
struct LData *data_hold;
int count = 0;
while(count +1 < idx){
itr_node = itr_node->next;
count++;
}
data_hold = itr_node->next->data;
itr_node->next = itr_node->next->next;
if(list->size == 0){
return data_hold;
}
list->size--;
return data_hold;
}
// Removes and returns the first element in the list
// Assumes that the list is not empty
struct LData *rem_first(struct LList *list){
struct LNode *itr_node = list->root;
struct LData *data_hold;
data_hold = itr_node->data;
list->root = itr_node->next;
list->size--;
return data_hold;
}
// Removes and returns the last element in the list
// Assumes that the list is not empty
struct LData *rem_last(struct LList *list){
struct LNode *itr_node = list->root;
struct LData* data_hold;
while(itr_node->next->next != list->sentinal)
itr_node = itr_node->next;
data_hold = itr_node->next->data;
itr_node->next = list->sentinal;
list->size--;
return data_hold;
}
// ==============================================================
// Sort operations
// ==============================================================
// Sorts the list according to the are_ordered operation of LData
void insertion_sort(struct LList *list){
struct LNode *node_i = list->root;
struct LNode *node_j;
int i,j;
for(i = 0; i < list->size-1; i++){
j = i;
node_i = node_i->next;
node_j = node_i;
while(j < list->size && !are_ordered(node_i->data, node_j->data)){
j++;
swap_elems(list, i, j);
node_j = node_j->next;
}
}
}