-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_in_Stack.cpp
More file actions
304 lines (203 loc) · 7.99 KB
/
Copy pathQueue_in_Stack.cpp
File metadata and controls
304 lines (203 loc) · 7.99 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
// creare una pila con 10 elementi e svuotarla in una coda
//prob itinere.
using namespace std;
//Node double linked list
template <typename T> class Node {
private:
T key;
Node* next;
public:
Node() : next(nullptr) {} // nodo vuoto
Node(const T& key) : key(key), next(nullptr) {}
//Node(const T& key, Node* next) : key(key), next(nullptr) {}
const T& get_key() const {
return key;
}
Node* get_next() const {
return next;
}
void set_key(const T& key){
this->key = key;
}
void set_next(Node* next){
this->next = next;
}
};
template <typename T>
ostream& operator<<(ostream& os, const Node<T>& nodo){ //overloading
os << nodo.get_key();
return os;
}
template < typename T> class ListSL {
private:
Node<T>* head;
// size_t size; quanti elementi abbiamo nella lista
public:
// 2 costruttori
ListSL() : head(nullptr) {} // crea lisata vuota
ListSL(const T& key) {
head = new Node<T>(key); // crea un lista con un solo nodo allinterno
}
~ListSL() { //distruttore
Node<T>* tmp = head;
while (tmp) {
Node<T>* next = tmp->get_next(); // salvi il next PRIMA
delete tmp;
tmp = next; // vai avanti
}
}
Node<T>* get_head() {
return head;
}
void set_head(Node <T>* head) {
this->head = head;
}
ListSL* add_head(const T& key){
Node<T>* tmp = this->get_head(); // tmp punta a un Node di tipo T(generico) this->get_head() mi permette di avere un ptr alla head della lista corrente
this->head = new Node<T>(key); // il primo nodo della lista in questione diventa un nuovo nodo appena creato
this->head->set_next(tmp); // il nuovo nodo ( ora e la nuova testa) ha come next, il tmp.
return this; // ritorni puntatre alloggetto corrente(list)
}
ListSL* add_tail(const T& key){ // riferimento costante a T
Node<T>* tmp = this->get_head();
if(tmp == nullptr){
return add_head(key); // se la lisat e vuota, add_head() equivale ad aggiungere in coda
}
while(tmp->get_next() != nullptr){ // entra nel ciclo solo se tmp ha un successivo
tmp = tmp->get_next(); // scorro la lista fino allultimo nodo
} // civlo mi serve per arrivare alla fine
tmp->set_next(new Node<T>(key)); //lultimo nodo ha un nuovo nodo come next
return this;
}
Node<T>* find(const T& key) {
Node<T>* tmp = get_head();
while(tmp != nullptr && tmp->get_key() != key){
tmp = tmp->get_next();
}
return tmp;
}
ListSL* add_after(const T& key, const T& befkey){
Node<T>* nodo_bef = find(befkey);
if(nodo_bef == nullptr){
return this; // non inseriamo, oppure inseriamo in coda.
}
Node<T>* nodo_key = new Node<T>(key);
nodo_key->set_next(nodo_bef->get_next());
nodo_bef->set_next(nodo_key);
return this;
}
ListSL* deletekey(const T& key){
// ricerca non mi fornisce il ptr al nodo prec.
Node<T>* prec = nullptr;
Node<T>* tmp = this->get_head();
while(tmp != nullptr && tmp->get_key() != key){
prec = tmp;
tmp = tmp->get_next();
}
if(tmp == nullptr){
return this;
}
if(prec == nullptr){
return delete_head();
}
prec->set_next(tmp->get_next());
delete tmp;
return this;
}
ListSL* delete_head() {
if(this->get_head() == nullptr){
return this; // se la lista e vuoto torna la lista
}
Node <T>* tmp = this->get_head(); // tmp punta alla testa della lista
this->set_head(tmp->get_next()); // imposta la nuova head il nodo successivo a tmp(vecchia head)
delete tmp;
return this;
}
ListSL* delete_tail(){
Node<T>* prev = nullptr; // ricorda il nodo prima dell ultimo
Node<T>* tmp = this->get_head();
if(tmp == nullptr){
return this; // se evuota esci
}
while(tmp->get_next() != nullptr){
prev = tmp;
tmp = tmp->get_next(); // scorri la lista fino alla fine
}
delete tmp;
if(prev != nullptr){
prev->set_next(nullptr); // se cera piu di un nodo prev diventa il nuovo tail
}else{
this->head = nullptr;
tmp = nullptr; //altrimnti tutto svuotato
}
return this;
}
T copy_head() {
return this->head->get_key();
}
};
template <typename T> ostream& operator<<(ostream& os, ListSL<T>& lista){
Node<T>* tmp = lista.get_head();
while (tmp != nullptr){
os << *tmp << " -> ";
tmp = tmp->get_next();
}
os << "nullptr";
return os;
}
template <typename T> class Stack : protected ListSL<T>{ // perforza protected
public:
Stack() : ListSL<T>() {}
Stack(const T& key) : ListSL<T>(key) {}
~Stack() {
}
Stack* push(const T& key){
return static_cast<Stack<T>*>(ListSL<T>::add_head(key));
//ListSL<T>::add_head(key);
//return this;
}
Stack* pop(){
return static_cast<Stack<T>*>(ListSL<T>::delete_head());
// ListSL<T>::delete_head();
// return this;
}
Node<T>* get_head() {
return ListSL<T>::get_head();
}
T copy_head() {
return ListSL<T>::copy_head();
}
friend ostream& operator<<(ostream& os, Stack<T>& pila){
operator<<(os, static_cast<ListSL<T>&>(pila));
return os;
}
};
template <typename T> class Queue : protected ListSL<T> {
public:
Queue() : ListSL<T>() {}
Queue(const T& key) :ListSL<T>(key) {}
Queue* enqueue(const T& key){
return static_cast<Queue<T>*>(ListSL<T>::add_tail(key));
}
Queue* dequeue() {
return static_cast<Queue<T>*>(ListSL<T>::delete_head());
}
friend ostream& operator<<(ostream& os, Queue<T>& coda){
operator<<(os, static_cast<ListSL<T>&>(coda));
return os;
}
};
int main(){
Stack<int>* pila = new Stack<int>(10);
pila->push(23)->push(120);
Queue<int>* coda = new Queue<int>();
while(pila->get_head() != nullptr){
int tmp = pila->get_head()->get_key(); // T value = this->get_head()->get_key()
coda->enqueue(tmp);
pila->pop();
}
cout << *coda << endl;
delete pila;
delete coda;
}