-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsll.c
247 lines (225 loc) · 5.13 KB
/
sll.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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*head=0,*t1,*t2,*end=0;
void create()
{
int ch=1;
while(ch==1)
{
t1= (struct Node*)malloc(sizeof(struct Node*));
printf("Enter data: ");
scanf("%d",&t1->data);
t1->next=0;
if(head==0)
{
head=end=t1;
}
else
{
end->next=t1;
end=end->next;
}
printf("Want to inset more ? (Yes - 1/ NO - 0) :");
scanf("%d",&ch);
}
}
void display()
{
if(head==0)
{
printf("List is empty!!");
}
else
{
int count=nodeCount();
t1=head;
while(t1!=0)
{
printf("%d -> ",t1->data);
t1=t1->next;
}
printf("\nThe number of nodes is : %d",count);
}
}
int nodeCount()
{
int nc;
t1=head;
while(t1!=0)
{
nc++;
t1=t1->next;
}
return nc;
}
void insert_beg()
{
t1 = (struct Node*)malloc(sizeof(struct Node*));
printf("Enter data to insert: ");
scanf("%d",&t1->data);
t1->next=0;
if(head==0)
{
head=end=t1;
}
else
{
t1->next=head;
head=t1;
}
}
void insert_mid()
{
int pos,n=1;
t1=(struct Node*)malloc(sizeof(struct Node*));
printf("Enter data to insert : ");
scanf("%d",&t1->data);
t1->next=0;
printf("Enter position to insert: ");
scanf("%d",&pos);
t2=head;
while(t2!=0 && n<pos-1)
{
t2=t2->next;
n++;
}
t1->next=t2->next;
t2->next=t1;
}
void insert_end()
{
if(head==0)
{
printf("List is empty!");
}
else
{
t1=(struct Node*)malloc(sizeof(struct Node*));
printf("Enter data to insert: ");
scanf("%d",&t1->data);
t1->next=0;
t2=head;
while(t2->next!=0)
{
t2=t2->next;
}
t2->next=t1->next;
t2->next=t1;
}
}
void del_beg()
{
if(head==0)
{
printf("List is empty!!");
}
else
{
t1=head;
head=t1->next;
t1->next=0;
free(t1);
}
}
void del_end()
{
if(head==0)
{
printf("List is empty !");
}
else
{
t1=head;
while(t1->next->next!=0)
{
t1=t1->next;
}
end=t1;
t1=t1->next;
end->next=0;
free(t1);
}
}
void del_mid()
{
if(head==0)
{
printf("List is empty!!");
}
else
{
int n=1,pos2;
printf("Enter position to delete : ");
scanf("%d",&pos2);
t1=head;
t2=head;
while(n<pos2-1)
{
t1=t1->next;
t2=t2->next;
n++;
}
t2=t2->next;
t1->next=t2->next;
t2->next=0;
free(t2);
}
}
int main()
{
int ch2=1,ch3,x,p;
while(ch2==1)
{
printf("\n1.Creation of Linked List \n2.Insertion \n3.Deletion \n4.Display");
printf("\nEnter your choice: ");
scanf("%d",&ch3);
switch(ch3)
{
case 1 : create();
break;
case 2 : printf("------Insertion-------");
printf("\n5.Insertion at the beginning \n6.Insertion at the end \n7.Insertion at desired location\n");
printf("Enter your choice (1-3): ");
scanf("%d",&x);
switch(x)
{
case 1 : insert_beg();
break;
case 2 : insert_end();
break;
case 3 : insert_mid();
break;
default : printf("Invalid Choice !\n");
break;
}
break;
case 3 : printf("------Deletion------");
printf("\n8.Deletion at beginning \n9.Deletion at the end \n10.Deletion at desired location\n");
printf("Enter your choice (1-3) : ");
scanf("%d",&p);
switch (p)
{
case 1 : del_beg();
break;
case 2 : del_end();
break;
case 3 : del_mid();
break;
default : printf("Invalid Choice !");
break;
}
break;
case 4 : display();
break;
default : printf("Invalid choice ");
break;
}
printf("\nEnter 1 to continue and 0 to exit : ");
scanf("%d",&ch2);
}
return 0;
}