forked from DSC-COEA-Ambajogai/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear Linked List
260 lines (244 loc) · 3.84 KB
/
Linear Linked List
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
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
# include<alloc.h>
struct node
{
int info;
struct node *next;
}*list;
void append();
void addbeg();
void addafter();
void delet();
void search();
void update();
void display();
void main()
{
char ch;
int choice;
list = NULL; // Initialize list
do
{
clrscr();
printf("\n1. Append\t2. Add first\t3. Addafter\t4. Delete\t5. Search\t6.update\t 7. Display");
printf("\nEnter ur choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: append(); break;
case 2: addbeg(); break;
2
case 3: addafter(); break;
case 4: delet(); break;
case 5: search(); break;
case 6: update(); break;
case 7: display(); break;
default: printf("\nWrong choice.");
}
printf("\nDo u want to cnt....");
flushall();
ch=getche();
}while(ch == 'y');
}
void append() //Insert at the end of the list
{
int x;
struct node *p, *t;
char ch;
do
{
printf("\nEnter data : ");
scanf("%d",&x);
t = malloc(sizeof(struct node)); // create a node
t->info = x;
t->next = NULL;
if(list == NULL) // List is Empty
list=t;
else // List Not Empty
{
p= list;
while(p->next != NULL) // Find Last node
p = p->next;
p->next = t; // Link new node to last node
}
printf("\n Do u want to insert more? ");
flushall();
ch=getche();
}while(ch == 'y');
}
3
void addbeg() //Insert at the first position.
{
struct node *t;
int x;
char ch;
do
{
t = malloc(sizeof(struct node)); // Create a node
printf("\n Enter data : ");
scanf("%d",&x);
t->info = x;
t->next = list;
list = t;
printf("\n Do U want to insert more? ");
flushall();
ch=getche();
}while(ch=='y');
}
void addafter() // Insert at the specified location
{
struct node *p, *t;
int pos, x,i;
char ch;
do
{
printf("\n Enter data : ");
scanf("%d",&x);
t = malloc(sizeof(struct node));// Create a node
t->info = x;
printf("\n Enter position : ");
scanf("%d",&pos);
p=list;
for(i=1 ; i<pos ; i++) // To get position
{
if(p == NULL)
break;
else
p = p->next;
}
4
if(i==pos) // Position found
{
t->next = p->next; // Link new and right node
p->next = t; // link new and left node
}
else // Less elements
printf("\n less elements ");
printf("\n DO u want to insert more ? ");
flushall();
ch=getche();
} while(ch == 'y');
}
/***** Delete a node from linked list *****/
void delet()
{
struct node *p,*r;
int x;
char ch;
do
{
printf("\nEnter data to be deleted : ");
scanf("%d",&x);
p = list;
if(p == NULL)
{
printf(" \n List Empty");
break;
}
else if(p->info == x) // x found in first node
{
printf("\n%d has been deleted",p->info);
list = p->next;
free(p);
}
else
{
r = p; // r: previous node
p = p->next; // q: current node
5
while(p != NULL)
{
if(p->info == x) // x found
{
printf("\n %d has been deleted",p->info);
r->next = p->next;
free(p);
break;
}
else // x not found
{
r = p;
p = p->next;
}
}
if(p==NULL)
printf("\n %d not found",x);
}
printf("\nDO u want to delete more? ");
flushall();
ch=getche();
}while(ch == 'y');
}
/***** Search given element in a list *****/
void search()
{
int x;
char ch;
struct node *p;
do
{
p = list;
printf("\n Enter element to be searched: ");
scanf("%d",&x);
6
while(p != NULL)
{
if(p->info==x)
break;
else
p = p->next;
}
if(p!=NULL)
printf("\n%d found ",x);
else
printf("\n %d Not Found",x);
printf("\n Do you want to search another element? ");
flushall();
ch= getche();
} while(ch=='y');
}
/***** Update a value in a node *****/
void update()
{
int x,nv;
char ch;
struct node *p;
do
{
p = list;
printf("\n Enter element to be updated: ");
scanf("%d",&x);
printf("\n Enter new value: ");
scanf("%d",&nv);
while(p != NULL)
{
if(p->info==x)
{
p->info = nv;
break;
}
else
p = p->next;
}
7
if(p!=NULL)
printf("\n %d found and updated ",x);
else
printf("\n %d Not Found",x);
printf("\n Do you want to search another element? ");
flushall();
ch= getche();
} while(ch=='y');
}
void display()
{
struct node *p;
p = list;
if(p == NULL)
printf("\n List Empty ");
else
for( p = list ; p!=NULL ; p = p->next)
printf("\n%u %3d",p,p->info);
}