-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleCircularList.cpp
183 lines (153 loc) · 3.12 KB
/
DoubleCircularList.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
178
179
180
181
182
183
#include <iostream>
using namespace std;
template <class H> class Node{
private:
H key;
Node<H> *next, *prev;
public:
Node(H k, Node<H> *n=0, Node<H> *p=0) : key(k), next(n), prev(p) {}
void setKey(H k){
key=k;
}
void setNext(Node<H> *n){
next=n;
}
void setPrev(Node<H> *p){
prev=p;
}
H getKey() const{
return key;
}
Node<H>* getNext() const{
return next;
}
Node<H>* getPrev() const{
return prev;
}
};
template <class H> class DoubleCL{
private:
Node<H> *head, *tail;
Node<H>* _search(H x){
if(!head){
return 0;
}
Node<H>* aux=head;
do{
if(x == aux->getKey()){
return aux;
}
aux=aux->getNext();
}
while(aux!=head && aux && x >= aux->getKey());
return 0;
}
public:
DoubleCL(){
head=tail=0;
}
DoubleCL<H>* ins(H x){
if(!head){
head=new Node<H>(x);
tail=head;
return this;
}
//inserimento in testa
if(x < head->getKey()){
Node<H>* aux=new Node<H>(x, head, tail);
tail->setNext(aux);
head->setPrev(aux);
head=aux;
return this;
}
//inserimento in coda
if(x > tail->getKey()){
Node<H>* aux=new Node<H>(x, head, tail);
head->setPrev(aux);
tail->setNext(aux);
tail=aux;
return this;
}
//inserimento standard
Node<H>* current=head->getNext();
while(current && x >= current->getKey()){
current=current->getNext();
}
if(current){
Node<H>* aux=new Node<H>(x, current, current->getPrev());
aux->getPrev()->setNext(aux);
current->setPrev(aux);
}
return this;
}
DoubleCL<H>* del(H x){
Node<H>* aux=_search(x);
if(!aux){
return this;
}
//eliminazione in testa
if(aux==head){
if(!head->getNext()){
head=0;
tail=0;
delete aux;
return this;
}
if(aux->getNext()==tail){
head=tail;
head->setNext(0);
head->setPrev(0);
delete aux;
return this;
}
head=head->getNext();
tail->setNext(head);
head->setPrev(tail);
delete aux;
return this;
}
//eliminazione in coda
if(aux==tail){
if(aux->getPrev()==head){
tail=head;
head->setNext(0);
head->setPrev(0);
delete aux;
return this;
}
tail=tail->getPrev();
tail->setNext(head);
head->setPrev(tail);
delete aux;
return this;
}
//standard
Node<H>* back=aux->getPrev();
Node<H>* forward=aux->getNext();
back->setNext(forward);
forward->setPrev(back);
delete aux;
return this;
}
void print(){
if(head){
Node<H>* aux=head;
do{
cout<<aux->getKey()<<" ";
aux=aux->getNext();
}
while(aux && aux!=head);
}
cout<<endl;
}
};
int main(){
DoubleCL<int>* l=new DoubleCL<int>();
l->ins(100)->ins(99)->print();
l->del(100)->del(99)->print();
l->ins(8)->ins(10)->ins(5)->ins(6)->ins(7)->ins(13);
l->print();
l->del(8)->del(13)->del(5)->del(7)->del(6)->del(11)->ins(11)->ins(9)->ins(1)->del(10);
l->print();
cout<<"ok";
}