-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedList.C
197 lines (187 loc) · 3.26 KB
/
DoublyLinkedList.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct nodetype
{
int info;
struct nodetype *prev;
struct nodetype *next;
}node;
void inorder(node *head)
{
if(head!=NULL)
{
inorder(head->prev);
printf("%d ",head->info);
inorder(head->next);
}
}
node *findmid(node *head)
{
node *p,*q;
p=head;
q=head;
while(p!=NULL || p->next!=NULL)
{
q=q->next;
p=(p->next)->next;
}
return q;
}
node *dlltobst(node *head)
{
node *temp,*p,*q;
if(head!=NULL && head->next!=NULL)
return head;
temp=findmid(head);
p=head;
while(p->next!=temp)
{
p=p->next;
}
p->next=NULL;
q=temp->next;
temp->prev=dlltobst(head);
temp->next=dlltobst(q);
return temp;
}
void add(node **head,node **tail,int value,int pos)
{
node *ptr=(node *)malloc(sizeof(node));
ptr->info=value;
if(pos==1)
{
ptr->prev=NULL;
if((*head)==NULL)
{
ptr->next=NULL;
}
else
{
ptr->next=(*head);
(*head)->prev=ptr;
}
(*head)=ptr;
return;
}
node *temp=(*head);
int k=1;
while(k<(pos-1) && temp!=NULL)
{
temp=temp->next;
k++;
}
if(k!=(pos-1))
{
printf("Desired position doesn't exist\n");
return;
}
ptr->next=temp->next;
ptr->prev=temp;
if((temp->next)!=NULL)
(temp->next)->prev=ptr;
else
(*tail)=ptr;
temp->next=ptr;
}
void traverse(node *head)
{
node *temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->next;
}
}
void reversetraverse(node *tail)
{
node *temp=tail;
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->prev;
}
}
void del(node **head,node **tail,int pos)
{
node *temp,*ptr;
temp=(*head);
if((*head)==NULL)
{
printf("List is empty\n");
return;
}
if(pos==1)
{
(*head)=(*head)->next;
if((*head)==NULL)
(*tail)=NULL;
else
(*head)->prev=NULL;
free(temp);
return;
}
int k=1;
while(k<pos && (temp->next)!=NULL)
{
k++;
temp=temp->next;
}
if(k!=pos)
printf("Desired position doesn't exist\n");
ptr=temp->prev;
ptr->next=temp->next;
if((temp->next) != NULL)
temp->next->prev=ptr;
else
(*tail)=(*tail)->prev;
free(temp);
}
void dispose(node **head,node **tail)
{
node *temp;
while((*head)!=NULL)
{
temp=(*head);
(*head)=(*head)->next;
free(temp);
}
(*tail)=NULL;
}
int main()
{
int choice;
node *head,*tail;
head=tail=NULL;
printf("Enter ur choice \n1:To add an element \n2:To delete an element\n 3:Forward traversal \n4:Reverse traversal\n 5:Exit\n");
scanf("%d",&choice);
do
{
switch(choice)
{
case 1:printf("Enter the element and position\n");
int val,pos;
scanf("%d%d",&val,&pos);
add(&head,&tail,val,pos);
break;
case 2:printf("Enter the position\n");
scanf("%d",&pos);
del(&head,&tail,pos);
break;
case 3:printf("Forward traversal is\n");
traverse(head);
break;
case 4:printf("Reverse order traversal is\n");
reversetraverse(tail);
break;
}
scanf("%d",&choice);
}while(choice!=5);
printf("Convert the doubly linked list into BST\n");
printf("The inorder traversal is\n");
node *ptr=dlltobst(head);
inorder(ptr);
dispose(&head,&tail);
getch();
return 0;
}