-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_exercises.cpp
356 lines (309 loc) · 7.81 KB
/
linked_list_exercises.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
struct Node
{
int data;
Node *next;
};
/*
Print elements of a linked list on console
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
void Print(Node *head)
{
if(head != NULL)
{
std::cout<<head->data<<std::endl;
Print(head->next);
}
}
/*
Insert Node at the begining of a linked list
Initially head pointer argument could be NULL for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
return back the pointer to the head of the linked list in the below method.
*/
Node* Insert(Node *head,int data)
{
Node* temp = new Node;
temp->data = data;
temp->next = head;
return temp;
}
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* InsertAtEnd(Node *head,int data)
{
//if head is NULL, create a new Node and make it the head of the list
if(head == NULL)
{
head = new Node;
head->data = data;
return head;
}
else
{
Node* iter;
iter = head;
//iterate through the linked list and stop at the last Node
while(iter->next != NULL)
{
iter = iter->next;
}
//create a new Node
Node* temp = new Node;
temp->data = data;
//connect the new Node to the last node on the list
iter->next = temp;
return head;
}
}
/*
Insert Node at a given position in a linked list
The linked list will not be empty and position will always be valid
First element in the linked list is at position 0
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* InsertNth(Node* head, int data, int position)
{
//if list uninitialized
if (head == NULL)
{
//create new node and make it the head of the list
head = new Node;
head->data= data;
return head;
}
else
{
//begin the iterator at the head
Node* iter;
iter = head;
//init loop counter
int counter;
//create new node
Node* temp = new Node;
temp->data = data;
//if new node is to be the new head(position 0)
if(position == 0)
{
temp->next = iter;
head = temp;
return head;
}
//if the position is not at the head, loop until reached position
for(counter = 1; counter <= position; counter ++)
{
//if counter has reached position
if(counter == position)
{
//connect the cords
temp->next = iter->next;
iter->next = temp;
}
iter = iter->next;
}
return head;
}
}
/*
Delete Node at a given position in a linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Delete (Node* head, int position)
{
//if deleting head
if(position == 0)
{
//create a buffer pointer to point at the head
Node* to_delete = head;
//re-assign the head to the next item in the linked list
head = head-> next;
//delete the original head
delete to_delete;
//return the new head
return head;
}
else
{
//initiaize counter and iterators
//counter is a numerical representation of the iteration through the linked list
int counter = 1;
//these iterators are pointers to the links in the linked list
Node* prev = head;
Node* to_delete = prev->next;
Node* next = to_delete->next;
while(counter++ != position)
{
prev = to_delete;
to_delete = next;
next = to_delete->next;
}
//after reached desired position
//connect the previous link to the following link
prev->next = next;
//assign the "next" link of the link that will be deleted to be NULL
to_delete->next = NULL;
//delete the link
delete to_delete;
return head;
}
}
/*
Print elements of a linked list in reverse order as standard output
head pointer could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
void ReversePrint(Node *head)
{
if(head != NULL)
{
if(head->next != NULL)
ReversePrint(head->next);
std::cout<< head->data <<std::endl;
}
}
/*
Reverse a linked list and return pointer to the head
The input list will have at least one element
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Reverse(Node* head)
{
if(head == NULL)
{
//list is empty
return NULL;
}
else if(head->next == NULL)
{
//list only has one link
//or end of the list in the recursive loop
return head;
}
else
{
Node* prev = head;//previous node placeholder
Node* iter = head-> next;//current node placeholder
Node* new_head;//final result and new head of list
//recursive call
//when it reaches the end of the list it will return the current "head", which is the last node in the list
//that value will travel through the recursions and stay the same, making the last item in the list the new head
new_head = Reverse(iter);
//ends once the (head->next == NULL) condition is true, which would mean the end of the list
prev->next = NULL;//dump the "next" pointer from the node that is previous to the current node
iter->next = prev;//connect the current node to the previous node
return new_head;//return new head
}
}
/*
Compare two linked lists A and B
Return 1 if they are identical and 0 if they are not.
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
int CompareLists(Node* headA, Node* headB)
{
if(headA->data == headB->data)
{
if(headA->next == NULL && headB->next == NULL)
return 1;//if end of the list
if((headA->next == NULL && headB->next != NULL)||(headA->next != NULL && headB->next == NULL))
return 0;//if lists are not same length
return CompareLists(headA->next,headB->next);
}
else
{
return 0;
}
}
/*
Merge two sorted lists A and B as one linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* MergeLists(Node* headA, Node* headB)
{
if(headA == NULL) return headB;
if(headB == NULL) return headA;
Node* iterA = headA;
Node* iterB = headB;
if(headA->data < headB->data)
{
headA->next = MergeLists(headA->next, headB);
return headA;
}else
{
headB->next = MergeLists(headA, headB->next);
return headB;
}
}
/*
Get Nth element from the end in a linked list of integers
Number of elements in the list will always be greater than N.
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
int GetNode(Node *head,int positionFromTail)
{
static int counter;
int final;
if(head->next != NULL)
{
final = GetNode(head->next, positionFromTail);
counter++;
}else{
counter = 0;
}
if(counter == positionFromTail) final = head->data;
return final;
}