-
Notifications
You must be signed in to change notification settings - Fork 0
/
Singly Linked List.cpp
261 lines (256 loc) · 5.11 KB
/
Singly Linked 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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert();
void lastinsert();
void random_insert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
int main()
{
int choice = 0;
while (choice != 9)
{
printf("\n...Choose One Option from the following List...\n");
printf("\nl.Insert in begining\n2.Insert at last\n3.Insert item at any location\n4.Delete from Beginning\n5.Delete from last\n6.Delete item from any location\n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice: ");
scanf("\n%d", &choice); // choice=1
switch (choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
random_insert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Invalid Choice..");
}
}
return 0;
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node *));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value: ");
scanf("%d", &item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("Item inserted");
}
}
void lastinsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value: ");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
ptr->next = NULL;
head = ptr;
printf("Item inserted");
}
else
{
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr->next = NULL;
printf("Item inserted");
}
}
}
void random_insert()
{
int i, loc, item;
struct node *ptr, *temp;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("Enter Value: ");
scanf("%d", &item);
ptr->data = item;
printf("\nEnter the location: ");
scanf("%d", &loc);
temp = head;
for (i = 1; i < loc; i++)
{
temp = temp->next;
if (temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr->next = temp->next;
temp->next = ptr;
printf("Item inserted");
}
}
void begin_delete()
{
struct node *ptr;
if (head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("Item deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr, *ptrl;
if (head == NULL)
{
printf("\nList is empty");
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ..\n");
}
else
{
ptr = head;
while (ptr->next != NULL)
{
ptrl = ptr;
ptr = ptr->next;
}
ptrl->next = NULL;
free(ptr);
printf("Item deleted\n");
}
}
void random_delete()
{
struct node *ptr, *ptrl;
int loc, i;
printf("\n Enter the location: ");
scanf("%d", &loc);
ptr = head;
for (i = 0; i < loc; i++)
{
ptrl = ptr;
ptr = ptr->next;
if (ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptrl->next = ptr->next;
free(ptr);
printf("Item deleted at loc %d", loc);
}
void search()
{
struct node *ptr;
int item, i = 0, flag;
ptr = head;
if (ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter searching item: ");
scanf("%d", &item);
while (ptr != NULL)
{
if (ptr->data == item)
{
printf("Item found at location %d ", i + 1);
flag = 0;
}
else
{
flag = 1;
}
i++;
ptr = ptr->next;
}
if (flag == 1)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;
if (ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nPrinting values while (ptr!=NULL).\n");
while (ptr != NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
}