-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlists.c
311 lines (229 loc) · 5.59 KB
/
linkedlists.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Linked List Problems
Based on Nick Parlante's Linked List Problems
<http://cslibrary.stanford.edu/105/LinkedListProblems.pdf>
Kai Rikhye
http://github.com/dutycycle
September 2014
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
/* Define a singly linked list node */
typedef struct Node {
int data;
struct Node *next;
} Node_t;
/* Utility functions: Printing */
void print_node(Node_t *node) {
printf("%d -> ", node -> data);
}
void print_linked_list(Node_t *head) {
Node_t *cur = head;
while (cur) {
print_node(cur);
cur = cur -> next;
}
printf("\n");
}
/** Utility functions: Insertion **/
void insert_at_tail(Node_t *head, int data) {
Node_t *cur = head;
while (cur -> next) {
cur = cur -> next;
}
Node_t *inserted = malloc(sizeof(Node_t));
inserted -> data = data;
inserted -> next = NULL;
cur -> next = inserted;
}
void insert_at_head(Node_t **head, int data) {
Node_t *newhead = malloc(sizeof(Node_t));
newhead -> data = data;
newhead -> next = *head;
*head = newhead;
}
void insert_after_value(Node_t *head, int value, int data) {
Node_t *cur = head;
while (cur) {
if (cur -> data == value) {
Node_t *new_node = malloc(sizeof(Node_t));
new_node -> data = data;
new_node -> next = cur -> next;
cur -> next = new_node;
return;
}
cur = cur -> next;
}
}
void reverse_linked_list(Node_t **head_ref) {
Node_t *cur = *head_ref;
Node_t *prev = NULL;
Node_t *next = NULL;
while (cur) {
next = cur -> next;
cur -> next = prev;
prev = cur;
cur = next;
}
*head_ref = prev;
}
void recurse_reverse_linked_list(Node_t **head_ref) {
Node_t *head = *head_ref;
if (head == NULL || head -> next == NULL) {
return;
}
Node_t *rest = head -> next;
recurse_reverse_linked_list(&rest);
head -> next -> next = head;
head -> next = NULL;
*head_ref = rest;
}
/*
Exercise 1: Count
Returns the number of times a given int occurs in a list.
*/
int count(Node_t *head, int value_to_count) {
Node_t *cur = head;
int my_count = 0;
while (cur) {
if (cur -> data == value_to_count) {
my_count++;
}
}
return my_count;
}
/*
Exercise 2: GetNth
Returns the n-th element of the list, where n is zero-indexed.
asserts failure if n is not in bounds.
*/
int get_node_at_index(Node_t *head, int index) {
Node_t *cur = head;
assert(cur != NULL);
if (index == 0) return head -> data;
for (int i = 1; i < index; i++) {
cur = cur -> next;
assert(cur != NULL);
}
return cur -> data;
}
/*
Exercise 3: DeleteList
Deallocate a list and null the head pointer.
*/
void delete_list(Node_t **head_ref) {
Node_t *cur = *head_ref;
Node_t *next = NULL;
while (cur) {
next = cur -> next;
free(cur);
cur = next;
}
*head_ref = NULL;
}
/*
Exercise 4: Pop
Given non-empty list, delete the head node and return its data.
*/
int pop(Node_t **head_ref) {
Node_t *head = *head_ref;
*head_ref = head -> next;
int data = head -> data;
free(head);
return data;
}
/*
Exercise 5: InsertNth
Insert new node at the specificed index within the list.
*/
void insertNth(Node_t **head_ref, int index, int value) {
Node_t *cur = *head_ref;
Node_t *prev = NULL;
Node_t *next = NULL;
// move to the specified index, keeping track of previous node
for (int i = 0; i < index; i++) {
prev = cur;
cur = cur -> next;
}
// insert at the current index, pointing to any existing node
Node_t *new = malloc(sizeof(new));
new -> data = value;
new -> next = cur;
if (prev == NULL) {
*head_ref = new;
} else {
prev -> next = new;
}
}
/*
Exercise 6: Sorted Insert
Given a list that is sorted in increasing order, insert the element
such that the sorted order of the list is maintained.
*/
void sortedInsert(Node_t **head_ref, int value) {
Node_t *cur = *head_ref;
Node_t *next = NULL;
Node_t *prev = NULL;
Node_t *new = malloc(sizeof(new));
new -> data = value;
new -> next = NULL;
while (cur != NULL && cur -> data < value) {
prev = cur;
cur = cur -> next;
}
if (cur == NULL && prev == NULL) {
// empty list passed in
*head_ref = new;
} else if (cur == NULL && prev != NULL) {
// value is bigger than everything in list; insert at end
prev -> next = new;
} else if (cur != NULL && prev == NULL) {
// value is smaller than everything in list; insert at beginning
new -> next = cur;
*head_ref = new;
} else if (cur != NULL && prev != NULL) {
// insert before cur
new -> next = cur;
prev -> next = new;
}
}
/* Exercise 7: InsertSort
Given a list, rearrange its node so they are sorted in increasing
order, utlizing the sortedInsert function.
*/
void insertSort(Node_t **head_ref) {
Node_t *cur = *head_ref;
Node_t *next = NULL;
while (cur != NULL) {
next = cur -> next;
int data = pop(&cur);
sortedInsert(&cur, data);
cur = next;
}
}
int main() {
Node_t *head = malloc(sizeof(Node_t));
head -> data = 5;
head -> next = NULL;
insert_at_tail(head, 600);
insert_at_head(&head, 4);
insert_after_value(head, 4, 7);
print_linked_list(head);
reverse_linked_list(&head);
print_linked_list(head);
recurse_reverse_linked_list(&head);
print_linked_list(head);
printf("%d\n", pop(&head));
print_linked_list(head);
insertNth(&head, 0, 1);
insertNth(&head, 1, 6);
insertNth(&head, 1, 5);
print_linked_list(head);
sortedInsert(&head, 2);
sortedInsert(&head, 0);
sortedInsert(&head, -1);
print_linked_list(head);
insertSort(&head);
print_linked_list(head);
}