forked from kcdon/File-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.c
More file actions
executable file
·228 lines (173 loc) · 4.04 KB
/
linkedlist.c
File metadata and controls
executable file
·228 lines (173 loc) · 4.04 KB
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
#include "linkedlist.h"
#include <stdlib.h>
struct llnode {
void *val;
struct llnode *next;
};
typedef struct llnode* LLnode;
struct linkedlist {
LLnode head;
LLnode tail;
};
struct ll_iterator {
LLnode curr;
};
LList makeLL() {
LList list = (LList) malloc(sizeof(struct linkedlist));
list->head = NULL;
return list;
}
LList cloneLL(LList l) {
LList clone = makeLL();
if (l) {
LLnode curr = l->head;
while (curr) {
appendToLL(clone, curr->val);
curr = curr->next;
}
}
return clone;
}
int sizeOfLL(LList l) {
int len = 0;
LLnode curr;
if (!l) return 0; /* Empty case */
curr = l->head;
while (curr) {
curr = curr->next;
len++;
}
return len;
}
int isEmptyLL(LList l) {
return !l || !(l->head);
}
void* getFromLL(LList l, int idx) {
LLnode curr;
/* Empty and below bounds cases */
if (!l || idx < 0)
return NULL;
curr = l->head;
while (curr) {
if (idx == 0)
return curr->val;
curr = curr->next;
idx--;
}
return NULL;
}
int indexOfLL(LList l, void *val) {
LLnode curr;
int i;
if (!l)
return -1;
curr = l->head;
for (i = 0; curr; i++, curr = curr->next) {
if (curr->val == val)
return i;
}
return -1;
}
void appendToLL(LList l, void *val) {
if (!l) return;
if (l->head == NULL) {
/* List is empty */
l->head = (LLnode) malloc(sizeof(struct llnode));
l->head->val = val;
l->head->next = NULL;
l->tail = l->head;
} else {
/* List is non-empty */
l->tail->next = (LLnode) malloc(sizeof(struct llnode));
l->tail = l->tail->next;
l->tail->val = val;
l->tail->next = NULL;
}
}
void addToLL(LList l, int idx, void *val) {
LLnode node;
/* Empty cases */
if (!l) return;
else if (l->head == NULL) {
appendToLL(l, val);
return;
}
node = (LLnode) malloc(sizeof(struct llnode));
node->val = val;
if (idx <= 0) {
/* Front of list case */
node->next = l->head;
l->head = node;
} else {
LLnode curr = l->head;
while (curr->next && idx > 1) {
curr = curr->next;
idx--;
}
/* Set new tail if necessary */
if (curr->next == NULL)
l->tail = node;
node->next = curr->next;
curr->next = node;
}
}
void* remFromLL(LList l, int idx) {
if (!l || l->head == NULL)
/* Empty list */
return NULL;
else if (l->head->next == NULL) {
/* Singleton list */
void *res = l->head->val;
l->head->val = NULL;
free(l->head);
l->head = l->tail = NULL;
return res;
} else {
/* Plural items */
LLnode curr = l->head;
LLnode rem;
void *res = NULL;
while (idx > 1 && curr->next->next) {
curr = curr->next;
idx--;
}
if (idx > 0) {
/* Item is at front of list */
rem = curr->next;
res = curr->next->val;
curr->next = curr->next->next;
} else {
rem = curr;
res = curr->val;
l->head = rem->next;
if (rem->next == NULL)
l->tail = curr;
}
rem->val = NULL;
rem->next = NULL;
free(rem);
return res;
}
}
LLiter makeLLiter(LList list) {
LLiter iter = (LLiter) malloc(sizeof(struct ll_iterator));
iter->curr = list ? list->head : NULL;
return iter;
}
int iterHasNextLL(LLiter iter) {
return iter && iter->curr != NULL;
}
void *iterNextLL(LLiter iter) {
if (iter && iter->curr) {
void *res = iter->curr->val;
iter->curr = iter->curr->next;
return res;
} else
return NULL;
}
void disposeIterLL(LLiter iter) {
if (iter) {
iter->curr = NULL;
free(iter);
}
}