-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlistalloperations.c
266 lines (240 loc) · 4.6 KB
/
linkedlistalloperations.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
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
/*Structure declaration for the node*/
struct node
{
int info;
struct node *link;
}*start;
/*This function will create a new linked list*/
void Create_List(int data)
{
struct node *q, *tmp;
/*Dynamic memory is been allocated for a node*/
tmp= (struct node*)malloc (sizeof (struct node));
tmp->info=data;
tmp->link=NULL;
if (start==NULL) /*If list is empty*/
start = tmp;
else
{ /*Element inserted at the end*/
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of create_list()*/
/*This function will add new element at the beginning of the linked list*/
void AddAtBeg(int data)
{
struct node *tmp;
tmp=(struct node*)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
}/*End of addatbeg()*/
/*Following function will add new element at any position*/
void AddAfter (int data ,int pos)
{
struct node *tmp,*q;
int i;
q=start;
/*Finding the position to add new element to the linked list*/
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
printf ("\n\n There are less than %d elements",pos);
getch();
return;
}
}/*End of for*/
tmp=(struct node*)malloc(sizeof (struct node));
tmp->info=data;
tmp->link=q->link;
q->link=tmp;
}/*End of addafter()*/
/*
Delete any element from the linked list*/
void Del(int data)
{
struct node *tmp,*q;
if (start->info == data)
{
tmp=start;
start=start->link; /*First element deleted*/
free(tmp);
return;
}
q=start;
while(q->link->link!= NULL)
{
if(q->link->info == data) /*Element deleted in between*/
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
return;
}
q=q->link;
}/*End of while */
if(q->link->info==data) /*Last element deleted*/
{
tmp=q->link;
free(tmp);
q->link=NULL;
return;
}
printf ("\n\nElement %d not found",data);
getch();
}/*End of del()*/
/*This function will display all the element(s) in the linked list*/
void Display()
{
struct node *q;
if(start == NULL)
{
printf ("\n\nList is empty");
return;
}
q=start;
printf("\n\nList is : ");
while(q!=NULL)
{
printf ("%d ", q->info);
q=q->link;
}
printf ("\n");
getch();
}/*End of display() */
/*Function to count the number of nodes in the linked list*/
void Count()
{
struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->link;
cnt++;
}
printf ("Number of elements are %d\n",cnt);
getch();
}/*End of count()*/
/*This function will reverse the linked list*/
void Rev()
{
struct node *p1,*p2,*p3;
if(start->link==NULL) /*only one element*/
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}/*End of rev()*/
/*Function to search an element from the linked list*/
void Search(int data)
{
struct node *ptr = start;
int pos = 1;
/*searching for an element in the linked list*/
while(ptr!=NULL)
{
if (ptr->info==data)
{
printf ("\n\nItem %d found at position %d", data, pos);
getch();
return;
}
ptr = ptr->link;
pos++;
}
if (ptr == NULL)
printf ("\n\n Item %d not found in list", data);
getch ();
}
int main()
{
int choice, n, m, position,i;
start=NULL;
while(1)
{
/*clrscr();*/
printf ("1.Create List\n");
printf ("2.Add at beginning\n");
printf ("3.Add after \n");
printf ("4.Delete\n");
printf ("5.Display\n");
printf ("6.Count\n");
printf ("7.Reverse\n");
printf ("8.Search\n");
printf ("9.Quit\n");
printf ("\nEnter your choice:");
scanf ("%d",&choice);
switch (choice)
{
case 1:
printf ("\n\nHow many nodes you want:");
scanf ("%d",&n);
for(i = 0;i<n;i++)
{
printf ("\nEnter the element:");
scanf ("%d",&m);
Create_List(m);
}
break;
case 2:
printf ("\n\nEnter the element : ");
scanf ("%d",&m);
AddAtBeg(m);
break;
case 3:
printf ("\n\nEnter the element:");
scanf ("%d",&m);
printf ("\nEnter the position after which this element is inserted:");
scanf ("%d",&position);
AddAfter(m,position);
break;
case 4:
if (start == NULL)
{
printf("\n\nList is empty");
continue;
}
printf ("\n\nEnter the element for deletion:");
scanf ("%d",&m);
Del(m);
break;
case 5:
Display();
break;
case 6:
Count();
break;
case 7:
Rev();
break;
case 8:
printf("\n\nEnter the element to be searched:");
scanf ("%d",&m);
Search(m);
break;
case 9:
exit(0);
default:
printf ("\n\nWrong choice");
return 0;
}/*End of switch*/
}/*End of while*/
}/*End of main()*/