-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkList.c
283 lines (270 loc) · 6.11 KB
/
LinkList.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
} *START = NULL;
int Count(); // Function Prototype or Function Decleraton
void InsertAtEnd()
{
struct node *ptr, *temp;
int key;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("Insufficient Space\n");
return;
}
else
{
printf("Enter the element you want to insert at end ");
scanf("%d", &key);
temp->data = key;
temp->link = NULL;
if (START == NULL)
{
START = temp;
}
else
{
ptr = START;
while (ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = temp;
}
}
}
void InsertAtBeg()
{
struct node *temp;
int key;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("Insufficient Space\n");
}
else
{
printf("Enter the element you want to insert at Begning ");
scanf("%d", &key);
temp->data = key;
temp->link = START;
START = temp;
}
}
void InsertAtIndex()
{
struct node *ptr, *temp;
int key, index;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("Insufficient Space\n");
}
else
{
printf("Enter the index at which you want to insert the element ");
scanf("%d", &index);
if (index == 1)
{
InsertAtBeg();
return;
}
else
{
if (index <= Count())
{
printf("Enter the element you want to insert ");
scanf("%d", &key);
ptr = START;
for (int i = 0; i < index - 2; i++)
{
ptr = ptr->link;
}
temp->data = key;
temp->link = ptr->link;
ptr->link = temp;
}
else
{
printf("Please enter a valid Index!\n");
}
}
}
}
void deleteAtBeg()
{
struct node *ptr = START;
if (ptr == NULL)
{
printf("Linked List is Empty\n");
}
else
{
START = ptr->link;
ptr->link = NULL;
free(ptr);
}
}
void deleteAtEnd()
{
struct node *ptr = START, *temp;
if (ptr == NULL)
{
printf("Linked List is Empty\n");
}
else
{
if (ptr->link == NULL)
{
START = NULL;
free(ptr);
}
else
{
// // do{
// // ptr = ptr->link;
// // } while (ptr != NULL);
// while(ptr->link->link != NULL){
// ptr = ptr->link;
// }
// ptr->link = NULL;
for (int i = 0; i < Count() - 2; i++)
{
ptr = ptr->link;
}
temp = ptr->link;
ptr->link = NULL;
free(temp);
}
}
}
void deleteAtIndex()
{
int del;
struct node *ptr = START, *temp;
if (ptr == NULL)
{
printf("Linkedlist is Empty.\n");
}
else
{
printf("Please enter the index of node you want to delete ");
scanf("%d", &del);
if (del == 1)
{
deleteAtBeg();
}
else if (del == Count())
{
deleteAtEnd();
}
else if (del < Count())
{
for (int i = 0; i < del - 2; i++)
{
ptr = ptr->link;
}
temp = ptr->link;
ptr->link = ptr->link->link;
free(temp);
}
else
{
printf("Invalid index.\n");
}
}
}
int Count() // Function Defination
{
struct node *ptr;
int count = 0;
ptr = START;
while (ptr != NULL)
{
ptr = ptr->link;
count++;
}
return count;
}
void display()
{
struct node *ptr;
ptr = START;
if (START == NULL)
{
printf("Linked list is Empty\n");
}
else
{
while (ptr->link != NULL)
{
printf("%d\t", ptr->data);
ptr = ptr->link;
}
printf("%d\n", ptr->data);
}
}
// void display(struct node *ptr)
// {
// if(ptr = NULL){
// return;
// }
// printf("%d\t",ptr->link);
// display(ptr->link);
// }
int main()
{
int choice;
while (1)
{
printf("Various Operations on Singly Linked List :\n");
printf("Enter 1 to insert an element at begning\n");
printf("Enter 2 to insert an element at ending \n");
printf("Enter 3 to insert an element at some inde\n");
printf("Enter 4 to display the Linked List\n");
printf("Enter 5 to count the elements of Linked List\n");
printf("Enter 6 to delete an element at begning\n");
printf("Enter 7 to delete an element at ending\n");
printf("Enter 8 to delete an element at some index\n");
printf("Enter 9 to exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
InsertAtBeg();
break;
case 2:
InsertAtEnd();
break;
case 3:
InsertAtIndex();
break;
case 4:
display(START);
break;
case 5:
printf("Length of our Linked List is %d\n", Count()); // Function Call
break;
case 6:
deleteAtBeg();
break;
case 7:
deleteAtEnd();
break;
case 8:
deleteAtIndex();
break;
case 9:
printf("Thank You!\n");
exit(0);
break;
default:
printf("Please enter a valid number\n");
}
}
return 0;
}