-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.c
313 lines (278 loc) · 7 KB
/
SinglyLinkedList.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
310
311
312
313
// Application's Memory
/*
Heap - Free Memory
Stack - Store the function calls
Static/Global - Store the Global variables
Code/Text - Store other Statements
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
// Singly Linked List
struct Node
{
int data;
struct Node *next;
};
struct Node *q; // For accessible to all
int InsertAtBeg(int x)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
// if (head->next != NULL) // If list is not empty
// temp->next = head;
head = temp; // store temp's properties into the head
}
struct Node *InsertionWithArgs(struct Node *head, int x)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
// if (head->next != NULL) // If list is not empty
// temp->next = head;
head = temp; // store temp's properties into the head
return head;
}
void InsertionWithPointers(struct Node *head, int x)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = x;
temp->next = NULL;
if ((*head).next != NULL) // If list is not empty
temp->next = head;
head = temp;
}
void InsertAtN(int data, int n)
{
struct Node *temp1 = (struct Node *)malloc(sizeof(struct Node));
temp1->data = data;
temp1->next = NULL;
if (n == 1) // Insert at Head
{
temp1->next = head; // link the newly created Node as existing head
head = temp1; // adjust head to point to the new node
return;
}
struct Node *temp2 = head; // Start from the head
for (int i = 0; i < n - 2; i++) // n-2 = previous node of the entered position
temp2 = temp2->next;
temp1->next = temp2->next; // Store the location of the next node in newly created node
temp2->next = temp1; // link previous node to the newly created node
}
void print()
{
struct Node *temp = head;
if (head == NULL)
{
printf("\nList is empty..!!\n");
return;
}
printf("\nList is :\n");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void printWithArgs(struct Node *head)
{
if (head == NULL)
{
printf("\nList is empty..!!\n");
return;
}
printf("\nList is :\n");
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
void PrintRecursively(struct Node *hd)
{
if (hd == NULL)
{
printf("\n");
return;
}
printf("%d ", hd->data);
PrintRecursively(hd->next);
}
void del(int n)
{
struct Node *temp = head;
if (n == 1)
{
head = temp->next; // head points to second node of temp
free(temp);
return;
}
for (int i = 0; i < n - 2; i++)
temp = temp->next; // points to n-1th node
struct Node *temp2 = temp->next;
temp->next = temp2->next; // joints with n+1th node
free(temp2);
}
void Reverse()
{
struct Node *currNode, *prevNode, *nextNode;
if (head->next == NULL) // If list has only one element then what is left
return;
currNode = head;
prevNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next; // store the current address to the next node
currNode->next = prevNode; // store the address of the current node to the previous node
prevNode = currNode; // move previous node to the current node
currNode = nextNode; // move current node to the next node
}
// After the completion of the loop, head is the last node
head = prevNode;
}
void ReversePrintRecurs(struct Node *hd)
{
if (hd == NULL)
{
printf("\n");
return;
}
ReversePrintRecurs(hd->next);
printf("%d ", hd->data);
}
struct Node *RevWithArg(struct Node *top)
{
struct Node *currNode, *prevNode, *nextNode;
currNode = top;
prevNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next; // store the address of the next node
currNode->next = prevNode;
prevNode = currNode;
currNode = nextNode;
}
// After the completion of the loop, head is the last node
top = prevNode;
return top;
}
void Rev(struct Node *hd)
{
if (hd->next == NULL)
{
head = hd;
return;
}
Rev(hd->next);
struct Node *q = hd->next;
q->next = hd;
hd->next = NULL;
}
void count()
{
struct Node *q;
int count = 0;
q = head;
while (q != NULL)
{
q = q->next;
count++;
}
printf("Total %d elements in the list...\n", count);
}
void search(int data)
{
struct Node *q = head;
int pos = 1;
while (q != NULL)
{
if (q->data == data)
{
printf("%d found at position %d \n", data, pos);
return;
}
q = q->next;
pos++;
}
if (q == NULL)
printf("%d is not found in list \n", data);
}
int main(void)
{
// struct Node *head = NULL;
head = NULL; // Create a empty list
int choice = 0, n, m;
while (1)
{
printf("\n 1. Create list");
printf("\n 2. Add at beginning");
printf("\n 3. Add after");
printf("\n 4. Delete element by position");
printf("\n 5. Display");
printf("\n 6. Length of list");
printf("\n 7. Reverse ");
printf("\n 8. Search");
printf("\n 9. Quit ");
printf("\nEnter your choice :- ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nHow many node(s) you want?? ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter %d element : ", i + 1);
scanf("%d", &m);
InsertAtN(m, i + 1);
}
break;
case 2:
printf("\nEnter the element ");
scanf("%d", &m);
InsertAtBeg(m);
break;
case 3:
printf("\nEnter the element ");
scanf("%d", &m);
int pos;
printf("Enter the position after which element is inserted ");
scanf("%d", &pos);
InsertAtN(m, pos);
break;
case 4:
if (head == NULL)
{
printf("\nList is empty ");
continue;
}
print();
printf("\nEnter the element position you want to delete ");
scanf("%d", &m);
del(m);
break;
case 5:
print();
break;
case 6:
count();
break;
case 7:
Reverse();
break;
case 8:
printf("Enter the element to be search: ");
scanf("%d", &m);
search(m);
break;
case 9:
printf("\nYou're Kicked out...\n");
exit(0);
default:
printf("Invalid choice");
} // switch
} // while
}