generated from Suvechchhaa/HacktoberFest-DSA2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSinglyLinkedList.c
290 lines (272 loc) · 6.68 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
/*
The Data Structure used here is a singly linked List.
Here instead of using a predefined linked list, I have used user input system of linked list
in which the linked list need not to be predefined and the user can create a linked list and
add new nodes to the linked list at the front, rear or any position and can also remove nodes
from the front, rear or any position of the linked list.
*/
#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node* header = NULL;
struct node *getnode()
{
struct node *new = (struct node *)malloc(sizeof(struct node));
}
// Function to insert at the front of the linked list
void insertAtFront(int data)
{
struct node* new = getnode();
new->data = data;
// Pointer of new will be assigned to previous header
new->link = header;
header = new;
}
// Function to insert at the end of the linked list
void insertAtEnd(int data)
{
struct node *new = getnode();
struct node *temp = getnode();
new->data = data;
new->link = NULL;
if(header==NULL)
{
header = new;
}
else
{
temp = header;
while (temp->link != NULL)
{
temp = temp->link;
}
temp->link = new;
}
}
// Function to insert at a position in the linked list
void insertAtPosition(int data)
{
int pos,i = 1;
struct node *new = getnode();
struct node *temp = getnode();
new->data = data;
new->link = NULL;
temp = header;
printf("\tEnter position : ");
scanf("%d", &pos);
if (header == NULL)
{
if(pos == 1)
{
insertAtFront(data);
}
else
{
printf("\nList is empty\n");
}
}
else if(pos == 1)
{
insertAtFront(data);
}
else
{
while (i < pos - 1)
{
temp = temp->link;
i++;
}
new->link = temp->link;
temp->link = new;
}
}
// Function to delete from the front of the linked list
int deleteFirst()
{
int data;
struct node* temp = getnode();
if (header == NULL)
{
printf("\nList is empty\n");
}
else
{
temp = header;
data = temp->data;
header = header->link;
free(temp);
//Return the deleted value
return data;
}
}
// Function to delete from the end of the linked list
int deleteEnd()
{
int data;
struct node *temp1 = getnode();
struct node *temp2 = getnode();
if (header == NULL)
{
printf("\nList is Empty\n");
}
else
{
temp1 = header;
while (temp1->link != NULL)
{
temp2 = temp1;
temp1 = temp1->link;
}
data = temp1->data;
temp2->link = NULL;
free(temp1);
return data;
}
}
// Function to delete from a position in the linked list
int deletePosition()
{
int pos,data,i = 1;
struct node *temp = getnode();
struct node *position = getnode();
printf("\tEnter position : ");
scanf("%d", &pos);
if (header == NULL)
{
printf("\nList is empty\n");
}
else if(pos == 1)
{
deleteFirst();
}
else
{
temp = header;
// Traverse till position
while (i < pos - 1)
{
temp = temp->link;
i++;
}
position = temp->link;
data = position->data;
// Change Links
temp->link = position->link;
// Free memory
free(position);
return(data);
}
}
// Function to display the linked list
int displaylist()
{
struct node* temp = getnode();
// List is not empty
if (header != NULL)
{
printf("\nThe linked list is : ");
temp = header;
printf("[header]");
while (temp != NULL)
{
printf("-->[%d]",temp->data);
temp = temp->link;
}
printf("[/]");
}
printf("\n");
}
int main()
{
int option,choice,data,value;
while(1)
{
printf("\nLinked list operations...\n1.Insert\n2.Delete\n3.Displaylist\n4.Exit");
printf("\nEnter option : ");
scanf("%d",&option);
switch(option)
{
case 1:
{
printf("\t1.Insert at Front\n\t2.Insert at End\n\t3.Insert at a position");
printf("\n\tEnter your choice : ");
scanf("%d",&choice);
printf("\tEnter the data to be inserted : ");
scanf("%d",&data);
switch(choice)
{
case 1:
{
insertAtFront(data);
break;
}
case 2:
{
insertAtEnd(data);
break;
}
case 3:
{
insertAtPosition(data);
break;
}
default:
{
printf("\tEnter the right choice.");
break;
}
}
displaylist();
break;
}
case 2:
{
printf("\t1.Delete from Front\n\t2.Delete from End\n\t3.Delete from a position");
printf("\n\tEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
value = deleteFirst();
break;
}
case 2:
{
value = deleteEnd();
break;
}
case 3:
{
value = deletePosition();
break;
}
default:
{
printf("\nEnter the right choice.");
}
}
displaylist();
break;
}
case 3:
{
displaylist();
break;
}
case 4:
{
exit(1);
break;
}
default:
{
printf("Enter the correct option.\n");
}
}
}
}