diff options
Diffstat (limited to 'algo1w3/NAWLinkedList.cpp')
-rw-r--r-- | algo1w3/NAWLinkedList.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/algo1w3/NAWLinkedList.cpp b/algo1w3/NAWLinkedList.cpp new file mode 100644 index 0000000..06e46d8 --- /dev/null +++ b/algo1w3/NAWLinkedList.cpp @@ -0,0 +1,61 @@ +#include <iostream> + +#include "NAWLinkedList.h" +#include "NAW.h" +#include "NAWLink.h" + +NAWLinkedList::NAWLinkedList() { } + +NAWLinkedList::~NAWLinkedList() { + NAWLink* node = this->next; + while (node != nullptr) { + NAWLink* next = node->next; + delete node; // also deletes NAW instance -> see NAWLink::~NAWLink + node = next; + } +} + +void NAWLinkedList::addToStart(const NAW& value) { + this->next = new NAWLink(value, this->next); + this->size++; +} + +NAWLink* NAWLinkedList::search(const NAW& value) const { + NAWLink* node = this->next; + while (node != nullptr) { + if (node->value->compareTo(value) == 0) return node; + node = node->next; + } + return nullptr; +} + +NAWLink* NAWLinkedList::searchParent(const NAW& value) const { + NAWLink* node = this->next; + while (node != nullptr) { + if (node->next == nullptr) return nullptr; + if (node->next->value->compareTo(value) == 0) return node; + node = node->next; + } + return nullptr; +} + +void NAWLinkedList::showAll() const { + std::cout << "/------------------------------\\" << std::endl; + NAWLink* node = this->next; + while (node != nullptr) { + std::cout << *node->value; // prints values -> see operator << (std::ostream&, const NAW&) + node = node->next; + if (node != nullptr) std::cout << " ---- " << std::endl; + } + std::cout << "\\------------------------------/" << std::endl; +} + +NAWLink* NAWLinkedList::removeFirst(const NAW& value) { + NAWLink* parent = searchParent(value); + if (parent == nullptr) return nullptr; + + NAWLink* node = parent->next; + parent->next = parent->next->next; + + return node; +} |