-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list.c
81 lines (58 loc) · 1.37 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
/*
* Linked List Implementation
* Benjamin Ellerby
* Ray Luow
* Evan Ricks
* Oliver Smith-Denny
*
*/
#include "linked_list.h"
#define RETURN_FAILURE -1
#define RETURN_SUCCESS 0
list_node * make_linked_list () {
list_node *new_list = calloc(1, sizeof(list_node));
new_list->listItem = NULL;
new_list->next = NULL;
return new_list;
}
int add_to_list (void *listItem, list_node *list) {
if (listItem == NULL) {
return RETURN_FAILURE;
}
list_node *curr = list;
if (curr->listItem == NULL) {
curr->listItem = listItem;
} else {
while (curr->next != NULL)
curr = curr->next;
list_node *new = calloc(1, sizeof(list_node));
new->next = NULL;
new->listItem = listItem;
curr->next = new;
}
return RETURN_SUCCESS;
}
int remove_from_list (int position, list_node *deleteNode, list_node *list) {
if (list == NULL || position < 1 || deleteNode == NULL) {
return RETURN_FAILURE;
}
list_node *curr = list;
for(int i = 0; i < position - 1; i++) {
if (curr->next != NULL) {
curr = curr->next;
} else {
return RETURN_FAILURE;
}
}
if (curr->next != NULL) {
if (curr->next->next == NULL) {
curr->next->listItem = NULL;
curr->next = NULL;
} else {
curr->next->listItem = NULL;
curr->next = curr->next->next;
}
free(deleteNode);
}
return RETURN_SUCCESS;
}