From 337fa15b59b22bddaa39f95b2a53de41483dfd10 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 3 May 2020 16:50:13 +0200 Subject: fix: list, print and remove --- I_anno/Programmazione_2/data_structures/list.cc | 87 +++++++++++++++++++------ 1 file changed, 68 insertions(+), 19 deletions(-) (limited to 'I_anno/Programmazione_2') diff --git a/I_anno/Programmazione_2/data_structures/list.cc b/I_anno/Programmazione_2/data_structures/list.cc index c0915e7..02ae2fd 100644 --- a/I_anno/Programmazione_2/data_structures/list.cc +++ b/I_anno/Programmazione_2/data_structures/list.cc @@ -58,6 +58,9 @@ public: list* push_before_value(T val, T newval) { node* iter = _head; + if(iter->value == val) + return this->push_front(newval); + while(iter && iter->next && iter->next->value != val) iter = iter->next; @@ -69,6 +72,41 @@ public: return this; } + list* pop_front() { + if(_head == nullptr) + return this; + + node* elem = _head; + _head = _head->next; + delete elem; + + return this; + } + + list* pop_back() { + if(_head == nullptr) + return this; + + node* iter = _head; + + while(iter->next && iter->next->next) { + iter = iter->next; + } + + if(iter->next == nullptr) { + delete iter; + _head = nullptr; + } else if(iter->next->next) { + delete iter->next->next; + } else { + delete iter->next; + } + + iter->next = nullptr; + + return this; + } + void print() { node* iter = _head; while(iter != nullptr) { @@ -81,25 +119,36 @@ private: }; int main() { - list* l = new list{}; - l->push_front(2)->push_front(1.7); - // 1.7 2 - - l->push_back(5.2); - // 2 1.7 5.2 - - l->push_back(10.1)->push_back(15); - // 2 1.7 5.2 10.1 15 - - l->push_after_value(5.2, 6.4); - // 2 1.7 5.2 6.4 10.1 15 - - l->push_before_value(5.2, 4.1); - // 2 1.7 4.1 5.2 6.4 10.1 15 - - l->push_before_value(4.1, 3.0); - // 2 1.7 3.0 4.1 5.2 6.4 10.1 15 - l->print(); + list* l = new list{}; + l->push_front(2)->push_front(1); + l->print(); cout << endl; + l->push_back(5); + l->print(); cout << endl; + l->push_back(10)->push_back(15); + l->print(); cout << endl; + l->push_after_value(5, 6); + l->print(); cout << endl; + l->push_before_value(5, 4); + l->print(); cout << endl; + l->push_before_value(4, 3); + l->print(); cout << endl; + l->pop_back(); + l->print(); cout << endl; + l->pop_front(); + l->print(); cout << endl; + l->push_front(1); + l->print(); cout << endl; + + // output + // 1 2 + // 1 2 5 + // 1 2 5 10 15 + // 1 2 5 6 10 15 + // 1 2 4 5 6 10 15 + // 1 2 3 4 5 6 10 15 + // 1 2 3 4 5 6 10 + // 2 3 4 5 6 10 + // 1 2 3 4 5 6 10 return 0; } -- cgit v1.2.3-18-g5258