-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkList.cpp
178 lines (177 loc) · 3.33 KB
/
DoublyLinkList.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
174
175
176
177
#include <iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
node *prev,*next;
};
node *head=NULL,*tail=NULL;
void insertion_at_end(int);
void display_rev();
void insertion_at_pos(int data,int pos);
void display();
void delete_node(int);
int main()
{ int data,choice,pos;
A:
cout <<"\nEnter your choice"
<<"\n1.Insertion at end of list"
<<"\n2.Insertion at position"
<<"\n3.Deletion from position"
<<"\n4.Display List in reverse order"
<<"\n5.Display list"
<<"\n6.Exit\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"\nEnter the data to insert:";
cin>>data;
insertion_at_end(data);
goto A;
case 2:cout<<"\nEnter data::";
cin>>data;
cout<<"Enter Position::";
cin>>pos;
insertion_at_pos(data,pos);
goto A;
case 3:cout<<"\nEnter Position::";
cin>>pos;
delete_node(pos);
goto A;
case 4:display_rev();
goto A;
case 5:display();
goto A;
case 6:exit(0);
default:cout<<"\nInvalid input";
goto A;
}
return 0;
}
void insertion_at_end(int data)
{
node *ptr=NULL;
ptr=new node;
ptr->data=data;
if(head==NULL)
{
ptr->prev=ptr->next=NULL;
head=tail=ptr;
}
else
{
tail->next=ptr;
ptr->prev=tail;
ptr->next=NULL;
tail=ptr;
}
}
void display()
{
node *p=NULL;
p=head;
cout<<"\nList is::";
if(p==NULL)
{
cout<<"\nList is Empty";
return;
}
while(p!=NULL)
{
cout<<" -> "<<p->data;
p=p->next;
}
}
void display_rev()
{
node *p=NULL;
p=tail;
cout<<"\nList is::";
if(p==NULL)
{
cout<<"\nList is Empty";
return;
}
while(p!=NULL)
{
cout<<" -> "<<p->data;
p=p->prev;
}
}
void insertion_at_pos(int data,int pos)
{
node *p=NULL,*temp=NULL;
p=head;
temp=new node;
temp->data=data;
int count=0;
if(pos==0)
{
temp->prev=NULL;
head->prev=temp;
temp->next=head;
head=temp;
return;
}
while(p!=NULL && count!=pos-1)
{
count++;
p=p->next;
}
if(p==NULL||p->next==NULL)
{
cout<<"\nInvalid position";
}
else
{
temp->next=p->next;
p->next->prev=temp;
p->next=temp;
temp->prev=p;
}
}
void delete_node(int pos)
{
int count=0;
node *p=NULL,*q=NULL;
p=head;
if(p==NULL)
{
cout<<"\nList is Empty";
return;
}
if(pos==0)
{
cout<<"\nDeleted data is::"<<head->data;
head=head->next;
head->prev=NULL;
delete p;
return;
}
while(p!=NULL && count!=pos-1)
{
count++;
p=p->next;
}
if(p==NULL ||p->next==NULL)
{
cout<<"\nInvalid Position";
return;
}
else
if(p->next->next==NULL)
{ cout<<"\nDeleted data is::"<<p->next->data;
q=p->next;
p->next=NULL;
tail=p;
delete q;
}
else
{
q=p->next->next;
cout<<"\nDeleted data is::"<<p->next->data;
p->next=q;
q->prev=p;
}
}