-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
252 lines (210 loc) · 5.41 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
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
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"
list createList(){
list xs;
xs.head = NULL;
xs.tail = NULL;
xs.size = 0;
return xs;
}
/** Inserts FIRST element into EMPTY list.
*
* @param key: key to be inserted.
* pList: pointer to EMPTY list into which key should be inserted.
*
* @return 1 if the insertion was successful,
* -1 if not (e.g. no more mem, etc,).
*
* Putting the first element into a list requires special treatment of null
* pointers in the empty list. This function does just that. Note that it is a
* private function that is only called by prepend or append.
*/
static int initializeList(int key, pList xs){
//allocatae new node
pElem firstItem = malloc(sizeof(elem));
if(firstItem == NULL){
printf("Not enough memory!");
return -1;
}
//initialize new node
firstItem->key = key;
firstItem->nextElem = NULL;
//update listpointers
xs->head = firstItem;
xs->tail = firstItem;
xs->size++;
return 0;
}
int append(int value, pList xs){
//inserting into an empty list is done via initializeList.
if(xs->size == 0){
return initializeList(value, xs);
}
//allocate space for new element
pElem newLast = malloc(sizeof(elem));
if(newLast == NULL){
printf("Not enough memory!");
return -1;
}
//instantiate new element
newLast->key = value;
newLast->nextElem = NULL;
//update pointers
pElem currLast = xs->tail;
currLast->nextElem = newLast;
xs->tail = newLast;
xs->size++;
return 0;
}
int prepend(int value, pList xs){
//inserting into an empty list is done via initializeList.
if(xs->size == 0){
return initializeList(value, xs);
}
pElem newFirst = malloc(sizeof(elem));
if(newFirst == NULL){
printf("Not enough memory!");
return -1;
}
//update lists first element
newFirst->key = value;
newFirst->nextElem = xs->head;
xs->head = newFirst;
xs->size++;
return 0;
}
int search(int value, pList xs){
pElem currElem = xs->head;
int k = 0;
//go through list
while(currElem->nextElem != NULL){
//if elem found, stop.
if(currElem->key == value){
return k;
}
//update pointer
currElem = currElem->nextElem;
k++;
}
return 0;
}
int removeElement(int pos, pList xs){
int n = xs->size;
//check validity of position (incl. that list be non empty)
if(pos < 0 || pos >= n){
printf("Position out of bounds or list empty!\n");
return -1;
}
// Different procedure depending on list size.
if(n==1){
//for one item first=last. List empty afterwards.
pElem firstItem = xs->head;
xs->head = NULL;
xs->tail = NULL;
free(firstItem);
}
//for two elements only simply redirect lists first/last pointer
else if(n==2){
if(pos == 0){
pElem firstItem = xs->head;
xs->head = xs->tail;
free(firstItem);
}else{
pElem lastItem = xs->tail;
xs->tail = xs->head;
xs->head->nextElem = NULL;
free(lastItem);
}
}
//if list larger need to traverse to element...
else{
pElem lItem = xs->head; //needed for traversing
pElem mItem = lItem->nextElem;
int k = 1;
//...except for first elemnt
if(pos == 0){
xs->head = mItem;
free(lItem);
}else{
//travese
while(k < pos){
lItem = mItem;
mItem = mItem->nextElem;
k++;
}
//elements in middle of the list deleted differently than at end.
if(pos == n-1){
xs->tail = lItem;
lItem->nextElem = NULL;
free(mItem);
}else{
lItem->nextElem = mItem->nextElem;
free(mItem);
}
}
}
xs->size--;
return 0;
}
void removeAll(pList xs){
int n = xs->size;
if(n == 0){
return;
}
pElem currItem = xs->head;
pElem nextItem = currItem->nextElem;
while(nextItem != NULL){
free(currItem);
currItem = nextItem;
nextItem = currItem->nextElem;
}
free(currItem);
xs->size = 0;
xs->head = NULL;
xs->tail = NULL;
}
void print(pList xs){
if(xs->size == 0){
printf("[]\n");
}else{
pElem currElem = xs->head;
printf("[%i", currElem->key);
while(currElem->nextElem != NULL){
currElem = currElem->nextElem;
printf(", %i", currElem->key);
}
printf("]\n");
}
}
int* toArray(pList xs){
int n = xs->size;
if(n ==0){
printf("List is empty!");
return NULL;
}
int* ptr;
ptr = malloc(sizeof(int)*n);
if(ptr == NULL){
printf("Not enough memory!");
return NULL;
}
pElem currItem = xs->head;
*ptr = currItem->key;
unsigned int k = 1;
while(currItem->nextElem != NULL){
currItem = currItem->nextElem;
*(ptr + k) = currItem->key;
k++;
}
return ptr;
}
void mergeLists(pList xs, pList ys){
xs->tail = ys->head;
xs->size += ys->size;
free(ys);
}
void removeList(pList xs){
removeAll(xs);
free(xs);
}