From f04e3a465b28b921f89695ca4743ac8b439b17f1 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 24 May 2020 15:45:44 +0200 Subject: fix: queue and stack push/pop --- I_anno/Programmazione_2/data_structures/queue.cc | 19 +++++-------------- I_anno/Programmazione_2/data_structures/stack.cc | 7 +++---- 2 files changed, 8 insertions(+), 18 deletions(-) (limited to 'I_anno/Programmazione_2/data_structures') diff --git a/I_anno/Programmazione_2/data_structures/queue.cc b/I_anno/Programmazione_2/data_structures/queue.cc index e1d4dcb..8398459 100644 --- a/I_anno/Programmazione_2/data_structures/queue.cc +++ b/I_anno/Programmazione_2/data_structures/queue.cc @@ -35,20 +35,11 @@ public: } node* dequeue() { - auto iter = _head; - if(!_head) return nullptr; - while(iter->next != _tail) { - iter = iter->next; - } - - node* elem = iter->next; - - delete iter->next; - iter->next = nullptr; - _tail = iter; - - return elem; + auto iter = _head; + delete _head; + _head = iter->next; + return iter; } void print() { @@ -60,8 +51,8 @@ public: cout << endl; } private: - node* _tail; node* _head; + node* _tail; }; int main() { diff --git a/I_anno/Programmazione_2/data_structures/stack.cc b/I_anno/Programmazione_2/data_structures/stack.cc index f1cc7d1..ffff780 100644 --- a/I_anno/Programmazione_2/data_structures/stack.cc +++ b/I_anno/Programmazione_2/data_structures/stack.cc @@ -34,12 +34,11 @@ public: node* pop() { if(!_head) return nullptr; - - auto old_head = _head; + node* elem = _head; delete _head; - _head = _head->next; + _head = elem->next; - return old_head; + return elem; } void print() { -- cgit v1.2.3-18-g5258