forked from oops-aman/Data-Structures-in-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll.c
223 lines (202 loc) · 4.52 KB
/
dll.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next,*prev;
}*head=0,*end=0,*t1,*t2;
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;
t1->prev=0;
if(head==0)
{
head=end=t1;
}
else
{
end->next=t1;
end=t1;
}
printf("Want to insert more (1-Yes/0-No) : ");
scanf("%d",&ch);
}
}
void insert_beg()
{
t1=(struct node*)malloc(sizeof(struct node*));
printf("Enter data to insert : ");
scanf("%d",&t1->data);
if(head==0)
{
t1->next=0;
t1->prev=0;
head=t1;
}
else
{
t1->prev=0;
t1->next=head;
head->prev=t1;
head=t1;
}
}
void insert_end()
{
t1=(struct node*)malloc(sizeof(struct node*));
printf("Enter data to insert : ");
scanf("%d",&t1->data);
if(head==0)
{
t1->next=0;
t1->prev=0;
head=t1;
}
else
{
t2=head;
while(t2->next!=0)
{
t2=t2->next;
}
t2->next=t1;
t1->prev=t2;
t1->next=0;
}
}
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;
t1->prev=t2;
t2->next=t1;
t2->next->prev=t1;
}
void nodecount()
{
int nc;
t1=head;
while(t1!=0)
{
nc++;
t1=t1->next;
}
printf("\nThe number of nodes is : %d",nc);
}
void display()
{
t1=head;
while(t1!=0)
{
printf("%d -> ",t1->data);
t1=t1->next;
}
nodecount();
}
void del_beg()
{
t1=head;
head=head->next;
head->prev=0;
free(t1);
}
void del_end()
{
t1=head;
while(t1->next!=0)
{
t1=t1->next;
}
t1->prev->next=0;
free(t1);
}
void del_mid()
{
int pos,n=1;
printf("Enter position to delete : ");
scanf("%d",&pos);
t1=head;
t2=head;
while(n<pos-1)
{
t1=t1->next;
t2=t2->next;
n++;
}
t1=t1->next;
t2->next=t1->next;
t1->next->prev=t2;
free(t1);
}
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("\n1.Insertion at the beginning \n2.Insertion at the end \n3.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("\n1.Deletion at beginning \n2.Deletion at the end \n3.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;
}