summaryrefslogtreecommitdiff
path: root/I_anno/Programmazione_2/data_structures/list.cc
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-05-03 18:34:18 +0200
committerSanto Cariotti <dcariotti24@gmail.com>2020-05-03 18:37:14 +0200
commit31162629a08bc2d3c9a8a4ca5ee58f35c01cea1e (patch)
treefe8c3db560138204f47fd111f93107691eedbb78 /I_anno/Programmazione_2/data_structures/list.cc
parent24066d1e8fc7bb9b99b1aa84a8a78e0d516005ad (diff)
fix: pop w value
Diffstat (limited to 'I_anno/Programmazione_2/data_structures/list.cc')
-rw-r--r--I_anno/Programmazione_2/data_structures/list.cc30
1 files changed, 19 insertions, 11 deletions
diff --git a/I_anno/Programmazione_2/data_structures/list.cc b/I_anno/Programmazione_2/data_structures/list.cc
index 02ae2fd..2aa97de 100644
--- a/I_anno/Programmazione_2/data_structures/list.cc
+++ b/I_anno/Programmazione_2/data_structures/list.cc
@@ -72,6 +72,23 @@ public:
return this;
}
+ list* pop(int val) {
+ if(_head == nullptr)
+ return this;
+ else if(_head->value == val)
+ return pop_front();
+
+ node<T>* iter = _head;
+ while(iter && iter->next && iter->next->value != val)
+ iter = iter->next;
+
+ node<T>* temp = iter->next;
+ iter->next = iter->next->next;
+ delete temp;
+
+ return this;
+ }
+
list* pop_front() {
if(_head == nullptr)
return this;
@@ -138,17 +155,8 @@ int main() {
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
+ l->pop(1);
+ l->print(); cout << endl;
return 0;
}