-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinked_list_merge_sorted_list.cpp
419 lines (380 loc) · 7.51 KB
/
linked_list_merge_sorted_list.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// reverse a L.L
//reverse pointers, not suggestible to copy and reverse the data.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node * next;
Node(int d)
{
data=d;
next=NULL;
}
};
// aproach if multiple linked lists have to be created at the same time
/*
class LinkedList
{
Node * head;
Node * tail;
public:
LinkedList()
{
head = NULL;
tail = NULL;
}
void insert()
{
}
};
*/
int findLinkedLength(Node * head)
{
int len=0;
while(head!=NULL)
{
head=head->next;
len++;
}
return len;
}
//changes should reflect in main. Use &, pass by refernce.
void insertAtHead(Node * &head, int data)
{
Node*n=new Node(data);
// Node *n .... dynamic memory allocation
// the object persists even after function is over
// Node n.....static memory allocation. The object also gets destroyed after function execution.
n->next=head;
head=n;
}
void insertAtTail(Node * &head , int data)
{
if(head==NULL)
{
head= new Node(data);
return;
}
Node * temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new Node(data);
return;
}
void insertInMiddle(Node *&head, int data, int posAfter)
{
if(head==NULL||posAfter==0)
{
insertAtHead(head,data);
}
else if(posAfter>=findLinkedLength(head))
{
insertAtTail(head,data);
}
//Insert ai Middle
else
{
Node *n= new Node(data);
Node * temp= head ;
while(posAfter>1)
{
temp=temp->next;
posAfter--;
}
n->next=temp->next;
temp->next=n;
}
}
bool searchRecursive(Node*head,int key)
{
if(head!=NULL)
{
if(head->data==key)
{
return true;
}
return searchRecursive(head->next, key);
}
return false;
}
bool searchIterative(Node*head, int key)
{
while(head!=NULL)
{
if(head->data==key)
{
return true;
}
head=head->next;
}
return false;
}
//changes should not reflect in main. Use pass by value.
void print(Node * head)
{
Node * temp= head;
while(temp != NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<endl;
}
void deleteAtHead(Node *& head)
{//'delete' takes operand as the memory address which has to be deleted
//delete temp where temp=NULL is not an error
//delete NULL is an eroor
if(head!=NULL)
{
Node* temp=head;
head=head->next;
delete temp;
return;
}
return;
}
void deleteAtTail(Node *&head)
{ Node * tempHead=head;
if (head!=NULL && head->next!=NULL)
{
while((tempHead->next)->next!=NULL)
{
tempHead=tempHead->next;
}
Node * temp=tempHead->next;
tempHead->next=NULL;
delete temp;
return;
}
else if (head!=NULL)
{
delete head;
head = NULL;
}
return;
}
void deleteInMiddle(Node * &head, int pos)
{
if(pos>0)
{
if(findLinkedLength(head)==pos)
{
deleteAtTail(head);
return;
}
else if (pos==1)
{
deleteAtHead(head);
return;
}
else if(pos<findLinkedLength(head))
{
Node * temp=head;
while(pos>2)
{
temp=temp->next;
pos--;
}
Node*dTemp=temp->next;
temp->next=dTemp->next;
delete dTemp;
return;
}
}
return;
}
void buildList(Node *&head)
{
int data;
cin>>data;
while(data!=-1)
{
insertAtTail(head,data);
cin>>data;
}
return;
}
// return type istrea& for cascading
istream& operator>>(istream &is, Node *& head)
{
buildList(head);
return is;
}
ostream& operator<<(ostream &os, Node * head)
{
print(head);
return os;
}
void reverse(Node * &head)
{
Node * p,*c,*n;
p=NULL;
c=head;
while(c!=NULL)
{
n=c->next;
c->next=p;
p=c;
c=n;
}
head=p;
}
Node * reverseRecursive(Node * head)
{
if(head==NULL || head->next==NULL)
{
return head;
}
Node * smallHead= reverseRecursive(head->next);
Node *c=head;
c->next->next=c;
c->next=NULL;
return smallHead;
}
int midPoint(Node * head)
{
if(head==NULL)
{
return -1;
//-1 symbolises L.L is empty and no midpoint exists
// function should not return null to main as this will give runtime error NULL->data
}
Node *fast=head->next, *slow=head;
while(!((fast==NULL) || (fast->next==NULL)))
{
fast=(fast->next)->next;
slow=slow->next;
}
return slow->data;
}
int findElementEnd(Node*head,int posEnd)
{
if(head==NULL || posEnd==0)
{
return -1;
}
Node * slow=head, *fast=head;
posEnd--;
while(posEnd && fast->next!=NULL)
{
fast=fast->next;
posEnd--;
}
while(fast->next!=NULL)
{
fast=fast->next;
slow=slow->next;
}
return slow->data;
}
void mergeSortedLists(Node*&head1, Node*&head2)
{
if(head1!=NULL && head2!=NULL)
{
Node *ptr1=head1, *ptr2=head2;
if(ptr1->data > ptr2->data)
{
head1=ptr2;
ptr2=ptr2->next;
}
else
{
ptr1=ptr1->next;
}
Node*curr=head1;
while(ptr1!=NULL && ptr2!=NULL)
{
if(ptr1->data>ptr2->data)
{
curr->next=ptr2;
curr=ptr2;
ptr2=ptr2->next;
}
else
{
curr->next=ptr1;
curr=ptr1;
ptr1=ptr1->next;
}
}
if(ptr2!=NULL)
{
curr->next=ptr2;
}
else if(ptr1!=NULL)
{
curr->next=ptr1;
}
curr=NULL;
head2=NULL;
ptr1=NULL;
ptr1=NULL;
}
else if(head1==NULL)
{
head1=head2;
}
}
int main()
{
Node * head1=NULL;
Node * head2=NULL;
cin>>head1;
cin>>head2;
mergeSortedLists(head1,head2);
cout<<head1;
//cout<<head;
//head=reverseRecursive(head);
//cout<<head;
//int midData=midPoint(head);
//cout<<midData<<endl;
// cout<<findElementEnd(head,7)<<endl;
//cout<<findElementEnd(head,63)<<endl;
//cout<<findElementEnd(head,0)<<endl;
//cout<<findElementEnd(head,4)<<endl;
//cout<<*mid;
//cout<<head2;
// buildList(head);
// print(head);
/*
insertAtHead(head,5);
insertAtHead(head,4);
insertAtHead(head,3);
insertAtTail(head,6);
insertInMiddle(head,2,3);
print(head);
*/
/*
if(searchRecursive(head,6))
{
cout<<"recursiveSearch: Element is present"<<endl;
}
else
{
cout<<"recursiveSearch: Element is not present"<<endl;
}
if(searchIterative(head,5))
{
cout<<"iterativeSearch: Element is present"<<endl;
}
else
{
cout<<"iterativeSearch: Element is not present"<<endl;
}
*/
/*
deleteInMiddle(head,3);
print(head);
deleteAtTail(head);
print(head);
deleteAtHead(head);
print(head);
deleteInMiddle(head,2);
print(head);
*/
return 0;
}