#include #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; }