-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathdoubly_linked_list.c
276 lines (250 loc) · 5.7 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *start = NULL;
struct node *last = NULL;
void create();
void display_forward();
void display_backward();
void insert_begin();
void insert_last();
void insert_specific();
void length();
void delete_first();
void delete_last();
void delete_specific();
void main()
{
int ch;
char choice;
while (1)
{
//printf("Enter your option\n");
printf("\n1. Create\n");
printf("2. Insert at Begin\n");
printf("3. Insert at Last\n");
printf("4. Insert at Location\n");
printf("5. Delete at First\n");
printf("6. Delete at Last\n");
printf("7. Delete specific\n");
printf("8. Display Forward\n");
printf("9.Display Backward\n");
printf("10. Length of the list\n");
printf("11. Exit\n");
printf("ENTER YOUR CHOICE: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
create();
break;
case 2:
insert_begin();
break;
case 3:
insert_last();
break;
case 4:
insert_specific();
break;
case 5:
delete_first();
break;
case 6:
delete_last();
break;
case 7:
delete_specific();
break;
case 8:
display_forward();
break;
case 9:
display_backward();
break;
case 10:
length();
break;
case 11:
exit(1);
break;
default:
printf("Invalid Input.");
}
}
}
void create()
{
char ch;
struct node *temp=(struct node*)malloc(sizeof(struct node));
printf("Enter first node:");
scanf("%d",&temp->data);
temp->prev=temp->next=NULL;
start=last=temp;
printf("Do you want to enter more(y/n): ");
while((getchar())!='\n');
scanf("%c",&ch);
while(ch=='y')
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the next node:");
scanf("%d",&temp->data);
last->next=temp;
temp->prev=last;
last=temp;
printf("Do you want to enter more data(y/n): ");
while((getchar())!='\n');
scanf("%c",&ch);
}
last -> next=NULL;
}
void insert_begin()
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
printf("enter the first node:");
scanf("%d",&temp->data);
if(start==NULL)
{
temp->prev=temp->next=NULL;
start=last=temp;
}
else{
start->prev=temp;
temp->next=start;
temp->prev=NULL;
start=temp;
}
}
void insert_last()
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
printf("enter the first node:");
scanf("%d",&temp->data);
if(start==NULL)
{
temp->prev=temp->next=NULL;
start=last=temp;
}
else{
last->next=temp;
temp->prev=last;
temp->next=NULL;
last=temp;
}
}
void insert_specific()
{
int x;
struct node *temp=start;
struct node *temp1=(struct node*)malloc(sizeof(struct node));
printf("enter the first node:");
scanf("%d",&temp1->data);
printf("enter the node after which do u want to insert:");
scanf("%d",&x);
while(temp->data!=x)
{
temp=temp->next;
}
temp1->next=temp->next;
temp->next->prev=temp1;
temp->next=temp1;
temp1->prev=temp;
}
void delete_first()
{
struct node *temp=start;
if(start==NULL)
{
printf("list is empty.");
}
else if(start->next==NULL)
start=last=NULL;
else
start=start->next;
start->prev=NULL;
free(temp);
}
void delete_last()
{
struct node *temp=last;
if(start==NULL)
{
printf("list is empty.");
}
else if(start->next==NULL)
start=last=NULL;
else {
last=last->prev;
last->next=NULL;
}
free(temp);
}
void delete_specific()
{
int x;
struct node *temp=start;
printf("enter the node which do u want to delete:");
scanf("%d",&x);
while(temp->data!=x)
{
temp=temp->next;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
void display_forward()
{
struct node *temp=start;
if(start==NULL)
{
printf("no nodes in the list.");
}
else {
while(temp!=NULL)
{
printf("%d",temp->data);
if (temp->next != NULL) //for formatting of diaplay
printf("->");
temp=temp->next;
}
}
}
void display_backward()
{
struct node *temp=last;
if(start==NULL)
{
printf("no nodes in the list.");
}
else {
while(temp!=NULL)
{
printf("%d",temp->data);
if (temp->prev != NULL) //for formatting of diaplay
printf("->");
temp=temp->prev;
}
}
}
void length(){
struct node *temp=start;
if(start==NULL){
printf("\n No nodes in the linked list.\n");
}
else{
int count=0;
while(temp!=NULL){
count++;
temp=temp->next;
}
printf("The length of list is: %d",count);
}
}