-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsll.c
263 lines (248 loc) · 7.02 KB
/
sll.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
/********************
CS201, Assign0, sll.c
Ben Bailey
This class implements a singly linked list that can be used with any generic data type.
********************/
#include "sll.h"
#include <stdlib.h>
#include <assert.h>
struct node //the node structure that makes up the linked list
{
void *data;
struct node *next;
}node;
struct sll //the singly linked list structure
{
void *head;
void *tail;
int size;
void (*display)(void *, FILE *);
void (*free)(void *);
};
extern SLL *
newSLL(void (*d)(void *,FILE *),void (*f)(void *)) { //constructor for the singly linked list
SLL *items = malloc(sizeof(SLL));
assert(items != 0);
items->head = 0;
items->tail = 0;
items->size = 0;
items->display = d;
items->free = f;
return items;
}
extern void
insertSLL(SLL *items,int index,void *value) { //inserts a node anywhere into the singly linked list, based on a given index
assert(index >= 0 && index <= items->size);
struct node *new = malloc(sizeof(node));
if (items->size == 0) {
new->data = value;
new->next = NULL;
items->head = new;
items->tail = new;
items->size ++;
return;
}
else if (index == 0) {
struct node *current = items->head;
new->data = value;
new->next = current;
items->head = new;
items->size ++;
return;
}
else if (index == items->size) {
struct node *current = items->tail;
current->next = new;
new->data = value;
new->next = NULL;
items->tail = new;
items->size ++;
return;
}
else {
struct node *current = items->head;
for (int i=0; i<index-1; i++) {
current = current->next;
}
struct node *currnext = current->next;
current->next = new;
new->data = value;
new->next = currnext;
items->size ++;
return;
}
}
extern void *
removeSLL(SLL *items,int index) { //removes a node from the singly linked list, based on a given index
assert(items->size > 0);
assert(index >= 0 && index < items->size);
if (index == 0) {
struct node *current = items->head;
items->head = current->next;
items->size --;
void *returnable = current->data;
free(current);
return returnable;
}
else if (index == items->size-1) {
struct node *current = items->head;
for (int i=0; i<index-1; i++) {
current = current->next;
}
struct node *removable = current->next;
current->next = NULL;
items->tail = current;
items->size --;
void *returnable = removable->data;
free(removable);
return returnable;
}
else {
struct node *current = items->head;
for (int i=0; i<index-1; i++) {
current = current->next;
}
struct node *removable = current->next;
current->next = removable->next;
items->size --;
void *returnable = removable->data;
free(removable);
return returnable;
}
}
extern void
unionSLL(SLL *recipient,SLL *donor) { //merges two singly linked lists. the first parameter is the list to be added to, and the second is the donor list
if (donor->head == NULL && donor->size == 0) {
return;
}
else if (recipient->head == NULL && recipient->size == 0) {
recipient->head = donor->head;
recipient->tail = donor->tail;
recipient->size += donor->size;
donor->head = 0;
donor->tail = 0;
donor->size = 0;
return;
}
else {
struct node *recTail = recipient->tail;
recTail->next = donor->head;
recipient->tail = donor->tail;
recipient->size += donor->size;
donor->head = 0;
donor->tail = 0;
donor->size = 0;
return;
}
}
extern void *
getSLL(SLL *items,int index) { //returns the data of a node in the list, based on a given index
assert(index >= 0 && index < items->size);
struct node *returnable = items->head;
if (index == 0) {
return returnable->data;
}
else if (index == items->size-1) {
returnable = items->tail;
return returnable->data;
}
else {
for (int i=0; i<index; i++) {
returnable = returnable->next;
}
return returnable->data;
}
}
extern void *
setSLL(SLL *items,int index,void *value) { //sets the data of a node in the list, based on a given index and value to be put in
assert(index >= 0 && index <= items->size);
if (items->size == 0) {
insertSLL(items, index, value);
struct node *returnable = NULL;
return returnable;
}
if (index == 0) {
struct node *current = items->head;
void *returnable = current->data;
current->data = value;
return returnable;
}
else if (index == items->size-1) {
struct node *current = items->tail;
void *returnable = current->data;
current->data = value;
return returnable;
}
else if (index == items->size) {
insertSLL(items, index, value);
struct node *returnable = NULL;
return returnable;
}
else {
struct node *current = items->head;
for (int i=0; i<index; i++) {
current = current->next;
}
void *returnable = current->data;
current->data = value;
return returnable;
}
}
extern int
sizeSLL(SLL *items) { //returns the number of nodes in the list
return items->size;
}
extern void
displaySLL(SLL *items,FILE *fp) { //displays the list as an array
printf("{");
struct node *current = items->head;
while (current != NULL) {
items->display(current->data, fp);
if (current->next != NULL) {printf(",");}
current = current->next;
}
printf("}");
return;
}
extern void
displaySLLdebug(SLL *items,FILE *fp) { //displays the list as an array, while also highlighting where the head and tail nodes are
if (items->size == 0) {
printf("head->{},tail->{}");
return;
}
else {
printf("head->{");
struct node *current = items->head;
while (current != NULL) {
items->display(current->data, fp);
if (current->next != NULL) {printf(",");}
current = current->next;
}
struct node *daTail = items->tail;
printf("},tail->{");
items->display(daTail->data, fp);
printf("}");
return;
}
}
extern void
freeSLL(SLL *items) { //frees each node's data, the node itself, and the linked list data structure as a whole
if (items->size ==0) {
free(items);
return;
}
else {
struct node *current = items->head;
while (current != NULL) {
struct node *freeable = current;
if (items->free != NULL) {
items->free(current->data);
}
current = current->next;
free(freeable);
}
free(items);
return;
}
return;
}