From e2e7a2e4fa6e50397da79da9cb8c2aec76779575 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 13 May 2020 20:21:54 +0200 Subject: fix: improve code quality for data structures --- .../data_structures/circle_double_list.cc | 126 +++++++++------------ .../data_structures/circle_list.cc | 118 ++++++++++--------- I_anno/Programmazione_2/data_structures/list.cc | 60 +++++----- .../data_structures/list_double.cc | 76 +++++-------- I_anno/Programmazione_2/data_structures/queue.cc | 2 + I_anno/Programmazione_2/data_structures/stack.cc | 2 + 6 files changed, 179 insertions(+), 205 deletions(-) (limited to 'I_anno') diff --git a/I_anno/Programmazione_2/data_structures/circle_double_list.cc b/I_anno/Programmazione_2/data_structures/circle_double_list.cc index efd0f4a..e50456b 100644 --- a/I_anno/Programmazione_2/data_structures/circle_double_list.cc +++ b/I_anno/Programmazione_2/data_structures/circle_double_list.cc @@ -16,98 +16,61 @@ public: list() : _head{nullptr} {} ~list() { - while(_head != nullptr) { - node* tmp = _head->next; - delete tmp; - _head = tmp; - } - } - - node* last_element() { node* iter = _head; - while(iter && iter->next != _head) { + while(iter->next != _head) { + node* tmp = iter; + delete tmp; iter = iter->next; } - - return iter; - } - - node* search(T val) { - if(_head == nullptr) return nullptr; - - node* iter = _head; - if(iter->value == val) - return iter; - - while(iter && iter->value != val) { - iter = iter->next; - if(iter == _head) break; - } - - if(iter == _head) return nullptr; - - return iter; + node* tmp = iter; + delete tmp; } list* push_front(T val) { - auto elem = last_element(); - if(_head == nullptr) { + auto elem = _last(); + if(!_head) { _head = new node{val, nullptr, nullptr}; _head->prev = _head->next = _head; } else { _head = new node{val, elem, _head}; - elem->next = _head; - _head->next->prev = _head; + elem->next = _head->next->prev = _head; } return this; } list* push_back(T val) { - if(_head == nullptr) { - _head = new node{val, nullptr, nullptr}; - _head->prev = _head->next = _head; - } else { - auto last_e = last_element(); - last_e->next = new node{val, last_e, _head}; - _head->prev = last_e->next; - } + if(!_head) return this->push_front(val); + + auto last_e = _last(); + last_e->next = new node{val, last_e, _head}; + _head->prev = last_e->next; return this; } list* push_after_value(T val, T newval) { - if(!search(val)) return this; - - node* iter = _head; - while(iter && iter->value != val) - iter = iter->next; - - if(iter == nullptr) - return this->push_front(newval); - - node* nod = new node{newval, iter, iter->next}; - iter->next = nod; - nod->next->prev = nod; + node* iter = _search(val); + if(iter) { + node* nod = new node{newval, iter, iter->next}; + iter->next = nod; + nod->next->prev = nod; + } return this; } list* push_before_value(T val, T newval) { - if(!search(val)) return this; + node* elem = _search(val); + if(!elem) return this; node* iter = _head; if(iter->value == val) return this->push_front(newval); - while(iter && iter->next && iter->next->value != val) { + while(iter->next != elem) iter = iter->next; - if(iter == _head) break; - } - - if(iter == nullptr) - return this->push_front(newval); node* nod = new node{newval, iter, iter->next}; iter->next = nod; @@ -117,19 +80,14 @@ public: } list* pop(int val) { - - if(_head == nullptr) - return this; - else if(_head->value == val) - return pop_front(); - - if(!search(val)) return this; + node* elem = _search(val); + if(!elem) return this; node* iter = _head; - while(iter && iter->next && iter->next->value != val) { + if(iter == elem) return this->pop_front(); + + while(iter->next != elem) iter = iter->next; - if(iter == _head) break; - } node* temp = iter->next; iter->next = iter->next->next; @@ -140,10 +98,10 @@ public: } list* pop_front() { - if(_head == nullptr) + if(!_head) return this; - auto last_e = last_element(); + auto last_e = _last(); node* elem = _head; _head = _head->next; _head->prev = last_e; @@ -154,11 +112,11 @@ public: } list* pop_back() { - if(_head == nullptr) + if(!_head) return this; node* iter = _head; - auto last_e = last_element(); + auto last_e = _last(); if(last_e == _head) { delete iter; @@ -187,6 +145,27 @@ public: } } private: + node* _last() { + node* iter = _head; + while(iter && iter->next != _head) { + iter = iter->next; + } + + return iter; + } + + node* _search(T val) { + node* iter = _head; + if(iter->value == val) return iter; + + while(iter && iter->value != val) { + iter = iter->next; + if(iter == _head) return nullptr; + } + + return iter; + } + node* _head; }; @@ -208,5 +187,6 @@ int main() { l->pop(2); l->print(); cout << '\n'; + delete l; return 0; } diff --git a/I_anno/Programmazione_2/data_structures/circle_list.cc b/I_anno/Programmazione_2/data_structures/circle_list.cc index b6be463..2143ac1 100644 --- a/I_anno/Programmazione_2/data_structures/circle_list.cc +++ b/I_anno/Programmazione_2/data_structures/circle_list.cc @@ -15,42 +15,20 @@ public: list() : _head{nullptr} {} ~list() { - while(_head != nullptr) { - node* tmp = _head->next; - delete tmp; - _head = tmp; - } - } - - node* last_element() { node* iter = _head; - while(iter && iter->next != _head) { - iter = iter->next; - } - - return iter; - } - - node* search(T val) { - if(_head == nullptr) return nullptr; - - node* iter = _head; - if(iter->value == val) - return iter; - - while(iter && iter->value != val) { + while(iter->next != _head) { + node* tmp = iter; + delete tmp; iter = iter->next; } - - if(iter == _head) return nullptr; - - return iter; + node* tmp = iter; + delete tmp; } list* push_front(T val) { - auto elem = last_element(); + auto elem = _last(); _head = new node{val, _head}; - if(elem == nullptr) + if(!elem) elem = _head; elem->next = _head; @@ -59,57 +37,61 @@ public: } list* push_back(T val) { - node* iter = _head; - if(_head == nullptr) { - _head = new node{val, _head}; - } else { - while(iter->next != nullptr && iter->next != _head) { - iter = iter->next; - } - iter->next = new node{val, _head}; - } + if(!_head) return this->push_front(val); + + auto last_e = _last(); + last_e->next = new node{val, _head}; + return this; } list* push_after_value(T val, T newval) { - if(!search(val)) return this; + node* iter = _search(val); + if(iter) + iter->next = new node{newval, iter->next}; + + return this; + } + + list* push_before_value(T val, T newval) { + node* elem = _search(val); + if(!elem) return this; node* iter = _head; - while(iter && iter->value != val) - iter = iter->next; - if(iter == nullptr) + if(iter->value == val) return this->push_front(newval); + while(iter->next != elem) + iter = iter->next; + iter->next = new node{newval, iter->next}; return this; } - list* push_before_value(T val, T newval) { - if(!search(val)) return this; + list* pop(int val) { + node* elem = _search(val); + if(!elem) return this; node* iter = _head; + if(iter == elem) return this->pop_front(); - if(iter->value == val) - return this->push_front(newval); - - while(iter && iter->next && iter->next->value != val) + while(iter->next != elem) iter = iter->next; - if(iter == nullptr) - return this->push_front(newval); - - iter->next = new node{newval, iter->next}; + node* temp = iter->next; + iter->next = iter->next->next; + delete temp; return this; } list* pop_front() { - if(_head == nullptr) + if(!_head) return this; - auto last_e = last_element(); + auto last_e = _last(); if(last_e == _head) { delete _head; @@ -125,11 +107,11 @@ public: } list* pop_back() { - if(_head == nullptr) + if(!_head) return this; node* iter = _head; - auto last_e = last_element(); + auto last_e = _last(); if(last_e == _head) { delete iter; @@ -155,6 +137,27 @@ public: } } private: + node* _last() { + node* iter = _head; + while(iter && iter->next != _head) { + iter = iter->next; + } + + return iter; + } + + node* _search(T val) { + node* iter = _head; + if(iter->value == val) return iter; + + while(iter && iter->value != val) { + iter = iter->next; + if(iter == _head) return nullptr; + } + + return iter; + } + node* _head; }; @@ -178,6 +181,9 @@ int main() { l->print(); cout << endl; l->push_before_value(3, 5); l->print(); cout << endl; + l->pop(5); + l->print(); cout << endl; + delete l; return 0; } diff --git a/I_anno/Programmazione_2/data_structures/list.cc b/I_anno/Programmazione_2/data_structures/list.cc index 2aa97de..8ddcbf9 100644 --- a/I_anno/Programmazione_2/data_structures/list.cc +++ b/I_anno/Programmazione_2/data_structures/list.cc @@ -15,7 +15,7 @@ public: list() : _head{nullptr} {} ~list() { - while(_head != nullptr) { + while(_head) { node* tmp = _head->next; delete tmp; _head = tmp; @@ -29,57 +29,50 @@ public: } list* push_back(T val) { + if(!_head) return this->push_front(val); + node* iter = _head; - if(_head == nullptr) { - _head = new node{val, nullptr}; - } else { - while(iter->next != nullptr) { - iter = iter->next; - } - iter->next = new node{val, nullptr}; + while(iter->next) { + iter = iter->next; } + iter->next = new node{val, nullptr}; return this; } list* push_after_value(T val, T newval) { - node* iter = _head; - while(iter && iter->value != val) - iter = iter->next; - - if(iter == nullptr) - return this->push_front(newval); - - iter->next = new node{newval, iter->next}; + node* iter = _search(val); + if(iter) + iter->next = new node{newval, iter->next}; return this; } list* push_before_value(T val, T newval) { + node* elem = _search(val); + if(!elem) return this; + node* iter = _head; if(iter->value == val) return this->push_front(newval); - while(iter && iter->next && iter->next->value != val) + while(iter->next != elem) iter = iter->next; - if(iter == nullptr) - return this->push_front(newval); - iter->next = new node{newval, iter->next}; return this; } list* pop(int val) { - if(_head == nullptr) - return this; - else if(_head->value == val) - return pop_front(); + node* elem = _search(val); + if(!elem) return this; node* iter = _head; - while(iter && iter->next && iter->next->value != val) + if(iter == elem) return this->pop_front(); + + while(iter->next != elem) iter = iter->next; node* temp = iter->next; @@ -90,7 +83,7 @@ public: } list* pop_front() { - if(_head == nullptr) + if(!_head) return this; node* elem = _head; @@ -101,7 +94,7 @@ public: } list* pop_back() { - if(_head == nullptr) + if(!_head) return this; node* iter = _head; @@ -110,7 +103,7 @@ public: iter = iter->next; } - if(iter->next == nullptr) { + if(!iter->next) { delete iter; _head = nullptr; } else if(iter->next->next) { @@ -126,12 +119,20 @@ public: void print() { node* iter = _head; - while(iter != nullptr) { + while(iter) { cout << iter->value << ' '; iter = iter->next; } } private: + node* _search(T value) { + node* iter = _head; + while(iter && iter->value != value) + iter = iter->next; + + return iter; + } + node* _head; }; @@ -158,5 +159,6 @@ int main() { l->pop(1); l->print(); cout << endl; + delete l; return 0; } diff --git a/I_anno/Programmazione_2/data_structures/list_double.cc b/I_anno/Programmazione_2/data_structures/list_double.cc index 9dc11b7..79cd8c3 100644 --- a/I_anno/Programmazione_2/data_structures/list_double.cc +++ b/I_anno/Programmazione_2/data_structures/list_double.cc @@ -23,22 +23,6 @@ public: } } - node* search(T val) { - if(_head == nullptr) return nullptr; - - node* iter = _head; - if(iter->value == val) - return iter; - - while(iter && iter->value != val) { - iter = iter->next; - } - - if(iter == _head) return nullptr; - - return iter; - } - list* push_front(T val) { if(_head == nullptr) { _head = new node{val, nullptr, nullptr}; @@ -51,50 +35,40 @@ public: } list* push_back(T val) { + if(!_head) return this->push_front(val); + node* iter = _head; - if(_head == nullptr) { - _head = new node{val, nullptr, nullptr}; - } else { - while(iter->next != nullptr) { - iter = iter->next; - } - iter->next = new node{val, iter, nullptr}; + while(iter->next) { + iter = iter->next; } + iter->next = new node{val, iter, nullptr}; return this; } list* push_after_value(T val, T newval) { - if(!search(val)) return this; - - node* iter = _head; - while(iter && iter->value != val) - iter = iter->next; - - if(iter == nullptr) - return this->push_front(newval); - - node* nod = new node{newval, iter, iter->next}; - iter->next = nod; - nod->next->prev = nod; + node* elem = _search(val); + if(elem) { + node* nod = new node{newval, elem, elem->next}; + elem->next = nod; + nod->next->prev = nod; + } return this; } list* push_before_value(T val, T newval) { - if(!search(val)) return this; + node* elem = _search(val); + if(!elem) return this; node* iter = _head; if(iter->value == val) return this->push_front(newval); - while(iter && iter->next && iter->next->value != val) + while(iter->next != elem) iter = iter->next; - if(iter == nullptr) - return this->push_front(newval); - node* nod = new node{newval, iter, iter->next}; iter->next = nod; nod->next->prev = nod; @@ -103,15 +77,13 @@ public: } list* pop(int val) { - if(_head == nullptr) - return this; - else if(_head->value == val) - return pop_front(); - - if(!search(val)) return this; + node* elem = _search(val); + if(!elem) return this; node* iter = _head; - while(iter && iter->next && iter->next->value != val) + if(iter == elem) return this->pop_front(); + + while(iter->next != elem) iter = iter->next; node* temp = iter->next; @@ -168,6 +140,14 @@ public: } } private: + node* _search(T value) { + node* iter = _head; + while(iter && iter->value != value) + iter = iter->next; + + return iter; + } + node* _head; }; @@ -194,5 +174,7 @@ int main() { l->pop(2); l->print(); cout << endl; + delete l; + return 0; } diff --git a/I_anno/Programmazione_2/data_structures/queue.cc b/I_anno/Programmazione_2/data_structures/queue.cc index 476074f..c565ac6 100644 --- a/I_anno/Programmazione_2/data_structures/queue.cc +++ b/I_anno/Programmazione_2/data_structures/queue.cc @@ -12,6 +12,7 @@ template class queue { public: queue() : _head{nullptr} {} + ~queue() { auto iter = _head; while(iter) { @@ -19,6 +20,7 @@ public: iter = iter->next; } } + queue* enqueue(T val) { if(!_head) { diff --git a/I_anno/Programmazione_2/data_structures/stack.cc b/I_anno/Programmazione_2/data_structures/stack.cc index efdb939..400039d 100644 --- a/I_anno/Programmazione_2/data_structures/stack.cc +++ b/I_anno/Programmazione_2/data_structures/stack.cc @@ -12,6 +12,7 @@ template class stack { public: stack() : _head{nullptr} {} + ~stack() { auto iter = _head; while(iter) { @@ -19,6 +20,7 @@ public: iter = iter->next; } } + stack* push(T val) { if(!_head) { -- cgit v1.2.3-18-g5258