diff --git a/solution.cpp b/solution.cpp new file mode 100644 index 0000000..d7fb740 --- /dev/null +++ b/solution.cpp @@ -0,0 +1,207 @@ +#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 { + +public: + NotFoundError() : runtime_error("Node not found!") {} +}; + +class NullNodeError : public runtime_error { + +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; + ~Node() { + if(next){ + next->previous = previous; +} + if(previous){ + previous->next = next; +} + previous=nullptr; + next=nullptr; + } +}; + +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> &nod); // 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> &nod) { + auto node = make_shared>(nod->value); + if (!nod) { + throw NullNodeError(); + } + if (!first) { + first = node; + } else { + first->previous = node; + 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; + } +} + +template List::~List() { + while (last->previous) { + shared_ptr> current = last->previous; + last = nullptr; + current->next = nullptr; + last = current; + } +} +int main() { + 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 { + lista.get("text"); + } catch (const EmptyListError &e) { + cout << e.what() << endl; + } + cout << "------------------------------" << endl << endl; + + lista.add(nodeElo); + lista.add(move(nodeHello)); + lista.addFirst(nodeNapis); + + cout << "Nullptr exceptions:" << endl; + cout << "------------------------------" << endl; + try { + lista.add(move(nodeHello)); + } catch (const NullNodeError &e) { + cout << e.what() << endl; + } + try { + 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"); + } catch (const NotFoundError &e) { + cout << e.what() << endl; + } + try { + auto node = lista.getBackward("text"); + } catch (const NotFoundError &e) { + cout << e.what() << endl; + } + cout << "------------------------------" << endl << endl; + + return 0; +}