-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoubly_linkedList.c
293 lines (250 loc) · 7.21 KB
/
doubly_linkedList.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//Doubly Linked list insertion and deletion operation.
#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node* lptr;
struct Node* rptr;
};
void insert_front(struct Node**L,struct Node**R,int val){
struct Node* newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = val;
if(*L==NULL){
*L=*R=newNode;
(*L)->lptr= NULL; // -> has more precedence than * so write it always in ().
(*L)->rptr= NULL;
return;
}
newNode->rptr = *L;
newNode->lptr = NULL;
(*L)->lptr = newNode;
*L = newNode;
}
void insert_rear(struct Node**L,struct Node**R,int val){
struct Node* newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = val;
if(*L==NULL){
*L=*R=newNode;
(*L)->lptr= NULL;
(*L)->rptr= NULL;
return;
}
newNode->lptr = *R;
newNode->rptr = NULL;
(*R)->rptr = newNode;
*R = newNode;
}
void insert_order(struct Node**L,struct Node**R,int val){
struct Node* newNode;
struct Node* temp = *L;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = val;
if(*L==NULL){
*L=*R=newNode;
(*L)->lptr= NULL;
(*L)->rptr= NULL;
return;
}
if(val <= (*L)->value){
newNode->rptr = *L;
newNode->lptr = NULL;
(*L)->lptr = newNode;
*L = newNode;
}
else if(val >= (*R)->value){
newNode->lptr = *R;
newNode->rptr = NULL;
(*R)->rptr = newNode;
*R = newNode;
}
else{
while(temp->value <= val)
temp = temp->rptr;
newNode->lptr = temp->lptr;
newNode->rptr = temp;
temp->lptr->rptr = newNode;
temp->lptr = newNode;
}
}
void insert_specs(struct Node**L,struct Node**R,int val){
int sval,n;
struct Node* newNode;
struct Node* temp = *L;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = val;
//here our list is not empty because user give us value which we have to put after some value,
//so the list is not empty and we dont have to write condition for null list
do{
printf("Enter after which value you want to insert:\n");
scanf("%d",&sval);
n=1; //initialize n here.
temp = *L; //initialize temp also because if one time value not found then temp comes to head part again for new loop
while(temp->value != sval){
temp = temp->rptr;
if(temp== NULL){ //this condition is special because written in while loop
printf("your given value is not found\n");
n=0;
break;
}
}
}while(n==0);
newNode->lptr = temp;
newNode->rptr = temp->rptr;
if(temp != *R) temp->rptr->lptr = newNode; // write this always above the below sentences.
temp->rptr = newNode;
if(temp == *R) //upgradation of Rear
*R = newNode;
}
void del_front(struct Node** Head,struct Node** Rear){
struct Node * delete;
if(*Head == NULL){
printf("The list is Empty\n");
return;
}
//condition for only Node
if((*Head)->rptr == NULL && (*Head)->lptr == NULL){
//above condition is just example of only Node,we can also write (Head = Rear).
printf("%d is removed\n",(*Head)->value);
free(*Head);
*Head = *Rear = NULL;
return;
}
delete = *Head;
*Head = (*Head)->rptr;
(*Head)->lptr = NULL;
printf("%d is removed\n",delete->value);
free(delete);
}
void del_end(struct Node** Head,struct Node** Rear){
struct Node * delete;
delete = *Rear;
if(*Head == NULL){
printf("The list is Empty\n");
return;
}
if(*Head == *Rear){ //condition for only Node
printf("%d is removed\n",(*Head)->value);
free(*Head);
*Head = *Rear = NULL;
return;
}
(*Rear) = (*Rear)->lptr;
(*Rear)->rptr = NULL;
printf("%d is removed\n",delete->value);
free(delete);
}
void del_specific(struct Node** Head,struct Node** Rear,int val){
struct Node * temp;
temp = *Head;
if(*Head == NULL){
printf("The list is Empty\n");
return;
}
if( val == (*Head)->value){
if(*Head == *Rear){ //condition for only Node
printf("%d is removed\n",(*Head)->value);
free(*Head);
*Head = *Rear = NULL;
}
else{
temp = *Head;
*Head = (*Head)->rptr;
(*Head)->lptr = NULL;
printf("%d is removed\n",temp->value);
free(temp);
}
}
else if(val == (*Rear)->value){
temp = *Rear;
(*Rear) = (*Rear)->lptr;
(*Rear)->rptr = NULL;
printf("%d is removed\n",temp->value);
free(temp);
}
else{
while (temp != NULL && temp->value != val)
temp = temp->rptr;
if(temp == NULL){
printf("Value is not found\n");
return;
}
//temp points to the node to be deleted
temp->lptr->rptr = temp->rptr;
temp->rptr->lptr = temp->lptr;
printf("%d is removed\n",temp->value);
free(temp);
}
}
void display(struct Node * H){
struct Node *temp;
temp = H;
if(temp == NULL)
printf("List is empty");
printf("The Numbers in list is..");
while(temp != NULL){
printf("%d ",temp->value);
temp = temp->rptr;
}
printf("\n");
}
int main(){
struct Node* Head;
struct Node* Rear;
Head = Rear = NULL;
int val,n;
printf("1 for insert value at front:\n");
printf("2 for insert value at rear:\n");
printf("3 for insert value according to order:\n");
printf("4 for insert value after specific value:\n");
printf("5 for delete from front\n");
printf("6 for delete from end\n");
printf("7 for delete specific value\n");
printf("8 for display\n");
printf("9 for Exit\n");
scanf("%d",&n);
while(n !=9){
switch(n){
case 1:
printf("Enter the value you want to insert:\n");
scanf("%d",&val);
insert_front(&Head,&Rear,val);
break;
case 2:
printf("Enter the value you want to insert:\n");
scanf("%d",&val);
insert_rear(&Head,&Rear,val);
break;
case 3:
printf("Enter the value you want to insert:\n");
scanf("%d",&val);
insert_order(&Head,&Rear,val);
break;
case 4:
printf("Enter the value you want to insert:\n");
scanf("%d",&val);
insert_specs(&Head,&Rear,val);
break;
case 5:
del_front(&Head,&Rear); //we have to sent rear because of the condition of only Node.
break;
case 6:
del_end(&Head,&Rear);
break;
case 7:
printf("Enter value you want to be deleted\n");
scanf("%d",&val);
del_specific(&Head,&Rear,val);
break;
case 8:
display(Head);
break;
default:
printf("Enter specific value:\n");
}
printf("Enter which type of the operation you want to apply:\n");
scanf("%d",&n);
}
return 0;
}