summaryrefslogtreecommitdiff
path: root/I_anno/Programmazione_2/data_structures/circle_list.cc
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-05-09 11:59:05 +0200
committerSanto Cariotti <dcariotti24@gmail.com>2020-05-09 11:59:05 +0200
commitf3f1bd47f8af2c5c445d2201792d82dd02b24c9b (patch)
treef516ebdfed3039ea717f273cf53530bc3decfc58 /I_anno/Programmazione_2/data_structures/circle_list.cc
parent802a578c02bd7f071c59000f45ae9c052cc1fb62 (diff)
fix: search function
Diffstat (limited to 'I_anno/Programmazione_2/data_structures/circle_list.cc')
-rw-r--r--I_anno/Programmazione_2/data_structures/circle_list.cc21
1 files changed, 21 insertions, 0 deletions
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<T>* search(T val) {
+ if(_head == nullptr) return nullptr;
+
+ node<T>* 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<T>{val, _head};
@@ -56,6 +72,8 @@ public:
}
list* push_after_value(T val, T newval) {
+ if(!search(val)) return this;
+
node<T>* 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<T>* iter = _head;
if(iter->value == val)
@@ -140,6 +160,7 @@ private:
int main() {
list<int>* l = new list<int>{};
+ l->push_before_value(4, 1);
l->push_back(4);
l->push_back(1);
l->push_back(0);