-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoubly_linked_list.c
246 lines (232 loc) · 3.99 KB
/
doubly_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
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
void insertb();
void inserte();
void insertp();
void deleteb();
void deletee();
void deletep();
void display();
void menu();
int choice,n,loc,i;
int main()
{
printf("\n1.Insert in beginning\n2.Insert in end\n3.Insert at any position\n4.Delete in beginning\n5.Delete in end\n6.Delete at any position\n7.Display\n8.Exit");
menu();
}
void menu(){
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:insertb();
break;
case 2:inserte();
break;
case 3:insertp();
break;
case 4:deleteb();
break;
case 5:deletee();
break;
case 6:deletep();
break;
case 7:display();
break;
case 8:
exit(1);
default:
printf("WRONG CHOICE,PLEASE ENTER CORRECT CHOICE");
break;
}
}
void insertb()
{
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node*));
if(ptr==NULL){
printf("Overflow");
}
else
{
printf("\nEnter your number: ");
scanf("%d",&n);
}
if(head==NULL)
{
ptr->data=n;
ptr->next=NULL;
ptr->prev=NULL;
head=ptr;
}
else
{
ptr->data=n;
ptr->prev=NULL;
ptr->next=head;
head=ptr->prev;
head=ptr;
}
printf("node inserted successfully\n");
menu();
}
void display()
{
struct node *ptr;
printf("\nprinting values...\n");
ptr=head;
while(ptr!=NULL)
{
printf("%d->",ptr->data);
ptr=ptr->next;
}
menu();
}
void deleteb()
{
struct node *ptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next==NULL)
{
head=NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr=head;
head=head->next;
head->prev=NULL;
free(ptr);
printf("\nnode deleted\n");
}
menu();
}
void inserte()
{
struct node *ptr,*temp;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value : ");
scanf("%d",&n);
ptr->data=n;
if(head==NULL)
{
ptr->next=NULL;
ptr->prev=NULL;
head=ptr;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next = ptr;
ptr->prev=temp;
ptr->next = NULL;
}
}
printf("Node inserted\n");
menu();
}
void deletee()
{
struct node *temp,*temp1;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next==NULL)
{
head=NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
temp1->prev=temp->prev;
free(temp);
printf("\nDeleted Node from the last\n");
}
menu();
}
void insertp()
{
struct node *ptr,*temp;
ptr=(struct node *)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location: ");
scanf("%d",&loc);
for(i=1;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("Cannot insert");
return;
}
}
printf("Enter value: ");
scanf("%d",&n);
ptr->data=n;
ptr->next=temp->next;
ptr->prev=temp;
temp->next=ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
menu();
}
void deletep()
{
struct node *ptr,*ptr1;
printf("\nEnter the location of the node you want to delete\n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1=ptr;
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1->next=ptr->next;
ptr1->prev=ptr->prev;
free(ptr);
printf("\nDeleted node");
printf("\n");
menu();
}