forked from nazirmohd2006/NewRepository
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoubleLinkedList.cpp
More file actions
216 lines (205 loc) · 6.34 KB
/
DoubleLinkedList.cpp
File metadata and controls
216 lines (205 loc) · 6.34 KB
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
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node *prev;
};
class List{
Node *START, *LAST;
public:
List(){
START = LAST = NULL;
}
Node* createNode(int num){
Node *tmp = new Node;
tmp->data = num;
tmp->next = tmp->prev = NULL;
return tmp;
}
int getNum(){
int n;
cout<<"Enter a number"<<endl;
cin>>n;
return n;
}
void addAtBeg(){
Node *newNode = createNode(getNum());
if(START == NULL){
START = LAST = newNode;
cout<<"First node added"<<endl;
}else{
if(newNode->data > START->data){
cout<<"Largest values not allowed at beginning"<<endl;
return;
}
newNode->next = START;
START->prev = newNode;
START = newNode;
cout<<"Node added at beginning"<<endl;
}
}
void addBetween(){
Node *curr=START, *prv=NULL, *newNode;
newNode = createNode(getNum());
if(START == NULL){
START = LAST = newNode;
cout<<"First node added"<<endl;
}else{
while(curr != NULL && curr->data < newNode->data){
prv = curr;
curr = curr->next;
}
if(curr == NULL){
LAST->next = newNode;
newNode->prev = LAST;
LAST = newNode;
cout<<"Node added at last"<<endl;
}else if(prv == NULL){
newNode->next = START;
START->prev = newNode;
START = newNode;
cout<<"Node added at beginning"<<endl;
}else{
newNode->next = curr;
newNode->prev = prv;
prv->next = newNode;
curr->prev = newNode;
cout<<"Node added between two nodes"<<endl;
}
}
}
void addAtLast(){
Node *newNode = createNode(getNum());
if(START == NULL){
START = LAST = newNode;
cout<<"First node added"<<endl;
}else{
if(newNode->data < LAST->data){
cout<<"Smaller values not allowed at last"<<endl;
return;
}
LAST->next = newNode;
newNode->prev = LAST;
LAST = newNode;
cout<<"Node added at last"<<endl;
}
}
void deleteFromBeg(){
Node *curr = START;
if(START == NULL){
cout<<"List is empty"<<endl;
return;
}
START = START->next;
if(START != NULL){
START->prev = NULL;
}
delete curr;
cout<<"\aNode deleted from beginning"<<endl;
}
void deleteFromLast(){
Node *curr = LAST;
if(START == NULL){
cout<<"List is empty"<<endl;
return;
}
LAST = LAST->prev;
if(LAST != NULL){
LAST->next = NULL;
}
delete curr;
cout<<"\aNode deleted from last"<<endl;
}
void deleteFromBtw(){
Node *curr = START, *prv=NULL;
int x = getNum();
if(START == NULL){
cout<<"List is empty"<<endl;
return;
}
while(curr!= NULL && curr->data != x){
prv = curr;
curr = curr->next;
}
if(curr == NULL){
cout<<"Node is not there"<<endl;
return;
}
if(curr == LAST){
LAST = LAST->prev;
if(LAST != NULL){
LAST->next = NULL;
delete curr;
cout<<"\aNode deleted from last"<<endl;
}
}
else if(prv == NULL){
START = START->next;
if(START != NULL){
START->prev = NULL;
delete curr;
cout<<"\aNode deleted from beginning"<<endl;
}
}else{
prv->next = curr->next;
curr->next->prev = prv;
delete curr;
cout<<"\aNode deleted from between two nodes"<<endl;
}
}
void printList(){
Node *curr = START;
if(START == NULL){
cout<<"List is empty"<<endl;
return;
}
cout<<"List in Forward Direction"<<endl;
while(curr != NULL){
cout<<curr->data<<" ";
curr = curr->next;
}
cout<<"\nList in Backward Direction"<<endl;
curr = LAST;
while(curr != NULL){
cout<<curr->data<<" ";
curr = curr->prev;
}
cout<<endl;
}
};
main(){
int choice;
List lst;
do{
cout<<"1. Add Beginning\n2. Add Last\n3. Add Between\n4. Print List"<<endl;
cout<<"5. Delete From Beginning\n6. Delete From Last\n7. Delete From Between"<<endl;
cout<<"0. Exit"<<endl;
cin>>choice;
switch(choice){
case 1:
lst.addAtBeg();
break;
case 2:
lst.addAtLast();
break;
case 3:
lst.addBetween();
break;
case 4:
lst.printList();
break;
case 5:
lst.deleteFromBeg();
break;
case 6:
lst.deleteFromLast();
break;
case 7:
lst.deleteFromBtw();
break;
}
}while(choice != 0);
}