-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDOUBLY LINKED LIST .CPP
173 lines (159 loc) · 2.93 KB
/
DOUBLY LINKED LIST .CPP
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
#include<iostream>
using namespace std;
struct node{
node*prev;
int data;
node*next;
};
node* create(){
node*p=new node();
cout<<"\n"<<"Enter the data:"<<"\n";
cin>>p->data;
p->prev=NULL;
p->next=NULL;
return p;
}
void display_fwd(node* head){
node*p;
p=head;
while(p->next!=NULL){
cout<<p->data<<"\t";
p=p->next;
}
cout<<p->data<<"\t";
}
void display_rev(node*head){
node*p,*q;
p=head;
while (p->next!=NULL)
{
p=p->next;
}
q=p;
while(q->prev!=NULL){
cout<<q->data<<"\t";
q=q->prev;
}
cout<<q->data<<"\t";
}
node*insert_beg(node*head){
node*p,*q;
p=create();
q=head;
if(head==NULL){
head=p;
}
else{
p->next=q;
q->prev=p;
head=p;
}
return head;
}
node* insert_anypos(node*head,int pos){
node*p,*q,*r;
p=create();
q=head;
int i=1;
for(i=i+1;i<pos-1;i++){
q=q->next;
}
r=q->next;
// p->next=q->next;
// q->next->prev=p;
p->next=r;
r->prev=p;
q->next=p;
p->prev=q;
return head;
}
node*insert_end(node*head){
node*p,*q;
p=create();
q=head;
if(head==NULL){
head=p;
}
else if(q->next==NULL){
q->next=p;
p->prev=q;
}
else if(q->next!=NULL){
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
p->prev=q;
}
return head;
}
node* del_beg(node*head){
node*p,*q;
q=head;
p=head->next;
delete q;
head=p;
return head;
}
node* del_end(node*head){
node*p,*q;
q=head;
while(q->next->next!=NULL){
q=q->next;
}
p=q->next;
q->next=NULL;
delete p;
return head;
}
int main(){
int choice,op,pos;
node*head;
head=create();
display_fwd(head);
do{
cout<<"\n"<<"Choose from the dropdown below:"<<"\n";
cout<<"1.Display"<<"\n";
cout<<"2.Display reverse"<<"\n";
cout<<"3.Insert at beginning"<<"\n";
cout<<"4.Insert at any position"<<"\n";
cout<<"5.Insert at end"<<"\n";
cout<<"6.Delete at beginning"<<"\n";
cout<<"7.Delete at end"<<"\n";
cin>>choice;
switch(choice){
case 1:
display_fwd(head);
break;
case 2:
display_rev(head);
break;
case 3:
head=insert_beg(head);
//display_fwd(head);
break;
case 4:
cout<<"\n"<<"Enter the position where you want to insert:"<<"\n";
cin>>pos;
head=insert_anypos(head,pos);
//display_fwd(head);
break;
case 5:
head=insert_end(head);
//display_fwd(head);
break;
case 6:
head=del_beg(head);
//display_fwd(head);
break;
case 7:
head=del_end(head);
//display_fwd(head);
break;
}
cout<<"\n"<<"Do you want to continue(YES=1/NO=0):"<<"\n";
cin>>op;
}while(op==1);
return 0;
}