summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-05-13 20:21:54 +0200
committerSanto Cariotti <dcariotti24@gmail.com>2020-05-13 20:21:54 +0200
commite2e7a2e4fa6e50397da79da9cb8c2aec76779575 (patch)
treefa2e8654b759c3eb2b274c2751f02299d2f48c95
parent0d2341999a9c844954aca9ed4cc739b8fde34584 (diff)
fix: improve code quality for data structures
-rw-r--r--I_anno/Programmazione_2/data_structures/circle_double_list.cc126
-rw-r--r--I_anno/Programmazione_2/data_structures/circle_list.cc118
-rw-r--r--I_anno/Programmazione_2/data_structures/list.cc60
-rw-r--r--I_anno/Programmazione_2/data_structures/list_double.cc76
-rw-r--r--I_anno/Programmazione_2/data_structures/queue.cc2
-rw-r--r--I_anno/Programmazione_2/data_structures/stack.cc2
6 files changed, 179 insertions, 205 deletions
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<T>* tmp = _head->next;
- delete tmp;
- _head = tmp;
- }
- }
-
- node<T>* last_element() {
node<T>* iter = _head;
- while(iter && iter->next != _head) {
+ while(iter->next != _head) {
+ node<T>* tmp = iter;
+ delete tmp;
iter = iter->next;
}
-
- 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) break;
- }
-
- if(iter == _head) return nullptr;
-
- return iter;
+ node<T>* tmp = iter;
+ delete tmp;
}
list* push_front(T val) {
- auto elem = last_element();
- if(_head == nullptr) {
+ auto elem = _last();
+ if(!_head) {
_head = new node<T>{val, nullptr, nullptr};
_head->prev = _head->next = _head;
} else {
_head = new node<T>{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<T>{val, nullptr, nullptr};
- _head->prev = _head->next = _head;
- } else {
- auto last_e = last_element();
- last_e->next = new node<T>{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<T>{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<T>* iter = _head;
- while(iter && iter->value != val)
- iter = iter->next;
-
- if(iter == nullptr)
- return this->push_front(newval);
-
- node<T>* nod = new node<T>{newval, iter, iter->next};
- iter->next = nod;
- nod->next->prev = nod;
+ node<T>* iter = _search(val);
+ if(iter) {
+ node<T>* nod = new node<T>{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<T>* elem = _search(val);
+ if(!elem) return this;
node<T>* 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<T>* nod = new node<T>{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<T>* elem = _search(val);
+ if(!elem) return this;
node<T>* 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<T>* 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<T>* 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<T>* iter = _head;
- auto last_e = last_element();
+ auto last_e = _last();
if(last_e == _head) {
delete iter;
@@ -187,6 +145,27 @@ public:
}
}
private:
+ node<T>* _last() {
+ node<T>* iter = _head;
+ while(iter && iter->next != _head) {
+ iter = iter->next;
+ }
+
+ return iter;
+ }
+
+ node<T>* _search(T val) {
+ 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;
+ }
+
node<T>* _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<T>* tmp = _head->next;
- delete tmp;
- _head = tmp;
- }
- }
-
- node<T>* last_element() {
node<T>* iter = _head;
- while(iter && iter->next != _head) {
- iter = iter->next;
- }
-
- 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) {
+ while(iter->next != _head) {
+ node<T>* tmp = iter;
+ delete tmp;
iter = iter->next;
}
-
- if(iter == _head) return nullptr;
-
- return iter;
+ node<T>* tmp = iter;
+ delete tmp;
}
list* push_front(T val) {
- auto elem = last_element();
+ auto elem = _last();
_head = new node<T>{val, _head};
- if(elem == nullptr)
+ if(!elem)
elem = _head;
elem->next = _head;
@@ -59,57 +37,61 @@ public:
}
list* push_back(T val) {
- node<T>* iter = _head;
- if(_head == nullptr) {
- _head = new node<T>{val, _head};
- } else {
- while(iter->next != nullptr && iter->next != _head) {
- iter = iter->next;
- }
- iter->next = new node<T>{val, _head};
- }
+ if(!_head) return this->push_front(val);
+
+ auto last_e = _last();
+ last_e->next = new node<T>{val, _head};
+
return this;
}
list* push_after_value(T val, T newval) {
- if(!search(val)) return this;
+ node<T>* iter = _search(val);
+ if(iter)
+ iter->next = new node<T>{newval, iter->next};
+
+ return this;
+ }
+
+ list* push_before_value(T val, T newval) {
+ node<T>* elem = _search(val);
+ if(!elem) return this;
node<T>* 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<T>{newval, iter->next};
return this;
}
- list* push_before_value(T val, T newval) {
- if(!search(val)) return this;
+ list* pop(int val) {
+ node<T>* elem = _search(val);
+ if(!elem) return this;
node<T>* 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<T>{newval, iter->next};
+ node<T>* 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<T>* iter = _head;
- auto last_e = last_element();
+ auto last_e = _last();
if(last_e == _head) {
delete iter;
@@ -155,6 +137,27 @@ public:
}
}
private:
+ node<T>* _last() {
+ node<T>* iter = _head;
+ while(iter && iter->next != _head) {
+ iter = iter->next;
+ }
+
+ return iter;
+ }
+
+ node<T>* _search(T val) {
+ 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;
+ }
+
node<T>* _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<T>* 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<T>* iter = _head;
- if(_head == nullptr) {
- _head = new node<T>{val, nullptr};
- } else {
- while(iter->next != nullptr) {
- iter = iter->next;
- }
- iter->next = new node<T>{val, nullptr};
+ while(iter->next) {
+ iter = iter->next;
}
+ iter->next = new node<T>{val, nullptr};
return this;
}
list* push_after_value(T val, T newval) {
- node<T>* iter = _head;
- while(iter && iter->value != val)
- iter = iter->next;
-
- if(iter == nullptr)
- return this->push_front(newval);
-
- iter->next = new node<T>{newval, iter->next};
+ node<T>* iter = _search(val);
+ if(iter)
+ iter->next = new node<T>{newval, iter->next};
return this;
}
list* push_before_value(T val, T newval) {
+ node<T>* elem = _search(val);
+ if(!elem) return this;
+
node<T>* 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<T>{newval, iter->next};
return this;
}
list* pop(int val) {
- if(_head == nullptr)
- return this;
- else if(_head->value == val)
- return pop_front();
+ node<T>* elem = _search(val);
+ if(!elem) return this;
node<T>* 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<T>* temp = iter->next;
@@ -90,7 +83,7 @@ public:
}
list* pop_front() {
- if(_head == nullptr)
+ if(!_head)
return this;
node<T>* elem = _head;
@@ -101,7 +94,7 @@ public:
}
list* pop_back() {
- if(_head == nullptr)
+ if(!_head)
return this;
node<T>* 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<T>* iter = _head;
- while(iter != nullptr) {
+ while(iter) {
cout << iter->value << ' ';
iter = iter->next;
}
}
private:
+ node<T>* _search(T value) {
+ node<T>* iter = _head;
+ while(iter && iter->value != value)
+ iter = iter->next;
+
+ return iter;
+ }
+
node<T>* _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<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) {
if(_head == nullptr) {
_head = new node<T>{val, nullptr, nullptr};
@@ -51,50 +35,40 @@ public:
}
list* push_back(T val) {
+ if(!_head) return this->push_front(val);
+
node<T>* iter = _head;
- if(_head == nullptr) {
- _head = new node<T>{val, nullptr, nullptr};
- } else {
- while(iter->next != nullptr) {
- iter = iter->next;
- }
- iter->next = new node<T>{val, iter, nullptr};
+ while(iter->next) {
+ iter = iter->next;
}
+ iter->next = new node<T>{val, iter, nullptr};
return this;
}
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;
-
- if(iter == nullptr)
- return this->push_front(newval);
-
- node<T>* nod = new node<T>{newval, iter, iter->next};
- iter->next = nod;
- nod->next->prev = nod;
+ node<T>* elem = _search(val);
+ if(elem) {
+ node<T>* nod = new node<T>{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<T>* elem = _search(val);
+ if(!elem) return this;
node<T>* 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<T>* nod = new node<T>{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<T>* elem = _search(val);
+ if(!elem) return this;
node<T>* 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<T>* temp = iter->next;
@@ -168,6 +140,14 @@ public:
}
}
private:
+ node<T>* _search(T value) {
+ node<T>* iter = _head;
+ while(iter && iter->value != value)
+ iter = iter->next;
+
+ return iter;
+ }
+
node<T>* _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 T>
class queue {
public:
queue() : _head{nullptr} {}
+
~queue() {
auto iter = _head;
while(iter) {
@@ -19,6 +20,7 @@ public:
iter = iter->next;
}
}
+
queue<T>* 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 T>
class stack {
public:
stack() : _head{nullptr} {}
+
~stack() {
auto iter = _head;
while(iter) {
@@ -19,6 +20,7 @@ public:
iter = iter->next;
}
}
+
stack<T>* push(T val) {
if(!_head) {