forked from shubhidua/Hacktoberfest-20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLink-List.cpp
268 lines (239 loc) · 4.71 KB
/
Link-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
262
263
264
265
266
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
} *start = NULL;
void create();
void display();
void insertBegin();
void insertEnd();
void insertMid();
int length();
void deleteBegin();
void deleteEnd();
void deleteMid();
int main()
{
while (1)
{
printf("\n1. Create a Linked List \n2. Display the Linked List\n3. Insert At the Beginning\n4. Insert At the End\n5. Insert in the Middle\n6. Delete from the Beginning\n7. Delete from the End\n8. Delete from the mid\n9. Exit\n");
int a;
scanf("%d", &a);
switch (a)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insertBegin();
break;
case 4:
insertEnd();
break;
case 5:
insertMid();
break;
case 6:
deleteBegin();
break;
case 7:
deleteEnd();
break;
case 8:
deleteMid();
break;
case 9:
exit(0);
break;
}
}
}
void create()
{
char ch;
struct node *newnode, *curnode;
start = NULL;
do
{
newnode = (struct node *)malloc(sizeof(struct node *));
printf("\nEnter the data ");
scanf("%d", &newnode->data);
if (start == NULL)
{
curnode = newnode;
start = newnode;
}
else
{
curnode->next = newnode;
curnode = newnode;
}
printf("\nDo You want to Enter More ");
ch = getch();
} while (ch != 'n');
curnode->next = NULL;
}
void display()
{
if (start == NULL)
{
printf("\nEmpty List\n");
}
else
{
struct node *temp = start;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
}
void insertBegin()
{
struct node *newnode = (struct node *)malloc(sizeof(struct node *));
printf("\nEnter the Data ");
scanf("%d", &newnode->data);
newnode->next = start;
start = newnode;
}
void insertEnd()
{
struct node *newnode = (struct node *)malloc(sizeof(struct node *));
printf("\nEnter the data ");
scanf("%d", &newnode->data);
if (start == NULL)
{
start = newnode;
}
else
{
struct node *temp = start;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
}
newnode->next = NULL;
}
int length()
{
int len=0;
if (start == NULL)
{
return 0;
}
else
{
struct node *temp = start;
while (temp != NULL)
{
len++;
temp = temp->next;
}
return len;
}
}
void insertMid()
{
struct node *newnode = (struct node *)malloc(sizeof(struct node*));
int pos;
printf("\nEnter the position ");
scanf("%d",&pos);
if(pos>length()||pos==0)
{
printf("\nInvalid Position");
}
else
{
printf("\nEnter the data ");
scanf("%d",&newnode->data);
if(start==NULL)
{
start = newnode;
}
else
{
struct node* temp=start;
int count=0;
while(count<pos-1)
{
temp=temp->next;
count++;
}
newnode->next = temp->next;
temp->next = newnode;
}
}
}
void deleteBegin()
{
if(start==NULL)
{
printf("\nInvalid Operation");
}
struct node* temp = start;
start = temp->next;
free(temp);
}
void deleteEnd()
{
if(start==NULL)
{
printf("\nList is Empty");
}
else if(start->next == NULL)
{
free(start->next);
start = NULL;
}
else
{
struct node* temp = start;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
free(temp->next);
temp->next = NULL;
}
}
void deleteMid()
{
int pos;
printf("\nEnter the position ");
scanf("%d",&pos);
if(pos>length()||pos==0)
{
printf("\nInvalid Position");
}
else if(pos==length())
{
deleteEnd();
}
else if(pos==1)
{
deleteBegin();
}
else
{
struct node* cur = start;
struct node* prev = NULL;
int count = 1;
while(count!=pos)
{
count++;
prev= cur;
cur = cur->next;
}
prev->next = cur->next;
free(cur);
}
}