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
| #include <stdlib.h>
int count;
struct node
{
int info;
struct node* next;
struct node* prev;
}*start,*ptr;
int INS( int ITEM, int pos)
{
struct node * newnode;
newnode= (struct node*) malloc ( sizeof(struct node) );
newnode->info=ITEM;
newnode->next=NULL;
newnode->prev=NULL;
// ERROR HANDLING #1 - Overflow?
if(newnode==NULL)
{
printf("\n No Memory Left to create a node. ");
return 0;
}
//ERROR HANDLING #2 - Position Overlimits
if( pos<=0 || pos>count+1 )
{
printf("\n Position Entered Over limits. ");
return 0;
}
// Condition 1: Entering the first node of LL
if(start==NULL)
{
start=newnode;
count=1;
}
// Condition 2: Entering node to the first position
else if(pos==1)
{
newnode->next=start;
start->prev=newnode;
start=newnode;
count++;
}
// Condition 3: Enterting a node to last position
else if(pos==count+1)
{
int i;
ptr=start;
for(i=2; i<=pos-1; i++)
ptr=ptr->next;
newnode->next=ptr->next;
newnode->prev=ptr;
ptr->next=newnode;
count++;
}
// Condition 4: Entering a node to any other position
else
{
int i;
ptr=start;
for(i=2; i<=pos-1; i++)
ptr=ptr->next;
newnode->next=ptr->next;
newnode->prev=ptr;
(ptr->next)->prev=newnode; // Note Line.
ptr->next=newnode;
count++;
}
return 1;
}
int DEL(int pos)
{
struct node *temp;
// ERROR HANDLING #1 - Underflow
if(start==NULL)
{
printf("\n There are no Nodes to delete. ");
return 0;
}
// ERROR HANDLING #2 - Position Overlimits
if( pos<=0 || pos>count )
{
printf("\n Position Entered Over limits. ");
return 0;
}
// Condition 1: Deleting Node at 1st position OR Last Node in LL.
if(pos==1)
{
temp=start;
start=start->next;
free(temp);
count--;
}
// Condition 2: Deleting node at last position
else if(pos==count)
{
for(ptr=start; ptr->next!=NULL; ptr=ptr->next);
(ptr->prev)->next=NULL;
printf("\n Deleting Node with value = %d" , ptr->info);
free(ptr);
count--;
}
// Condition 3; Deleting node at any other position
else
{
int i;
ptr=start;
for(i=2; i<=pos; i++)
ptr=ptr->next;
(ptr->prev)->next=ptr->next;
(ptr->next)->prev=ptr->prev;
printf("\n Deleting Node with value = %d",ptr->info);
free(ptr);
count--;
}
return 1;
}
int F_Traverse()
{
if(start==NULL)
{
printf("\n There are no elements in this Linked List.");
return 0;
}
printf("\n [");
for(ptr=start; ptr!=NULL; ptr=ptr->next)
printf(" - %d",ptr->info);
printf(" - ]\n");
return 1;
}
int R_Traverse()
{
if(start==NULL)
{
printf("\n There are no elements in this Linked List.");
return 0;
}
for(ptr=start; ptr->next!=NULL; ptr=ptr->next);
printf("\n [");
for(; ptr!=NULL; ptr=ptr->prev)
printf(" - %d",ptr->info);
printf(" - ]\n");
return 1;
}
int Search(int ITEM)
{
int i;
for(i=1,ptr=start; ptr!=NULL; ptr=ptr->next,i++)
if(ptr->info==ITEM)
return i;
return -1;
}
int main()
{
int ch,success;
int ITEM; int pos=1;
start=NULL;
count=0;
do
{
printf("\n ___________________________________________________ ");
printf("\n MENU: ");
printf("\n\n 1. Insert a Node. ");
printf("\n 2. Delete a Node. ");
printf("\n 3. Forward Traversing the Doubly LL. ");
printf("\n 4. Reverse Traversing the Doubly LL. ");
printf("\n 5. Search for a node. ");
printf("\n 0. EXIT. ");
printf("\n\n Your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: //INSERT
printf("\n Enter the Item to Insert: ");
scanf("%d",&ITEM);
if(start!=NULL)
{
printf("\n Enter the position: ");
scanf("%d", &pos);
}
success=INS(ITEM, pos);
break;
case 2: //DELETE
if(start!=NULL)
{
printf("\n Enter the position: ");
scanf("%d", &pos);
}
success=DEL(pos);
break;
case 3: //Forward Traversing
printf("\n The Elements: ");
success=F_Traverse();
break;
case 4: //Reverse Traversing
printf("\n The Elements: \n");
success=R_Traverse();
break;
case 5: //Searching
printf("\n Enter the Item to Search for: ");
scanf("%d",&ITEM);
pos=Search(ITEM);
if(pos!=-1)
printf("\n The Item is found at %d Position",pos);
else
printf("\n Item not found. ");
break;
case 0:
printf("\n Exiting... ");
}
}while(ch);
return 1;
}
|