From f279107065146a4940f5e73602a1c3c09e58b31d Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 18 Oct 2020 18:56:43 +0200 Subject: chore: name of first year folder --- 1_anno/Programmazione_2/data_structures/list.cc | 164 ++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 1_anno/Programmazione_2/data_structures/list.cc (limited to '1_anno/Programmazione_2/data_structures/list.cc') diff --git a/1_anno/Programmazione_2/data_structures/list.cc b/1_anno/Programmazione_2/data_structures/list.cc new file mode 100644 index 0000000..8ddcbf9 --- /dev/null +++ b/1_anno/Programmazione_2/data_structures/list.cc @@ -0,0 +1,164 @@ +#include + +using namespace std; + +template +struct node { + T value; + node* next; +}; + + +template +class list { +public: + list() : _head{nullptr} {} + + ~list() { + while(_head) { + node* tmp = _head->next; + delete tmp; + _head = tmp; + } + } + + list* push_front(T val) { + _head = new node{val, _head}; + + return this; + } + + list* push_back(T val) { + if(!_head) return this->push_front(val); + + node* iter = _head; + while(iter->next) { + iter = iter->next; + } + iter->next = new node{val, nullptr}; + + return this; + } + + list* push_after_value(T val, T newval) { + 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->next != elem) + iter = iter->next; + + iter->next = new node{newval, iter->next}; + + return this; + } + + list* pop(int val) { + node* elem = _search(val); + if(!elem) return this; + + node* iter = _head; + if(iter == elem) return this->pop_front(); + + while(iter->next != elem) + iter = iter->next; + + node* temp = iter->next; + iter->next = iter->next->next; + delete temp; + + return this; + } + + list* pop_front() { + if(!_head) + return this; + + node* elem = _head; + _head = _head->next; + delete elem; + + return this; + } + + list* pop_back() { + if(!_head) + return this; + + node* iter = _head; + + while(iter->next && iter->next->next) { + iter = iter->next; + } + + if(!iter->next) { + delete iter; + _head = nullptr; + } else if(iter->next->next) { + delete iter->next->next; + } else { + delete iter->next; + } + + iter->next = nullptr; + + return this; + } + + void print() { + node* iter = _head; + 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; +}; + +int main() { + list* l = new list{}; + l->push_front(2)->push_front(1); + l->print(); cout << endl; + l->push_back(5); + l->print(); cout << endl; + l->push_back(10)->push_back(15); + l->print(); cout << endl; + l->push_after_value(5, 6); + l->print(); cout << endl; + l->push_before_value(5, 4); + l->print(); cout << endl; + l->push_before_value(4, 3); + l->print(); cout << endl; + l->pop_back(); + l->print(); cout << endl; + l->pop_front(); + l->print(); cout << endl; + l->push_front(1); + l->print(); cout << endl; + l->pop(1); + l->print(); cout << endl; + + delete l; + return 0; +} -- cgit v1.2.3-18-g5258