-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLL.cpp
More file actions
151 lines (128 loc) · 2.64 KB
/
Copy pathDLL.cpp
File metadata and controls
151 lines (128 loc) · 2.64 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
#include<iostream>
#include<cstddef>
using namespace std;
typedef string Elem;
class DNode{
private:
Elem elem;
DNode* prev;
DNode* next;
friend class DLinkedList;
};
class DLinkedList{
public:
DLinkedList();
~DLinkedList();
bool empty() const;
const Elem& front() const;
const Elem& back() const;
void addFront(const Elem& e);
void addBack(const Elem& e);
void removeFront();
void removeBack();
void showlist();
private:
DNode* header;
DNode* trailer;
protected:
void add(DNode* v,const Elem& e);
void remove(DNode* v);
};
DLinkedList::DLinkedList(){
header = new DNode;
trailer = new DNode;
header->next = trailer;
trailer->prev = header;
}
DLinkedList::~DLinkedList(){
while(!empty()) removeFront();
delete header;
delete trailer;
}
bool DLinkedList::empty() const{
return (header->next == trailer);
}
const Elem& DLinkedList::front() const{
return header->next->elem;
}
const Elem& DLinkedList::back() const{
return trailer->prev->elem;
}
void DLinkedList::add(DNode* v,const Elem& e){
DNode* u = new DNode;
u->elem = e;
u->next = v;
u->prev = v->prev;
v->prev->next = u;
v->prev = u;
}
void DLinkedList::addFront(const Elem& e){
add(header->next,e);
}
void DLinkedList::addBack(const Elem& e){
add(trailer,e);
}
void DLinkedList::remove(DNode* v){
DNode* u = v->prev;
DNode* w = v->next;
u->next = w;
w->prev = u;
delete v;
}
void DLinkedList::removeFront(){
remove(header->next);
}
void DLinkedList::removeBack(){
remove(trailer->prev);
}
void DLinkedList::showlist(){
DNode* p = new DNode;
p=header->next;
cout<<"\nheader";
while(p->next!=NULL){
cout<<" <=> "<<p->elem;
p=p->next;
}
cout<<" <=> trailer\n"<<endl;
}
int main(){
cout<<"initializing Doubly linked list...."<<endl;
DLinkedList list;
cout<<"enter first element : ";
Elem ele;
cin>>ele;
list.addFront(ele);
int choice;
do{
cout<<"1.) add element from front"<<endl;
cout<<"2.) add element from back"<<endl;
cout<<"3.) remove element from front"<<endl;
cout<<"4.) remove element from back"<<endl;
cout<<"5.) show list"<<endl;
cout<<"6.) quit"<<endl;
cout<<"\nEnter your choice : ";
cin>>choice;
switch (choice) {
case 1:
cout<<"Enter element : ";
cin>>ele;
list.addFront(ele);
break;
case 2:
cout<<"Enter element : ";
cin>>ele;
list.addBack(ele);
break;
case 3:
list.removeFront();
break;
case 4:
list.removeBack();
break;
case 5:
list.showlist();
break;
cout<<"Enter Choice : ";
}
}while (choice!=6);
}