-
Notifications
You must be signed in to change notification settings - Fork 4
/
linkedList.c
146 lines (126 loc) · 3.5 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
#include <stdio.h>
#include <stdlib.h>
#include "popc.h"
#include "linkedList.h"
// Initializes the linked list
linkedListProto ptr linkedListProtoNew () {
linkedListProto ptr proto = (linkedListProto ptr) malloc (sizeof (linkedListProto));
dbcEnsure (proto != NULL, "Memory Allocation Error!");
return proto;
}
// Initializes the linked list
linkedList ptr linkedListNew () {
linkedList ptr ll = (linkedList ptr) malloc (sizeof (linkedList));
dbcEnsure (ll != NULL, "Memory Allocation Error!");
ll -> head = NULL;
ll -> current = NULL;
ll -> tail = NULL;
return ll;
}
// Initializes the linked list
void linkedListDel (linkedList ptr ll) {
// PENDING
// TODO: loop through the list and free all objs
free (ll);
}
linkedListNode ptr linkedListNewNode () {
linkedListNode ptr lln = (linkedListNode ptr) malloc (sizeof (linkedListNode));
dbcEnsure (lln != NULL, "Memory Allocation Error!");
lln -> previous = NULL;
lln -> next = NULL;
lln -> obj = NULL;
return lln;
}
// Initializes the linked list
void linkedListDelNode (linkedListNode ptr lln) {
// PENDING
// TODO: loop through the list and free all objs
free (lln);
}
// Adds data to the linked list
linkedListNode ptr linkedListInsertNode (linkedList ptr ll, object ptr obj) {
return linkedListAddNodeAfterTail (ll, obj);
}
/* PENDING
//update
void linkedListUpdateNode (linkedList ptr ll, linkedListNode ptr lln, object ptr obj) {
//PENDING
}
*/
// Adds data to the linked list’s head
linkedListNode ptr linkedListAddNodeBeforeHead (linkedList ptr ll, object ptr obj) {
linkedListNode ptr cell = linkedListNewNode ();
cell -> obj = obj;
cell -> previous = NULL;
if (ll -> head == NULL) {
cell -> next = NULL;
ll -> tail = cell;
}
else {
cell -> next = ll -> head;
}
ll -> head = cell;
return cell;
}
// Adds data to the linked list’s tail
linkedListNode ptr linkedListAddNodeAfterTail (linkedList ptr ll, object ptr obj) {
linkedListNode ptr cell = (linkedListNode ptr) malloc (sizeof (linkedListNode));
dbcEnsure (cell != NULL, "Memory Allocation Error!");
cell -> obj = obj;
cell -> next = NULL;
if (ll -> head == NULL) {
cell -> previous = NULL;
ll -> head = cell;
}
else {
cell -> previous = ll -> tail;
ll -> tail -> next = cell;
}
ll -> tail = cell;
return cell;
}
// Removes a cell from the linked list
void linkedListDeleteNode (linkedList ptr ll, linkedListNode ptr cell) {
if (cell == ll -> head) {
if (ll -> head -> next == NULL) {
ll -> head = NULL;
ll -> tail = NULL;
free (cell);
}
else {
ll -> head = ll -> head -> next;
ll -> head -> previous = NULL;
free (cell);
}
}
else {
linkedListNode ptr tmpNode = ll -> head -> next;
while (tmpNode != NULL && tmpNode != cell) {
tmpNode = tmpNode -> next;
}
if (tmpNode != NULL) {
tmpNode -> previous -> next = tmpNode -> next;
if (tmpNode -> next != NULL) tmpNode -> next -> previous = tmpNode -> previous;
free (cell);
}
}
}
// Returns a pointer to the cell containing a specific data item
linkedListNode ptr linkedListGetNode (linkedList ptr ll, linkedListCompareDelegate compareCallback, void ptr obj) {
linkedListNode ptr cell = ll -> head;
while (cell != NULL) {
if (compareCallback(cell -> obj, obj) == 0) {
return cell;
}
cell = cell -> next;
}
return NULL;
}
void linkedListDisplay (linkedList ptr ll, linkedListDisplayDelegate displayCallback) {
printf("\nLinked List\n");
linkedListNode ptr current = ll -> head;
while (current != NULL) {
displayCallback(current -> obj);
current = current -> next;
}
}