summaryrefslogtreecommitdiff
path: root/I_anno
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-05-14 16:39:49 +0200
committerSanto Cariotti <dcariotti24@gmail.com>2020-05-14 16:39:49 +0200
commit81d68a2807241848a9f96ae9f0f0b405f348154f (patch)
treef1b822655f60580a0bb125c16b51da5e942142de /I_anno
parentb0e77499f3a87539dc2f1d9f64f7448d7c0914fc (diff)
fix: return node removed stack and queue
Diffstat (limited to 'I_anno')
-rw-r--r--I_anno/Programmazione_2/data_structures/queue.cc13
-rw-r--r--I_anno/Programmazione_2/data_structures/stack.cc13
2 files changed, 17 insertions, 9 deletions
diff --git a/I_anno/Programmazione_2/data_structures/queue.cc b/I_anno/Programmazione_2/data_structures/queue.cc
index c565ac6..e1d4dcb 100644
--- a/I_anno/Programmazione_2/data_structures/queue.cc
+++ b/I_anno/Programmazione_2/data_structures/queue.cc
@@ -34,19 +34,21 @@ public:
return this;
}
- queue<T>* dequeue() {
+ node<T>* dequeue() {
auto iter = _head;
- if(!_head) return this;
+ if(!_head) return nullptr;
while(iter->next != _tail) {
iter = iter->next;
}
+ node<T>* elem = iter->next;
+
delete iter->next;
iter->next = nullptr;
_tail = iter;
- return this;
+ return elem;
}
void print() {
@@ -67,7 +69,10 @@ int main() {
q->dequeue();
q->enqueue(4)->enqueue(2)->enqueue(8);
- q->dequeue();
+ q->print();
+ auto e = q->dequeue();
+ if(e)
+ cout << e->value << endl;
q->enqueue(1);
q->print();
diff --git a/I_anno/Programmazione_2/data_structures/stack.cc b/I_anno/Programmazione_2/data_structures/stack.cc
index 400039d..f1cc7d1 100644
--- a/I_anno/Programmazione_2/data_structures/stack.cc
+++ b/I_anno/Programmazione_2/data_structures/stack.cc
@@ -32,14 +32,14 @@ public:
return this;
}
- stack<T>* pop() {
- if(!_head) return this;
+ node<T>* pop() {
+ if(!_head) return nullptr;
auto old_head = _head;
+ delete _head;
_head = _head->next;
- delete old_head;
- return this;
+ return old_head;
}
void print() {
@@ -59,7 +59,10 @@ int main() {
s->pop();
s->push(4)->push(2)->push(8);
- s->pop();
+ s->print();
+ auto e = s->pop();
+ if(e)
+ cout << e->value << endl;
s->push(1);
s->print();