diff options
author | Santo Cariotti <dcariotti24@gmail.com> | 2020-05-24 15:45:44 +0200 |
---|---|---|
committer | Santo Cariotti <dcariotti24@gmail.com> | 2020-05-24 15:45:44 +0200 |
commit | f04e3a465b28b921f89695ca4743ac8b439b17f1 (patch) | |
tree | eb31e7d58b6f150562499b831073b6a656eacef3 /I_anno/Programmazione_2 | |
parent | d58e4f1ff807fc7ef321495ed6e231ea798102d5 (diff) |
fix: queue and stack push/pop
Diffstat (limited to 'I_anno/Programmazione_2')
-rw-r--r-- | I_anno/Programmazione_2/data_structures/queue.cc | 19 | ||||
-rw-r--r-- | I_anno/Programmazione_2/data_structures/stack.cc | 7 |
2 files changed, 8 insertions, 18 deletions
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<T>* dequeue() { - auto iter = _head; - 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 elem; + auto iter = _head; + delete _head; + _head = iter->next; + return iter; } void print() { @@ -60,8 +51,8 @@ public: cout << endl; } private: - node<T>* _tail; node<T>* _head; + node<T>* _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<T>* pop() { if(!_head) return nullptr; - - auto old_head = _head; + node<T>* elem = _head; delete _head; - _head = _head->next; + _head = elem->next; - return old_head; + return elem; } void print() { |