From fa49124ad79f6f0783433ddbdb4d45b9940cc3ab Mon Sep 17 00:00:00 2001 From: 226332 <226332@student.pwr.edu.pl> Date: Mon, 10 Apr 2017 12:09:34 +0200 Subject: [PATCH 1/5] Moja propozycja --- solution.cpp | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 solution.cpp diff --git a/solution.cpp b/solution.cpp new file mode 100644 index 0000000..b7f3224 --- /dev/null +++ b/solution.cpp @@ -0,0 +1,213 @@ +#include +#include +#include +#include +#include +using namespace std; +class EmptyListError:public runtime_error{ + static ostringstream cnvt; +public: + EmptyListError() + :runtime_error("Usage of empty list!") + {} +}; + +class NotFoundError:public runtime_error{ + static ostringstream cnvt; +public: + NotFoundError() + :runtime_error("Node not found!") + {} +}; + +class NullNodeError:public runtime_error{ + static ostringstream cnvt; +public: + NullNodeError() + :runtime_error("Cannot add empty Node pointer!") + {} +}; + + + +template +class Node +{ +public: + Node(const T v) : + next(nullptr), + previous(nullptr), + value(v) + {} + shared_ptr> next; + shared_ptr> previous; + T value; +}; + +template +class List +{ +public: + List(); + ~List(){} + // dodaje element na koniec listy + void add(shared_ptr> &&node); // przenoszenie + void add(shared_ptr> &nod); // kopiowanie + shared_ptr> get(const T value); // zwraca element o wskazanej wartości + void addFirst(shared_ptr> node); // dodaje element na początek listy + shared_ptr> getBackward(const T value); // zwraca element o wskazanej wartości od końca + + +private: + shared_ptr> first; + shared_ptr> last; +}; +template +List::List() + :first(nullptr) + ,last(nullptr) + {} + +template +void List::add(shared_ptr> &&node){ + if(!node){ + throw NullNodeError(); + } + if(!first){ + first = node; + }else if (!first->next){ + first->next=node; + last=node; + last->previous=first; + }else{ + node->previous=last; + last->next=node; + last=node; + } + node=nullptr; +} + +template +void List::add(shared_ptr> &nod){ + if(!nod){ + throw NullNodeError(); + } + auto node=make_shared>(nod->value); + if(!first){ + first = node; + }else if (!first->next){ + first->next=node; + last=node; + last->previous=first; + }else{ + node->previous=last; + last->next=node; + last=node; + } +} + +template +void List::addFirst(shared_ptr> node){ + if(!first){ + first = node; + }else{ + node->next=first; + first=node; + } +} +template +shared_ptr> List::get(const T value){ + if(!first){ + throw EmptyListError(); + return nullptr; + }else{ + shared_ptr> current = first; + do{ + if(current->value == value){ + cout << "Found value " << current->value << endl; + return current; + } + else{ + cout << "Going through " << current->value << endl; + current = current->next; + } + } while(current); + throw NotFoundError(); + return nullptr; + } +} + +template +shared_ptr> List::getBackward(const T value){ + shared_ptr> current=last; + if(!last){ + current=first; + } + + if(!first){ + throw EmptyListError(); + return nullptr; + }else{ + do{ + if(current->value == value){ + cout << "Found value " << current->value << endl; + return current; + } + else{ + cout << "Going through " << current->value << endl; + current = current->previous; + } + } while(current); + throw NotFoundError(); + return nullptr; + } +} + +int main() +{ + List lista; + auto nodeNapis=make_shared>("napis"); + auto nodeHello=make_shared>("hello"); + cout<<"Empty list exceptions:"< Date: Mon, 10 Apr 2017 12:18:12 +0200 Subject: [PATCH 2/5] Removed unused libs and formated --- solution.cpp | 336 +++++++++++++++++++++++---------------------------- 1 file changed, 153 insertions(+), 183 deletions(-) diff --git a/solution.cpp b/solution.cpp index b7f3224..f1ed2bb 100644 --- a/solution.cpp +++ b/solution.cpp @@ -1,213 +1,183 @@ #include #include -#include -#include -#include using namespace std; -class EmptyListError:public runtime_error{ - static ostringstream cnvt; -public: - EmptyListError() - :runtime_error("Usage of empty list!") - {} -}; +class EmptyListError : public runtime_error { + static ostringstream cnvt; -class NotFoundError:public runtime_error{ - static ostringstream cnvt; public: - NotFoundError() - :runtime_error("Node not found!") - {} + EmptyListError() : runtime_error("Usage of empty list!") {} }; -class NullNodeError:public runtime_error{ - static ostringstream cnvt; +class NotFoundError : public runtime_error { + static ostringstream cnvt; + public: - NullNodeError() - :runtime_error("Cannot add empty Node pointer!") - {} + NotFoundError() : runtime_error("Node not found!") {} }; +class NullNodeError : public runtime_error { + static ostringstream cnvt; +public: + NullNodeError() : runtime_error("Cannot add empty Node pointer!") {} +}; -template -class Node -{ +template class Node { public: - Node(const T v) : - next(nullptr), - previous(nullptr), - value(v) - {} - shared_ptr> next; - shared_ptr> previous; - T value; + Node(const T v) : next(nullptr), previous(nullptr), value(v) {} + shared_ptr> next; + shared_ptr> previous; + T value; }; -template -class List -{ +template class List { public: - List(); - ~List(){} - // dodaje element na koniec listy - void add(shared_ptr> &&node); // przenoszenie - void add(shared_ptr> &nod); // kopiowanie - shared_ptr> get(const T value); // zwraca element o wskazanej wartości - void addFirst(shared_ptr> node); // dodaje element na początek listy - shared_ptr> getBackward(const T value); // zwraca element o wskazanej wartości od końca - - + List(); + ~List() {} + // dodaje element na koniec listy + void add(shared_ptr> &&node); // przenoszenie + void add(shared_ptr> &nod); // kopiowanie + shared_ptr> get(const T value); // zwraca element o wskazanej wartości + void addFirst(shared_ptr> node); // dodaje element na początek listy + shared_ptr> + getBackward(const T value); // zwraca element o wskazanej wartości od końca + private: - shared_ptr> first; - shared_ptr> last; + shared_ptr> first; + shared_ptr> last; }; -template -List::List() - :first(nullptr) - ,last(nullptr) - {} - -template -void List::add(shared_ptr> &&node){ - if(!node){ - throw NullNodeError(); - } - if(!first){ - first = node; - }else if (!first->next){ - first->next=node; - last=node; - last->previous=first; - }else{ - node->previous=last; - last->next=node; - last=node; - } - node=nullptr; +template List::List() : first(nullptr), last(nullptr) {} + +template void List::add(shared_ptr> &&node) { + if (!node) { + throw NullNodeError(); + } + if (!first) { + first = node; + } else if (!first->next) { + first->next = node; + last = node; + last->previous = first; + } else { + node->previous = last; + last->next = node; + last = node; + } + node = nullptr; } -template -void List::add(shared_ptr> &nod){ - if(!nod){ - throw NullNodeError(); - } - auto node=make_shared>(nod->value); - if(!first){ - first = node; - }else if (!first->next){ - first->next=node; - last=node; - last->previous=first; - }else{ - node->previous=last; - last->next=node; - last=node; - } +template void List::add(shared_ptr> &nod) { + if (!nod) { + throw NullNodeError(); + } + auto node = make_shared>(nod->value); + if (!first) { + first = node; + } else if (!first->next) { + first->next = node; + last = node; + last->previous = first; + } else { + node->previous = last; + last->next = node; + last = node; + } } -template -void List::addFirst(shared_ptr> node){ - if(!first){ - first = node; - }else{ - node->next=first; - first=node; - } +template void List::addFirst(shared_ptr> node) { + if (!first) { + first = node; + } else { + node->next = first; + first = node; + } } -template -shared_ptr> List::get(const T value){ - if(!first){ - throw EmptyListError(); - return nullptr; - }else{ - shared_ptr> current = first; - do{ - if(current->value == value){ - cout << "Found value " << current->value << endl; - return current; - } - else{ - cout << "Going through " << current->value << endl; - current = current->next; - } - } while(current); - throw NotFoundError(); - return nullptr; - } +template shared_ptr> List::get(const T value) { + if (!first) { + throw EmptyListError(); + return nullptr; + } else { + shared_ptr> current = first; + do { + if (current->value == value) { + cout << "Found value " << current->value << endl; + return current; + } else { + cout << "Going through " << current->value << endl; + current = current->next; + } + } while (current); + throw NotFoundError(); + return nullptr; + } } -template -shared_ptr> List::getBackward(const T value){ - shared_ptr> current=last; - if(!last){ - current=first; - } - - if(!first){ - throw EmptyListError(); - return nullptr; - }else{ - do{ - if(current->value == value){ - cout << "Found value " << current->value << endl; - return current; - } - else{ - cout << "Going through " << current->value << endl; - current = current->previous; - } - } while(current); - throw NotFoundError(); - return nullptr; - } +template shared_ptr> List::getBackward(const T value) { + shared_ptr> current = last; + if (!last) { + current = first; + } + + if (!first) { + throw EmptyListError(); + return nullptr; + } else { + do { + if (current->value == value) { + cout << "Found value " << current->value << endl; + return current; + } else { + cout << "Going through " << current->value << endl; + current = current->previous; + } + } while (current); + throw NotFoundError(); + return nullptr; + } } -int main() -{ - List lista; - auto nodeNapis=make_shared>("napis"); - auto nodeHello=make_shared>("hello"); - cout<<"Empty list exceptions:"< lista; + auto nodeNapis = make_shared>("napis"); + auto nodeHello = make_shared>("hello"); + cout << "Empty list exceptions:" << endl; + cout << "------------------------------" << endl; + try { + auto node = lista.get("text"); + } catch (const EmptyListError &e) { + cout << e.what() << endl; + } + cout << "------------------------------" << endl << endl; + + lista.add(nodeNapis); + lista.add(move(nodeHello)); + + cout << "Nullptr exceptions:" << endl; + cout << "------------------------------" << endl; + try { lista.add(move(nodeHello)); - - - cout<<"Nullptr exceptions:"< Date: Mon, 10 Apr 2017 12:26:29 +0200 Subject: [PATCH 3/5] list is now a unique ptr --- solution.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/solution.cpp b/solution.cpp index f1ed2bb..d0ee5d4 100644 --- a/solution.cpp +++ b/solution.cpp @@ -137,30 +137,30 @@ template shared_ptr> List::getBackward(const T value) { } int main() { - List lista; + unique_ptr> lista; auto nodeNapis = make_shared>("napis"); auto nodeHello = make_shared>("hello"); cout << "Empty list exceptions:" << endl; cout << "------------------------------" << endl; try { - auto node = lista.get("text"); + auto node = lista->get("text"); } catch (const EmptyListError &e) { cout << e.what() << endl; } cout << "------------------------------" << endl << endl; - lista.add(nodeNapis); - lista.add(move(nodeHello)); + lista->add(nodeNapis); + lista->add(move(nodeHello)); cout << "Nullptr exceptions:" << endl; cout << "------------------------------" << endl; try { - lista.add(move(nodeHello)); + lista->add(move(nodeHello)); } catch (const NullNodeError &e) { cout << e.what() << endl; } try { - lista.add(nullptr); + lista->add(nullptr); } catch (const NullNodeError &e) { cout << e.what() << endl; } @@ -168,12 +168,12 @@ int main() { cout << "Not found exceptions:" << endl; cout << "------------------------------" << endl; try { - auto node = lista.get("text"); + auto node = lista->get("text"); } catch (const NotFoundError &e) { cout << e.what() << endl; } try { - auto node = lista.getBackward("text"); + auto node = lista->getBackward("text"); } catch (const NotFoundError &e) { cout << e.what() << endl; } From c7f1d88147b9c5c6edc543ef64e109613c202855 Mon Sep 17 00:00:00 2001 From: 226332 <226332@student.pwr.edu.pl> Date: Mon, 10 Apr 2017 13:30:30 +0200 Subject: [PATCH 4/5] Now it's working and ready to check --- solution.cpp | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/solution.cpp b/solution.cpp index d0ee5d4..3e8f61c 100644 --- a/solution.cpp +++ b/solution.cpp @@ -9,14 +9,12 @@ class EmptyListError : public runtime_error { }; class NotFoundError : public runtime_error { - static ostringstream cnvt; public: NotFoundError() : runtime_error("Node not found!") {} }; class NullNodeError : public runtime_error { - static ostringstream cnvt; public: NullNodeError() : runtime_error("Cannot add empty Node pointer!") {} @@ -28,17 +26,21 @@ template class Node { shared_ptr> next; shared_ptr> previous; T value; + ~Node() { + next = nullptr; + previous = nullptr; + } }; template class List { public: List(); - ~List() {} + ~List(); // dodaje element na koniec listy void add(shared_ptr> &&node); // przenoszenie void add(shared_ptr> &nod); // kopiowanie shared_ptr> get(const T value); // zwraca element o wskazanej wartości - void addFirst(shared_ptr> node); // dodaje element na początek listy + void addFirst(shared_ptr> &nod); // dodaje element na początek listy shared_ptr> getBackward(const T value); // zwraca element o wskazanej wartości od końca @@ -84,10 +86,15 @@ template void List::add(shared_ptr> &nod) { } } -template void List::addFirst(shared_ptr> node) { +template void List::addFirst(shared_ptr> &nod) { + auto node = make_shared>(nod->value); + if (!nod) { + throw NullNodeError(); + } if (!first) { first = node; } else { + first->previous = node; node->next = first; first = node; } @@ -136,44 +143,55 @@ template shared_ptr> List::getBackward(const T value) { } } +template List::~List() { + while (last->previous) { + shared_ptr> current = last->previous; + last = nullptr; + current->next = nullptr; + last = current; + } +} int main() { - unique_ptr> lista; + List lista; auto nodeNapis = make_shared>("napis"); + auto nodeElo = make_shared>("elo"); auto nodeHello = make_shared>("hello"); cout << "Empty list exceptions:" << endl; cout << "------------------------------" << endl; try { - auto node = lista->get("text"); + lista.get("text"); } catch (const EmptyListError &e) { cout << e.what() << endl; } cout << "------------------------------" << endl << endl; - lista->add(nodeNapis); - lista->add(move(nodeHello)); + lista.add(nodeElo); + lista.add(move(nodeHello)); + lista.addFirst(nodeNapis); cout << "Nullptr exceptions:" << endl; cout << "------------------------------" << endl; try { - lista->add(move(nodeHello)); + lista.add(move(nodeHello)); } catch (const NullNodeError &e) { cout << e.what() << endl; } try { - lista->add(nullptr); + lista.add(nullptr); } catch (const NullNodeError &e) { cout << e.what() << endl; } + cout << "------------------------------" << endl << endl; cout << "Not found exceptions:" << endl; cout << "------------------------------" << endl; try { - auto node = lista->get("text"); + auto node = lista.get("text"); } catch (const NotFoundError &e) { cout << e.what() << endl; } try { - auto node = lista->getBackward("text"); + auto node = lista.getBackward("text"); } catch (const NotFoundError &e) { cout << e.what() << endl; } From bb1af301afc0811d238d9a5195c1655acbba2f20 Mon Sep 17 00:00:00 2001 From: 226332 <226332@student.pwr.edu.pl> Date: Mon, 10 Apr 2017 14:00:56 +0200 Subject: [PATCH 5/5] Update solution.cpp --- solution.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/solution.cpp b/solution.cpp index 3e8f61c..d7fb740 100644 --- a/solution.cpp +++ b/solution.cpp @@ -27,8 +27,14 @@ template class Node { shared_ptr> previous; T value; ~Node() { - next = nullptr; - previous = nullptr; + if(next){ + next->previous = previous; +} + if(previous){ + previous->next = next; +} + previous=nullptr; + next=nullptr; } };