-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingly Linked List.c
221 lines (204 loc) · 7.39 KB
/
Singly Linked List.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
/******************************************************************************
Singly Linked List
By Sreeram SP
*******************************************************************************/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *ptr,*ptr1,*ptr2,*first=NULL;
void main()
{
int ch,x;
int c;
printf("1.Insert\n2.Delete\n3.Display\n");
printf("\nEnter Choice=");
scanf("%d",&c);
switch(c)
{
case 1:
{
printf("INSERTION\n");
printf("Enter the node\n1.In the Beginning\n2.In the End\n3.After \n4.Before\n");
printf("Enter Choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:/*Entering the data In the Beginning*/
{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the data=");
scanf("%d",&ptr->info);/*Entering the data*/
ptr->link=first;
first=ptr;
}
main();
break;
case 2:/*Entering the data In the End*/
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the data=");
scanf("%d",&ptr->info);/*if no nodes are there then entering the data*/
if(first==NULL)
{
ptr->link=first;
first=ptr;
}
else
{
ptr1 =first;
while(ptr1->link!=NULL)
{
ptr1=ptr1->link;
}
ptr1->link=ptr;
ptr->link=NULL;
}
main();
break;
case 3:/*Entering the data In After*/
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the data=");
scanf("%d",&ptr->info);
printf("Enter the element next to which next node is to be created=");
scanf("%d",&x);
ptr1=first;
while(ptr1!=NULL && ptr1->info!=x)
{
ptr1=ptr1->link;
}
if(ptr1==NULL)
printf("Element not found");
else
{
ptr->link=ptr1->link;
ptr1->link=ptr;
}
main();
break;
case 4:/*Entering the data In Before*/
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the data=");
scanf("%d",&ptr->info);
printf("Enter the element before to which next node is to be created=");
scanf("%d",&x);
ptr1=first;
while(ptr1!=NULL && ptr1->info!=x)
{
ptr2=ptr1;
ptr1=ptr1->link;
}
if(ptr1==NULL)
printf("Element not found");
else if(ptr1==first)
{
ptr->link=ptr1;
first=ptr;
}
else
{
ptr2->link=ptr;
ptr->link=ptr1;
}
main();
break;
default:main();
}
}
break;
case 2:
{
printf("DELETION\n");
printf("Deleting from \n1.Beginning\n2.End\n3.In Between\n");
printf("Enter Choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:/*Deleting the data from the Beginning*/
if(first==NULL)
printf("Linked List Underflow");
else
{
ptr=first;
first=first->link;
ptr->link=NULL;
printf("%d is deleted",ptr->info);
free(ptr);
}
main();
break;
case 2:/*Deleting the data from the End*/
if(first==NULL)
printf("Link List Underflow");
else
{
ptr=first;
while(ptr->link!=NULL)
{
ptr1=ptr;
ptr=ptr->link;
}
if(ptr1==first && ptr==NULL)
printf("%d is deleted",ptr1->info);
else
{
ptr1->link=NULL;
printf("%d is deleted",ptr->info);
free(ptr);
}
}
main();
break;
case 3:/*Deleting the data In Between*/
if(first==NULL)
printf("Link List Underflow");
else
{
printf("Enter the element of the node to be deleted=");
scanf("%d",&x);
ptr=first;
while(ptr!=NULL && ptr->info!=x)
{
ptr1=ptr;
ptr=ptr->link;
}
if(ptr==NULL)
printf("Element not found");
else
{
if(ptr==first)
first=ptr->link;
else
ptr1->link=ptr->link;
ptr->link=NULL;
printf("%d is deleted",ptr->info);
free(ptr);
}
}
main();
break;
default:main();
}
}
case 3:/*Display*/
{
printf("DISPLAY\n");
if(first==NULL)
printf("No Nodes");
else
{
ptr=first;
while(ptr!=NULL)
{
printf("%d\t",ptr->info);
ptr=ptr->link;
}
}
main();
break;
}
case 4 :exit(0);
}
}