-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdoubly.c
More file actions
239 lines (213 loc) · 3.33 KB
/
doubly.c
File metadata and controls
239 lines (213 loc) · 3.33 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *next,*prev;
int info;
};
typedef struct node snode;
snode *head,*a,*b;
void create();
void traverse();
void count_nodes();
void ins_beg();
void ins_end();
void ins_any_pos();
void del_beg();
void del_end();
void del_any_pos();
void search();
void main()
{
int ch;
while(1)
{
printf("Enter your choice:\n1 Create Double linked list\n2 Traverse\n3 Count Nodes\n4 Insert at beginning\n5 Insert at end\n6 Insert at any position\n7 Delete from beginning\n8 Delete from end\n9 Delete from any position\n10 Search for a node\nDefault:Exit\n");
scanf("%d",&ch);
switch(ch)
{ case 1:
create();
break;
case 2:
traverse();
break;
case 3:
count_nodes();
break;
case 4:
ins_beg();
break;
case 5:
ins_end();
break;
case 6:
ins_any_pos();
break;
case 7:
del_beg();
break;
case 8:
del_end();
break;
case 9:
del_any_pos();
break;
case 10:
search();
break;
default:
exit(0);
}
}
}
void create()
{
int i;
a=(snode*)malloc(sizeof(snode));
printf("Enter data\n");
scanf("%d",&a->info);
a->prev=a->next=NULL;
head=a;
printf("If you wish to continue,press 1 else 0:\n");
scanf("%d",&i);
while(i)
{
b=(snode*)malloc(sizeof(snode));
printf("Enter data\n");
scanf("%d",&b->info);
b->prev=a;
b->next=NULL;
a->next=b;
a=b;
printf("If you wish to continue,press 1 else 0:\n");
scanf("%d",&i);
}
}
void traverse()
{
a=head;
printf("List:\n");
while(a!=NULL)
{
printf("%d\n",a->info);
a=a->next;
}
}
void count_nodes()
{
int nodes;
a=head;
while(a!=NULL)
{
nodes++;
a=a->next;
}
printf("\n%d\n",nodes);
}
void ins_beg()
{
if(head!=NULL)
{
a=(snode*)malloc(sizeof(snode));
printf("enter data:");
scanf("%d",&a->info);
a->next=head;
a->prev=NULL;
head->prev=a;
head=head->prev;
}
else
{
a=(snode*)malloc(sizeof(snode));
printf("Enter data\n");
scanf("%d",&a->info);
a->prev=a->next=NULL;
head=a;
}
}
void ins_end()
{
b=head;
while(b->next!=NULL)
{
b=b->next;
}
a=(snode*)malloc(sizeof(snode));
printf("enter data:");
scanf("%d",&a->info);
b->next=a;
a->next=NULL;
}
void ins_any_pos()
{
int i,j=2;
printf("Enter position:");
scanf("%d",&i);
a=head;
while(j<i)
{
a=a->next;
j++;
}
b=(snode*)malloc(sizeof(snode));
printf("Enter data:\n");
scanf("%d",&b->info);
b->next=a->next;
b->prev=a;
a->next=b;
}
void del_beg()
{
a=head;
head=head->next;
head->prev=NULL;
free(a);
}
void del_end()
{
a=head;
while(a->next!=NULL)
a=a->next;
a->prev->next=NULL;
free(a);
}
void del_any_pos()
{
a=head;
int i,j=1;
printf("Enter position:");
scanf("%d",&i);
while(j<i)
{
a=a->next;
j++;
}
printf("%d",a->info);
a->next->prev=a->prev;
a->prev->next=a->next;
free(a);
}
void search()
{
int i=1,temp;
printf("Enter data to be searched:");
scanf("%d",&temp);
a=head;
while(a!=NULL)
{
if(a->info==temp)
{
printf("Node found at %d\n",i);
break;
}
else
{
i++;
if(a->next==NULL)
{
printf("Sorry,NOde not found!\n");
}
a=a->next;
}
}
}