-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassic_LinkedList.c
275 lines (233 loc) · 6.81 KB
/
Classic_LinkedList.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
//
// Created by wmespindula on 28/10/2021.
//
#include <malloc.h>
#include "LinkedList.h"
struct Node *classic_split(struct Node *head);
struct Node *classic_mergeSort(struct Node *head);
LinkedList classic_initLinkedList() {
LinkedList ll = {
// variables
.size = 0,
.head = NULL,
// methods
.add = classic_insertTail,
.addLast = classic_insertTail,
.get = classic_get,
.getFirst = classic_getFirst,
.getLast = classic_getLast,
.delete = classic_deleteTail,
.isDone = classic_isDone,
.next = classic_next,
.previous = classic_previous,
.verbosePrint = verbosePrint,
.simplePrint = simplePrint,
.forEach = classic_forEach,
.sort = classic_sort
};
return ll;
}
/**
* Find tail iteration from head
* @param linkedList
* @return pointer to tail element
*/
Node *findTail(LinkedList linkedList) {
Node *curr;
for (curr = linkedList.head; curr->pNextNode != NULL; curr = curr->pNextNode) {}
return curr;
}
Node *findElement(LinkedList linkedList, u_int pos) {
if (pos == 0) {
return linkedList.head;
} else {
Node *current = linkedList.head;
for (int i = 0; i < pos; i++) {
current = current->pNextNode;
}
return current;
}
}
Node *classic_newNode(double data) {
Node *pElement = (Node *) malloc(sizeof(Node));
pElement->data = data;
pElement->isValid = true;
return pElement;
}
bool classic_insertHead(LinkedList *linkedList, double data) {
Node *newElement = classic_newNode(data);
newElement->pPrevNode = NULL;
Node *head = linkedList->head;
if (head != NULL) {
newElement->pNextNode = head;
head->pPrevNode = newElement;
} else {
linkedList->tail = newElement;
}
linkedList->head = newElement;
linkedList->size++;
return true;
}
bool classic_insertTail(LinkedList *linkedList, double data) {
if (linkedList->head == NULL) {
return classic_insertHead(linkedList, data);
} else {
Node *newNode = classic_newNode(data);
Node *tailElement = linkedList->tail;
tailElement->pNextNode = newNode;
newNode->pPrevNode = tailElement;
linkedList->tail = newNode;
}
linkedList->size++;
return true;
}
bool classic_insertAt(LinkedList *linkedList, double data, u_int pos) {
if (pos <= linkedList->size) {
if (pos == 0) {
return classic_insertHead(linkedList, data);
} else if (pos == linkedList->size) {
return classic_insertTail(linkedList, data);
} else {
Node *elementAt = findElement(*linkedList, pos);
Node *preElementAt = elementAt->pPrevNode;
Node *newElement = classic_newNode(data);
preElementAt->pNextNode = newElement;
newElement->pPrevNode = preElementAt;
newElement->pNextNode = elementAt;
elementAt->pPrevNode = newElement;
linkedList->size++;
return true;
}
}
return false;
}
Node classic_get(LinkedList linkedList, u_int pos) {
if (pos < linkedList.size) {
return *(findElement(linkedList, pos));
} else {
return *(linkedList.tail);
}
}
Node classic_getFirst(LinkedList linkedList) {
if (linkedList.head != NULL) { return *(linkedList.head); }
else {
Node invalidNode = {
.isValid = false
};
return invalidNode;
}
}
Node classic_getLast(LinkedList linkedList) {
if (linkedList.tail != NULL) {
return *(linkedList.tail);
} else {
Node invalidNode = {
.isValid = false
};
return invalidNode;
}
}
bool classic_isDone(Node node) {
return node.isValid == false;
}
Node classic_next(Node node) {
if (node.pNextNode != NULL) {
return *node.pNextNode;
} else {
Node invalidNode = {
.isValid = false
};
return invalidNode;
}
}
Node classic_previous(Node node) {
if (node.pPrevNode != NULL) {
return *node.pPrevNode;
} else {
Node invalidNode = {
.isValid = false
};
return invalidNode;
}
}
bool classic_deleteHead(LinkedList *linkedList) {
Node *head = linkedList->head;
linkedList->head = head->pNextNode;
head->pNextNode->pPrevNode = NULL;
free(head);
return true;
}
bool classic_deleteTail(LinkedList *linkedList) {
Node *tail = linkedList->tail;
if (tail != NULL) {
if (tail->pPrevNode != NULL) {
linkedList->tail = tail->pPrevNode;
tail->pPrevNode->pNextNode = NULL;
free(tail);
} else {
linkedList->tail = NULL;
linkedList->head = NULL;
free(tail);
}
linkedList->size--;
return true;
} else {
return false;
}
}
void classic_forEach(LinkedList linkedList, void (*func)(double data)) {
Node *curr = linkedList.head;
while (curr != NULL) {
double data = curr->data;
func(data);
curr = curr->pNextNode;
}
}
void classic_sort(LinkedList *linkedList) {
Node *newHead = classic_mergeSort(linkedList->head);
linkedList->head = newHead;
linkedList->tail = findTail(*linkedList);
}
/* ------ MERGE SORT ALGORITHM (Code from GeeksForGeeks) ------ */
// Function to classic_merge two linked lists
struct Node *classic_merge(struct Node *first, struct Node *second) {
// If first linked list is empty
if (!first)
return second;
// If second linked list is empty
if (!second)
return first;
// Pick the smaller value
if (first->data < second->data) {
first->pNextNode = classic_merge(first->pNextNode, second);
first->pNextNode->pPrevNode = first;
first->pPrevNode = NULL;
return first;
} else {
second->pNextNode = classic_merge(first, second->pNextNode);
second->pNextNode->pPrevNode = second;
second->pPrevNode = NULL;
return second;
}
}
// Function to do classic_merge sort
struct Node *classic_mergeSort(struct Node *head) {
if (!head || !head->pNextNode)
return head;
struct Node *second = classic_split(head);
// Recur for left and right halves
head = classic_mergeSort(head);
second = classic_mergeSort(second);
// Merge the two sorted halves
return classic_merge(head, second);
}
struct Node *classic_split(struct Node *head) {
struct Node *fast = head, *slow = head;
while (fast->pNextNode && fast->pNextNode->pNextNode) {
fast = fast->pNextNode->pNextNode;
slow = slow->pNextNode;
}
struct Node *temp = slow->pNextNode;
slow->pNextNode = NULL;
return temp;
}