-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathabcd2.c
270 lines (242 loc) · 4.67 KB
/
abcd2.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
#include<stdio.h>
#include<malloc.h>
struct dlist
{
int data;
struct dlist *lptr;
struct dlist *rptr;
};
struct dlist *first=NULL;
struct dlist *last=NULL;
void create()
{
struct dlist *node=NULL;
char ch='y';
int c=1;
struct dlist *temp=NULL;
while(ch=='y' || ch=='Y')
{
if(c==1)
{
node= (struct dlist *)malloc(sizeof(struct dlist));
fflush(stdin);
printf("enter the value ");
scanf("%d",&node->data);
node->lptr=NULL;
node->rptr=NULL;
first=node;
last=node;
c=c+1;
}
else
{
node->rptr=(struct dlist *)malloc(sizeof(struct dlist));
temp=node;
node=node->rptr;
fflush(stdin);
printf("enter the value ");
scanf("%d",&node->data);
node->rptr=NULL;
node->lptr=temp;
last=node;
}
fflush(stdin);
printf("press y to continue");
scanf("%c",&ch);
fflush(stdin);
}
}
void fdisplay()
{
struct dlist *node=first;
while(node!=NULL)
{
printf("->%d",node->data);
node=node->rptr;
}
}
void bdisplay(){
struct dlist *node=last;
while(node!=NULL)
{
printf("->%d",node->data);
node=node->lptr;
}
}
void insert_beg()
{
struct dlist *node=(struct dlist *)malloc(sizeof(struct dlist));
printf("enter the value to insert");
fflush(stdin);
scanf("%d",&node->data);
node->lptr=NULL;
node->rptr=first;
first=node;
}
void insert_end()
{
struct dlist *node=(struct dlist *)malloc(sizeof(struct dlist));
printf("enter the value to insert");
fflush(stdin);
scanf("%d",&node->data);
last->rptr=node;
node->lptr=last;
node->rptr=NULL;
last=node;
/*
struct list *node=first;
while(node->ptr!=NULL)
{
node=node->ptr;
}
node->ptr=(struct list *)malloc(sizeof(struct list));
node=node->ptr;
printf("enter the value to insert");
fflush(stdin);
scanf("%d",&node->data);
node->ptr=NULL;*/
}
void insert_pos()
{
int i;
int pos;
printf("Enter position ");
fflush(stdin);
scanf("%d",&pos);
struct dlist *node=first;
for(i=0;i<pos-2;i++){
node=node->rptr;
}
int val;
printf("Enter value to be inserted");
fflush(stdin);
scanf("%d",&val);
struct dlist *n=(struct dlist *)malloc(sizeof(struct dlist));
n->data=val;
n->rptr=node->rptr;
node->rptr->lptr=n;
n->lptr=node;
node->rptr=n;
}
void delete_beg()
{
struct list *node=first;
first=first->ptr;
free(node);
}
void delete_end()
{
struct list *node=first;
struct list *nnode=first;
while(node->ptr!=NULL){
nnode=node;
node=node->ptr;
}
nnode->ptr=NULL;
free(node);
}
void delete_pos()
{
int i;
int pos;
printf("Enter position ");
fflush(stdin);
scanf("%d",&pos);
struct list *node=first;
struct list *nnode=first->ptr;
for(i=0;i<pos-2;i++){
//node=node->ptr;
node=node->ptr;
nnode=nnode->ptr;
}
node->ptr=nnode->ptr;
free(nnode);
//node->ptr=node->ptr->ptr;
}
void Selection()
{
struct list *node=first;
int t;
struct list *nnode=NULL;
//int m
while(node->ptr!=NULL){
nnode=node->ptr;
while(nnode!=NULL){
if(node->data>nnode->data){
t=node->data;
node->data=nnode->data;
nnode->data=t;
}
nnode=nnode->ptr;
}
node=node->ptr;
}
}
void Selection(){
//struct list *node=first;
struct list *i,*j;
i=j=NULL;
int t;
for(i=first;i->ptr!=NULL;i=i->ptr){
for(j=i->ptr;j!=NULL;j=j->ptr){
if(i->data>j->data){
t=i->data;
i->data=j->data;
j->data=t;
}
}
}
}
int main()
{
int ch;
create();
while(5)
{
printf("\nenter 1 for insert at beginning");
printf("\nenter 2 for insert at end");
printf("\nenter 3 for insert at any position");
printf("\nenter 4 for delete at beginning");
printf("\nenter 5 for delete at end");
printf("\nenter 6 for delete at any position");
printf("\nenter 7 for fdisplay");
printf("\nenter 8 for bdisplay");
printf("\nenter 9 for Selection sort");
printf("\nenter 10 for exit");
printf("\nenter your menu choice");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_beg();
break;
case 2:
insert_end();
break;
case 3:
insert_pos();
break;
case 4:
//delete_beg();
break;
case 5:
//delete_end();
break;
case 6:
//delete_pos();
break;
case 7:
fdisplay();
break;
case 8:
bdisplay();
break;
case 9:
//Selection();
break;
case 10:
return 0;
}
}
}