-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkedlist.c
376 lines (328 loc) · 9.99 KB
/
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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Author: Ronald Hove
* Student Number: 3459314
* Date: August 30 2018
* Assignment: Practical 4
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "linkedlist.h"
#include "helper.h"
/*
* This function checks if the linked list is empty or not
* Algorithm
* 1. Method to check if linked list is populated with nodes
* 2. Performs a null pointer check on the head node
* 3. Returns an integer either 0 or 1, 1 if there are nodes in linked list, 0 if none
*
* Outputs
* No outputs
*
*/
int isEmpty(struct Node* *refToHeadNode){
return *refToHeadNode == NULL ? 1 : 0;
}
/*
* This function pushes new nodes onto the linked list using the following algorithm
*
* Algorithm
* 1. If the item to push is validated
* 2. allocate node
* 3. put in the data
* 4. if the list is empty
* 5. set tail to the new node
* 6. Make next of new node as head
* 7. move the head to point to the new node
*
* Inputs
* refToCurrentHeadNode is the address of the previously pushed node
* newData contains the actual data stored by the newly created node being pushed
*
* Outputs
* Outputs an error if the input is invalid
*
*/
void push(struct Node* *refToCurrentHeadNode, char newData, struct Node* *refToTailNode){
if(isPushItemValid(newData) == 1){
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = newData;
if(isEmpty(&(*refToCurrentHeadNode)) == 1){
(*refToTailNode) = newNode;
}
newNode->link = (*refToCurrentHeadNode);
(*refToCurrentHeadNode) = newNode;
}else{
throwInvalidInputException();
}
}
/*
* This function adds new nodes onto the linked list at the position before the head node
*
* Algorithm
* 1. allocate node
* 2. set a node last as ref to the last node
* 3. set the link to new node to NULL
* 4. if the list is empty
* 5. set head to the new node
* 6. Otherwise traverse to the end of the list
* 7. make tail pointer point to the new node
*
* Inputs
* refToCurrentHeadNode is the address of the previously pushed node
* newData contains the actual data stored by the newly created node being pushed
*
* Outputs
* No outputs
*
*/
void append(struct Node** refToCurrentHeadNode, char newData, struct Node* *refToTailNode) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *refToCurrentHeadNode;
newNode->data = newData;
newNode->link = NULL;
if (isEmpty(&*(refToCurrentHeadNode)) == 1) {
*refToCurrentHeadNode = newNode;
return;
}
while (last->link != NULL){
last = last->link;
}
last->link = newNode;
(*refToTailNode) = newNode;
}
/*
* This function adds a node after some existing node in the list
*
* Algorithm
* 1. Find the node which will be previous to the new node to enter the list
* 2. check if the search result returned anything other than null or of the list is empty
* 3. if yes throw an exception
* 4. Otherwise
* 5. set the pointer to the previous node to the new node's pointer to point to thew next node
*
* newData is data contained in thew new allocated node
* existing data is the data contained in the node before thew new node to enter the list
*
* Outputs
* No outputs to the user
*
*/
void insertAfter(char newData, char existingData, struct Node* *refToHeadNode, struct Node* *refToTail){
struct Node** previousNode = find(&(*refToHeadNode), existingData);
if (previousNode == NULL || isEmpty(&(*refToHeadNode)) == 1) {
throwInvalidInputException();
return;
}
push(&(*previousNode)->link,newData, &(*refToTail));
}
/*
* This function adds a node before some existing node in the list
*
* Algorithm
* 1. Find the node which will be previous to the new node to enter the list
* 2. check if the search result returned anything other than null or of the list is empty
* 3. if yes throw an exception
* 4. Otherwise
* 5. set the pointer to the previous node to the new node's pointer to point to thew next node
*
* Inputs
* newData is data contained in thew new allocated node
* existing data is the data contained in the node after thew new node to enter the list
*
* Outputs
* no outputs to the user
*
*/
void insertBefore(char newData, char existingData, struct Node* *refToHeadNode, struct Node* *refToTailNode){
struct Node** nextNode = find(&(*refToHeadNode), existingData);
if (nextNode == NULL || isEmpty(&(*refToHeadNode)) == 1) {
throwInvalidInputException();
return;
}
push(nextNode,newData, &(*refToTailNode));
}
/*
* This function nodes containing specific input data
*
* Algorithm
* 1. If list is not empty
* 2. traverse the list while there are nodes
* 3. check if the dataToFind is equal to each node in the list
* 4. if data is equal
* 5. return the data and exit the loop
* 6. return null if no data found
*
*
* Inputs
* refToCurrentHeadNode is the address of the previously pushed node
* newData contains the actual data stored by the newly created node being pushed
*
* Outputs
* No user outputs
*
* Return data
*
* the node containing the input data if found or null
*
*/
struct Node** find(struct Node** refToCurrentHeadNode, char dataToFind){
if(isEmpty(&(*refToCurrentHeadNode)) == 0){
struct Node** temp = &(*refToCurrentHeadNode);
while (*temp){
if((*temp)->data == dataToFind){
return temp;
}
temp = &(*temp)->link;
}
}
return NULL;
}
/*
* This function prints the data at each node in the linked list
* Function of type void | no returned outputs
*
* Algorithm
* 1. allocate node temp to the head node
* 2. create a counter
* 3. Iterate while the next node is not NULL
* 4. check if the counter is in range (0,size of linked list)
* 5. print a [-] after each printed node
* 6. print the date at each node
* 7. set temp equal to the next node
* 8. increment count variable
* 9. print a new line after loop exits
*
* Outputs
* prints out the data at each node for the size of the linked list
*
*/
void printList(struct Node** refToHeadNode){
if(isEmpty(&(*refToHeadNode)) == 1){
printf("%s\n", "-");
}else{
struct Node** temp = &(*refToHeadNode);
int count = 0;
while (*temp != NULL){
if(count > 0 && count < getSize(&(*refToHeadNode))) {
printf("%s", "-");
}
printf("%c", (*temp)->data);
temp = &(*temp)->link;
count++;
}
printf("\n");
}
}
/*
* This function removes a node that contains a specified data item
*
* Algorithm
* 1. Check if linked list is empty
* 2. throw an invalid input | cant run remove command on empty list
* 3. return; | Simply break out of the method
* 4. Otherwise
* 5. Create a new node temp which holds a reference to the head & last which points to the tail
* 6. Check if the temp node holds the node to remove
* 7. if yes then remove it and decrement the size of list as well remove the link and lastly free that memory
* 8. Otherwise iterate the list while the pointer to each successive node is not a null pointer
* 9. if data in temp is equal to the node to remove
* 10. if date in temp is equal to the tail node data
* 11. set tail to the last node
* 12. set pointer to last node equal to pointer to temp node
* 13 decrement the size
* 14 free up the memory
*
* Inputs
* struct Node* *referenceToHeadNode | a pointer to the head node
* char nodeToRemove | the data specific to the node to remove
*
* Outputs
* Outputs an error if the the input is invalid
*
*/
void removeNode(struct Node** refToCurrentHeadNode, char nodeToRemove, struct Node** refToTail){
if (isEmpty(&(*refToCurrentHeadNode)) == 1){ /*Check if the list is empty*/
throwInvalidInputException();
return;
}else{
struct Node** temp = &(*refToCurrentHeadNode), **last = temp;
if ((*temp) != NULL && (*temp)->data == nodeToRemove){ /* Check if the head node is the nodeToRemove */
(*refToCurrentHeadNode) = (*temp)->link;
//free(*temp);
return;
}
while (*temp != NULL){
if((*temp)->data == nodeToRemove){
if((*temp)->data == (*refToTail)->data){
(*refToTail) = (*last); /*set tail as the last node*/
}
(*last)->link = (*temp)->link;
//free((*temp));
return;
}
last = temp;
temp = &(*temp)->link;
}
throwInvalidInputException(); /* if program ends up here that means nodeToRemove was not found*/
}
}
/*
* This function returns the data in the head node of the linked list
*
* Algorithm
* 1. return tail->data | a pointer accessing the data in the head node
*
* Inputs
* No inputs
*
* Outputs
* No outputs
*
*/
char getHead(struct Node* *refToHead){
return (*refToHead)->data;
}
/*
* This function returns the data in the tail node of the linked list
* Algorithm
* return tail->data | a pointer accessing the data in the tail node
*
* Inputs
* No inputs
*
* Outputs
* No outputs
*
*/
char getTail(struct Node* *refToTail){
return (*refToTail)->data;
}
/*
* This function returns the size of the linked list
* Algorithm
* 1. return size | an int storing the size of the array
*
* Inputs
* No inputs
*
* Outputs
* No outputs
*
*/
int getSize(struct Node** refToHeadNode){
int size = 0;
if (isEmpty(&(*refToHeadNode)) == 1){
size = 0;
}else{
struct Node** temp = &(*refToHeadNode);
size = 0;
while (*temp != NULL){
size++;
temp = &(*temp)->link;
}
}
return size;
}