-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.h
247 lines (226 loc) · 4.03 KB
/
set.h
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
template<class T>
class set;
template<class T>
class setNode{
friend class set<T>;
template <class K>
friend std :: ostream& operator<<(std::ostream&os, const set<K>&);
private:
T value;
setNode<T> * next;
setNode<T> * prev;
};
template<class T>
class set{
public:
set();//COSTRUTTORE
set(const set<T>&); //COSTRUTTORE DI COPIA
~set(); //DISTRUTTORE
set<T>& operator=(const set<T> &);
bool operator==(const set<T> &);
void clear();//CLEAR
bool empty()const;//SET VUOTO
bool isIn(T t)const;//CONTROLLO VALORI UGUALI
void insert(T t);//INSERIMENTO
void remove(setNode<T> *t);//RIMOZION
setNode<T> *find(T t) const;//RICERCA
size_t getsize() const;//size
void merge(set<T> &v);//UNIONE
void difference(set<T> &v);//DIFFERENZA
void intersect(set<T> &v);//INTERSEZIONE
template <class K>
friend std :: ostream& operator<<(std::ostream& os, const set<K>&);
private:
setNode<T> * head;
int length=0;
};
//COSTRUTTORE
template <typename T>
set<T>::set(){
head=nullptr;
}
//COSTRUTTORE COPIA
template <typename T>
set<T>::set(const set<T>&s){
head=nullptr;
setNode<T> *node = new setNode<T>;
node=s.head;
while(node!=nullptr){
insert(node->value);
node=node->next;
}
}
//DISTRUTTORE
template <class T>
set<T>::~set(){
clear();
}
//SIZE
template<typename T>
size_t set<T>::getsize() const {
return length;
}
//CLEAR
template<typename T>
void set<T>::clear(){
setNode<T> *temp;
while(head!=nullptr){
temp=head;
head=head->next;
delete temp;
}
}
//EMPTY
template<typename T>
bool set<T>::empty()const{
return head==nullptr;
}
//INSERIMENTO
template<class T>
void set<T>::insert(T t){
if(!isIn(t)){
setNode<T> *node=new setNode<T>;
node->value=t;
node->next=node->prev=nullptr;
if(!empty()){
node->next=head;
head->prev=node;
}
head=node;
length++;
}
}
//ISIN
template<typename T>
bool set<T>::isIn(T t) const{
setNode<T> *temp;
temp=head;
bool found=false;
while(temp!=nullptr and !found){
if(temp->value==t){
found=true;
}
else{
temp=temp->next;
}
}
return found;
}
//FIND
template<typename T>
setNode<T> *set<T>::find(T t) const{
setNode<T> *temp;
temp=head;
bool found=false;
while(temp!=nullptr and !found){
if(temp->value==t){
found=true;
}
else{
temp=temp->next;
}
}
return temp;
}
//RIMUOVI
template<typename T>
void set<T>::remove(setNode<T> *t){
if(empty()){
throw"Il set è vuoto";
}
if(t->prev==nullptr){
head=t->next;
}else if(t->next==nullptr){
t->prev->next=nullptr;
}
else{
t->next->prev=t->prev;
t->prev->next=t->next;
}
delete t;
length--;
}
//STAMPA
template <class K>
std :: ostream& operator<<(std::ostream& os, const set<K>&p){
os<<"[";
if(!p.empty()){
setNode<K>* tmp=p.head;
while(tmp!=nullptr) {
os<<tmp->value<<"|";
tmp=tmp->next;
}
}
os<<"]";
return os;
}
//UNION
template<class T>
void set<T>::merge(set<T> &v){
setNode<T> *node= new setNode<T>;
node=v.head;
while(node != nullptr){
T value=node->value;
if(!find(value)){
insert(value);
}
node=node->next;
}
}
//INTERSECT
template<class T>
void set<T>::intersect(set<T> &v){
setNode<T> *node= new setNode<T>;
node=head;
while(node!=nullptr){
T value=node->value;
if(!v.find(value)){
node=node->next;
remove(find(value));
}else{
node=node->next;
}
}
}
//DIFFERENCE
template<class T>
void set<T>::difference(set<T> &v){
setNode<T> *node= new setNode<T>;
node=head;
while(node!=nullptr){
T value=node->value;
if(v.find(value)){
node=node->next;
remove(find(value));
}else{
node=node->next;
}
}
}
//OPERATORE=
template<class T>
set<T>& set<T>::operator=(const set<T> &s){
clear();
setNode<T> *node= new setNode<T>;
node=s.head;
while(node!=nullptr){
T value=node->value;
insert(value);
node=node->next;
}
}
//OPERATORE==
template<class T>
bool set<T>::operator==(const set<T> &s){
setNode<T> *node= new setNode<T>;
node=head;
while(node!=nullptr){
T value=node->value;
if(!s.find(value)){
return false;
}else{
node=node->next;
}
}
return true;
}