forked from FourierSignal/Hello-World
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle_linked_list.c
More file actions
388 lines (332 loc) · 8.63 KB
/
Copy pathSingle_linked_list.c
File metadata and controls
388 lines (332 loc) · 8.63 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
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node_struct{
int data;
struct node_struct *next;
}node;
node *head_ptr = NULL;
node* append_node(node **head,int val)
{
node *curr;
node *last_node;
// first = *head;
printf("Append_list\n");
last_node=(node *)malloc(sizeof(struct node_struct));
last_node->data = val;
last_node->next = NULL;
if(*head == NULL)
*head = last_node;
else
{
for(curr = *head; curr->next!= NULL; curr=curr->next)
;
curr->next = last_node;
}
return (*head);
}
node* addnode_at_head(node **head,int val)
{
node *first = (node *)malloc(sizeof(struct node_struct));
first->data = val;
first->next = NULL;
if(*head == NULL)
*head = first;
else
{
first->next = (*head);
(*head) = first;
}
}
/*
best logic i found till now for delete node is
1) handle case: if head is NULL
2) handle junking of head node as seperate case
3) handle junking of nodes other than head node seperately
here dont junk: curr node.
instead junk : curr->next node
this helps in skipping the last node case (deleting last node).
*/
node* delete_node(node **head,int val)
{
node *curr;
node *junk_node;
printf("delete_node\n");
if(*head == NULL)
{
printf("Head is NULL\n");
//return *head;
return NULL;
}
else if((*head)->data == val)
{
junk_node = (*head);
(*head)=(*head)->next;
}
else
{
//note : we can't generalize logic for deleting first node and deleting any other node
// or atleast we need to handle or check wether we are deleting first node.
/*
for(curr = *head;(curr->next != NULL); curr=curr->next)
{
if(curr->data == val)
{
if(*head == curr)
{
junk_node = curr;
*head = curr->next;
break;
}
else
{
junk_node = curr;
??? here is the problem: how we handle prev pointer
break;
}
}
}
*/
/*
Even with prev we can't generalize logic)
prev = *head;
for(curr = *head;(curr->next != NULL); curr=curr->next)
{
if(curr->data == val)
{
junk_node = curr;
prev->next = curr-> next;
// in head node case: this statement fails:
// head node case: (*head)->next = (*head)->next; won't skip the current node
// It replaces curr->next with same value => no effect
break;
}
prev=curr;
}
free(junk_node);
*/
/*
prev = *head;
for(curr = *head;(curr->next != NULL) &&(curr->data==val); curr=curr->next)
we need to handle head case.
and also last node case.
so better is to go for handling head seperately and junking curr->next node.
*/
for(curr = *head;(curr->next != NULL); curr=curr->next)
{
if(curr->next->data == val)
{
junk_node = curr->next;
curr->next = curr->next->next;
//here there is no problem with last node case
//if junk_node is last node curr->next->next is NULL
//there is not a problem becuse we are dereferencing curr->next but not curr->next->next
break;
}
}
}
free(junk_node);
return (*head);
}
node* Insert_before_node(node **head,int val)
{
node *curr;
node *new_node;
node *temp;
printf("Insert_node\n");
if(*head == NULL)
{
printf("Head is NULL\n");
return *head;
}
new_node = (node *)malloc(sizeof(struct node_struct));
printf("Enter value to be inserted\n");
scanf("%d",&(new_node->data));
new_node->next = NULL;
if((*head)->data == val)
{
new_node->next = (*head);
(*head)=new_node;
}
else
{
for(curr = *head;(curr->next != NULL); curr=curr->next)
{
if(curr->next->data == val)
{
temp = curr->next;
curr->next = new_node;
new_node->next = temp;
break;
}
}
}
return (*head);
}
node* Insert_after_node(node **head,int val)
{
node *temp;
node *new_node;
node *curr;
printf("Insert_After_a node\n");
if(*head == NULL)
{
printf("Head is NULL\n");
return *head;
}
new_node = (node *)malloc(sizeof(struct node_struct));
printf("Enter value to be inserted\n");
scanf("%d",&(new_node->data));
new_node->next = NULL;
#if 0
for(curr = *head;(curr->next != NULL); curr=curr->next)
{
if(curr->data == val)
{
temp = curr->next;
curr->next = new_node;
new_node->next = temp;
break;
}
}
if(curr->next==NULL)
{
curr->next = new_node;
}
#endif
for(curr = *head;(curr!= NULL); curr=curr->next)
{
if(curr->data == val)
{
temp = curr->next;
curr->next = new_node;
new_node->next = temp;
break;
}
}
return (*head);
}
print_list(node *head)
{
printf("print_list\n");
node *curr;
for(curr=head;curr!=NULL;curr=curr->next)
{
printf("|%d|--->",curr->data);
}
printf("NULL\n");
}
int main()
{
char choice;
int val;
printf(" %p \n",&head_ptr);
printf("Enter A : add a node @End\n");
printf("Enter B : add a node @head\n");
printf("Enter I : Insert a node Before a node\n");
printf("Enter J : Insert a node After a node\n");
printf("Enter D: Delete a node \n");
printf("Enter P: print list\n");
printf("Enter Q: Quit\n");
while (1)
{
printf("Enter choice \n");
choice=getchar();
printf("choice=%d %c\n",choice,choice);
switch(choice)
{
case 'A':
printf("Enter val to append\n");
scanf("%d",&val);
printf("val=%d\n",val);
append_node(&head_ptr,val);
break;
case 'B':
printf("Enter val @ head\n");
scanf("%d",&val);
addnode_at_head(&head_ptr,val);
break;
case 'D':
printf("Enter val to be deleted\n");
scanf("%d",&val);
delete_node(&head_ptr,val);
break;
case 'I':
printf("Enter val before which node to be Inserted\n");
scanf("%d",&val);getchar();getchar();getchar();
Insert_before_node(&head_ptr,val);
break;
case 'J':
printf("Enter val after which nodeto be Inserted\n");
scanf("%d",&val);getchar();getchar();getchar();
Insert_after_node(&head_ptr,val);
break;
case 'P':
print_list(head_ptr);
break;
case 'Q':
exit(0);
break;
default:
printf("Invalid choice\n");
}
getchar();getchar();getchar();
}
return 0;
}
//##################################################################################################################
Binary Tree
Algorithm:
1. if no root node , create node, Initialise left,right pointers to NULL, point root to the node.
2. if root node exist ,
find val is left to node or right to node.
if left , pass pointer to left pointer, val
if right, pass pointer to right pointer , val
recursively
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val == (*tree)->data)
{
return *tree;
}
else if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
}
void deltree(node * tree)
{
if(tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}