-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.c
More file actions
356 lines (297 loc) · 6.43 KB
/
linked_list.c
File metadata and controls
356 lines (297 loc) · 6.43 KB
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
#include<stdio.h>
#include<stdlib.h>
typedef struct sll {
int data;
struct sll *next;
}node;
/*
* Create single node in linked list
*
**/
node* create_node(int adata) {
node *sll_node = (node *)malloc(sizeof(node));
sll_node->data = adata;
sll_node->next = 0;
return sll_node;
}
/**
* insert node in linked list
**/
void insert_node(node **head,int adata) {
node *temp_node = create_node(adata);
if(*head == 0) {
*head = temp_node;
}
else {
temp_node->next = *head;
*head = temp_node;
}
}
/**
*print linked list
* */
void print_ll(node *head){
while(head != 0) {
printf("%d \t",(head->data));
head = head->next;
}
printf("\n");
}
void do_swap1(node **h1,node **h2) {
int temp = (*h1)->data;
(*h1)->data = (*h2)->data;
(*h2)->data = temp;
}
void do_swap(node **h1,node **h2) {
//printf("Before swap h1 = %d and h2 = %d\n",(*h1)->data,(*h2)->data);
node *t = *h1;
*h1 = *h2;
*h2 = t;
//int t = (*h1)->data;
//(*h1)->data = (*h2)->data;
//(*h2)->data = t;
//printf("After swap h1 = %d and h2 = %d\n",(*h1)->data,(*h2)->data);
}
/*
* 1.Use of swapping with double star or with reference to pointer
*
* Notice power of indirection
* A single variable able to store 2 values
* eg.node** indir = &temp;
* now this indir variable store 2 values
* 1.Complete node (with data and address)
* a.To assign this
* *indir = head1;
* b.To use this
* *indir
* 2.Reference to next node (Reference is alias of object)
* semantics of reference :
* IF you want to change value in variable you must have reference.
* it is stored as
* *indir = &(head1->next); //Store reference to the next variable
*
* NOTE in C
* *node->next interpreted as (*node->next) which has
* different semantics than (*node)->next
*/
/**
* input : 2 sorted list
* output : combined sorted list without extra space
*
**/
node* merge2_list_2(node** head1,node** head2) {
node* head3 = 0;
node** indir = &head3;
if(*head1 == 0 && *head2 == 0)
return 0;
else if(*head2 == 0)
return *head1;
else if(*head1 != 0) {
while(*head1 != 0) {
if((*head1)->data > (*head2)->data) {
do_swap(head1,head2);
}
*indir = *head1;
indir = &((*head1)->next);
*head1 = *indir;
}
*indir = *head2;
return head3;
}
}
/*
*
* version 1 : just input parameters pointer instead of refrences
**/
node* merge2_list_1(node *head1,node *head2) {
node *temp3 = 0;
node **indir = &temp3;
if(head1 == 0 && head2 == 0)
return 0;
else if(head2 == 0)
return head1;
else if(head1 != 0) {
while(head1 != 0) {
if(head1->data > head2->data) {
do_swap(&head1,&head2);
}
*indir = head1;
indir = &head1->next;
head1 = *indir;
}
*indir = head2;
return temp3;
}
}
/**
* Reverse linked list with using double pointer to head
* **/
static void print_rev_ll(node** head_ref) {
node* curr = *head_ref;
node* next = 0;
node* prev = 0;
while(curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head_ref = prev;
}
/**
*Reverse linked list with using single pointer
*
* **/
static node * print_rev_ll1(node *head) {
node *next;
node *prev = 0;
next = head;
while(head) {
next = head->next;
head ->next = prev;
prev = head;
head = next;
}
return prev;
}
/**
* reverse list between m and n.
* Idea : just swap the data instead of modifying pointer
* This approach is problematic as we just swap 2 data items at a time.
* Given 1->2->3->4->5->NULL, m = 2 and n = 4,
* return 1->4->3->2->5->NULL.
* **/
static node * reverse_ll_between(node* head,int m,int n) {
node *main_node = head;
int t = n-m;
int isRun = 0;
node *prev = 0;
node *temp = 0;
while(m != 0 && main_node != 0) {
prev = main_node;
main_node = main_node->next;
m--;
}
if(m != 0 || main_node == 0) {
return head;
}
else {
while(main_node != 0 && t != 0) {
temp = main_node->next;
do_swap1(&temp,&main_node);
//main_node->next = prev;
//prev = main_node;
main_node = temp;
t--;
}
}
//print_ll(head); to verify entries are clearly swapped
return main_node;
}
/**
* Delete current node in the linked list.
* **/
static void delete_node(node **node_ref) {
node * curr = *node_ref;
node *next = curr->next;
if(next != 0) {
//store next node information in the current node
curr->data = next->data;
curr->next = next->next;
}
next = 0;
free(next);
}
//Y-Problem : solution to detect Y problem in linked list
//Time complexity : O(n)
static void detect_y_problem(node *head1,node *head2) {
int l1;
int l2;
int drift;
node *temp2;
node *temp = head1;
while(head1 !=0) {
temp = temp->next;
l1++;
}
temp = head2;
while(head2 != 0) {
temp = temp->next;
l2++;
}
//calculate drift and traverse the distance upto drift point
drift = l1-l2;
if(drift < 0) {
drift = -drift;
}
temp = head1;
temp2 = head2;
while(drift != 0) {
if(l1 < l2) {
temp = temp->next;
}else {
temp2 = temp2->next;
}
drift--;
}
while ( temp != 0 && temp2 != 0 ) {
if(temp == temp2) {
printf ("\nY detected \n");
break;
}
temp = temp->next;
temp2 = temp2->next;
}
}
//P-problem : Solution to detect P condition in linked list.
//Time complexity : O(n)
static void detect_cycle(node *head) {
int found_cycle = 0;
node *hare = head; //Initially both run at same speed
node *tortoise = head;
while(hare) {
hare = hare->next; //hare runs twice as tortoise
if(hare) {
hare = hare->next;
tortoise = tortoise->next;
if(hare == tortoise) {
found_cycle = 1;
break;
}
}
}
if(found_cycle == 1) {
printf("\nfound cycle \n");
}
else {
printf("\nnot found cycle \n");
}
}
int main() {
int i=0 ;
int a[] = {8,7,2,1};
int b[] = {10,8,5,4};
node* head = 0;
node* head2 = 0;
for(;i<4;i++) {
insert_node(&head,a[i]);
insert_node(&head2,b[i]);
}
print_ll(head);
print_ll(head2);
node* h = merge2_list_2(&head,&head2);
// node *h = merge2_list_1(&head,&head2);
//node *h = MergeLists(head,head2);
//node* h = merge2_list_1(head,head2);
print_ll(h);
print_rev_ll(&h);
print_ll(h);
node *t = h->next->next;
delete_node(&t);
print_ll(h);
printf (" \n reversing list ...\n");
node* h1 = print_rev_ll1(h);
print_ll(h1);
reverse_ll_between(h1,2,4);
print_ll(h1);
}