-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplementation_of_doubly_LL_with_removing_alternate_elements.c
175 lines (175 loc) · 3.28 KB
/
implementation_of_doubly_LL_with_removing_alternate_elements.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
#include<stdio.h>
#include<stdlib.h>
typedef struct doublyNode
{
struct doublyNode* prev;
int info;
struct doublyNode* next;
}sd;
void display(sd* head)
{
sd*p=head;
while(p!=NULL)
{
printf("%d\t",p->info);
p=p->next;
}
printf("\n");
}
sd* iab(sd*head)
{
sd*temp=(sd*)malloc(sizeof(sd));
if(temp==NULL)
{
printf("OVERFLOW!!!!\n");
return head;
}
else if(head==NULL)
{
head=temp;
temp->next=temp->prev=NULL;
}
else
{
temp->next=head;
head->prev=temp;
temp->prev=NULL;
head=temp;
}
printf("Enter the info for new node : ");
scanf("%d",&temp->info);
display(head);
return head;
}
sd* iae(sd* head)
{
sd* temp=(sd*)malloc(sizeof(sd));
if(temp==NULL)
{
printf("OVERFLOW!!!\n");
return head;
}
else if(head==NULL)
{
head=temp;
temp->next=temp->prev=NULL;
}
else
{
sd*p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
temp->prev=p;
temp->next=NULL;
}
printf("Enter the info for new node : ");
scanf("%d",&temp->info);
display(head);
return head;
}
sd* dfb(sd*head)
{
if(head==NULL)
{
printf("UNDERFLOW!!!!\n");
}
else
{
sd*p=head;
head=head->next;
head->prev=NULL;
free(p);
}
display(head);
return head;
}
sd* dfe(sd* head)
{
if(head==NULL)
{
printf("UNDERFLOW!!!!\n");
}
else if(head->next==NULL)
{
free(head);
head=NULL;
}
else
{
sd* p=head,*p1;
while(p->next!=NULL)
{
p1=p;
p=p->next;
}
p1->next=NULL;
free(p);
}
display(head);
return head;
}
sd* remalt(sd*head)
{
printf("1-for removing odd elements\n2-for removing even position elements\n Enter your choice : ");
int c;
scanf("%d",&c);
sd*p,*p1;
p=(c==1)?head:head->next;
if(p==head)
{
head=head->next;
head->prev=NULL;
p1=p;
p=p->next;
free(p1);
}
else
{
p=head;
}
while(p!=NULL && p->next!=NULL)
{
p=p->next;
p->prev->next=p->next;
if(p->next!=NULL)
p->next->prev=p->prev;
p1=p;
free(p);
p=p1->next;
}
display(head);
return head;
}
int main()
{
sd* head=NULL;
int c;
do
{
printf("1-Insert at beg\n2-Insert at end\n3-Delete from beginning\n4-Delete from end\n5-Remove Alternate elements\nEnter your choice : ");
scanf("%d",&c);
switch (c)
{
case 1:
head=iab(head);
break;
case 2:
head=iae(head);
break;
case 3:
head=dfb(head);
break;
case 4:
head=dfe(head);
break;
case 5:
head=remalt(head);
break;
}
}
while(c>0 && c<6);
return 0;
}