From f3f1bd47f8af2c5c445d2201792d82dd02b24c9b Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 9 May 2020 11:59:05 +0200 Subject: fix: search function --- .../data_structures/circle_double_list.cc | 2 ++ .../data_structures/circle_list.cc | 21 +++++++++++++++++++++ .../data_structures/list_double.cc | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+) (limited to 'I_anno/Programmazione_2') 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 96ac31a..efd0f4a 100644 --- a/I_anno/Programmazione_2/data_structures/circle_double_list.cc +++ b/I_anno/Programmazione_2/data_structures/circle_double_list.cc @@ -33,6 +33,8 @@ public: } node* search(T val) { + if(_head == nullptr) return nullptr; + node* iter = _head; if(iter->value == val) return iter; diff --git a/I_anno/Programmazione_2/data_structures/circle_list.cc b/I_anno/Programmazione_2/data_structures/circle_list.cc index ee32a8c..b6be463 100644 --- a/I_anno/Programmazione_2/data_structures/circle_list.cc +++ b/I_anno/Programmazione_2/data_structures/circle_list.cc @@ -31,6 +31,22 @@ public: 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) return nullptr; + + return iter; + } + list* push_front(T val) { auto elem = last_element(); _head = new node{val, _head}; @@ -56,6 +72,8 @@ public: } list* push_after_value(T val, T newval) { + if(!search(val)) return this; + node* iter = _head; while(iter && iter->value != val) iter = iter->next; @@ -69,6 +87,8 @@ public: } list* push_before_value(T val, T newval) { + if(!search(val)) return this; + node* iter = _head; if(iter->value == val) @@ -140,6 +160,7 @@ private: int main() { list* l = new list{}; + l->push_before_value(4, 1); l->push_back(4); l->push_back(1); l->push_back(0); diff --git a/I_anno/Programmazione_2/data_structures/list_double.cc b/I_anno/Programmazione_2/data_structures/list_double.cc index d8dda99..9dc11b7 100644 --- a/I_anno/Programmazione_2/data_structures/list_double.cc +++ b/I_anno/Programmazione_2/data_structures/list_double.cc @@ -23,6 +23,22 @@ 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}; @@ -49,6 +65,8 @@ public: } list* push_after_value(T val, T newval) { + if(!search(val)) return this; + node* iter = _head; while(iter && iter->value != val) iter = iter->next; @@ -64,6 +82,8 @@ public: } list* push_before_value(T val, T newval) { + if(!search(val)) return this; + node* iter = _head; if(iter->value == val) @@ -88,6 +108,8 @@ public: else if(_head->value == val) return pop_front(); + if(!search(val)) return this; + node* iter = _head; while(iter && iter->next && iter->next->value != val) iter = iter->next; -- cgit v1.2.3-18-g5258