From 71df7745a0ebf2c6bc991ccaf16b6cf3c7739698 Mon Sep 17 00:00:00 2001 From: rasmuskozak <85702965+rasmuskozak@users.noreply.github.com> Date: Wed, 22 Sep 2021 09:03:46 +0200 Subject: [PATCH] Updated the TODOs in linked_list_tpp --- .../.vscode/settings.json | 5 + .../linked_list.hpp | 4 +- .../linked_list.tpp | 97 ++++++++++++++++--- 3 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 hw04/ordered_list_linked_starter_code/.vscode/settings.json diff --git a/hw04/ordered_list_linked_starter_code/.vscode/settings.json b/hw04/ordered_list_linked_starter_code/.vscode/settings.json new file mode 100644 index 0000000..4a67f19 --- /dev/null +++ b/hw04/ordered_list_linked_starter_code/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "*.tpp": "cpp" + } +} \ No newline at end of file diff --git a/hw04/ordered_list_linked_starter_code/linked_list.hpp b/hw04/ordered_list_linked_starter_code/linked_list.hpp index 8f867e0..d284012 100644 --- a/hw04/ordered_list_linked_starter_code/linked_list.hpp +++ b/hw04/ordered_list_linked_starter_code/linked_list.hpp @@ -47,8 +47,8 @@ class LinkedList: public AbstractList private: - //TODO - + Node head; + }; #include "linked_list.tpp" diff --git a/hw04/ordered_list_linked_starter_code/linked_list.tpp b/hw04/ordered_list_linked_starter_code/linked_list.tpp index adbdc12..af68a68 100644 --- a/hw04/ordered_list_linked_starter_code/linked_list.tpp +++ b/hw04/ordered_list_linked_starter_code/linked_list.tpp @@ -1,74 +1,143 @@ #include "linked_list.hpp" +#include "Node.hpp" +#include template LinkedList::LinkedList() { - //TODO + head->next = nullptr; } template LinkedList::~LinkedList() { - //TODO + Node* current = new Node; + while(current != nullptr) + { + Node* next = new Node{current->next}; + delete current; + current = next; + } } template LinkedList::LinkedList(const LinkedList& x) { - //TODO + Node* current = new Node; + Node* current_x = new Node; + current = this.head; + current_x = x.head; + + if(head == nullptr) + { + x.head.item = head.item; + return; + } + + while(current != nullptr) + { + current = current -> next; + current_x -> next = new Node{current->item}; + } } template void LinkedList::swap(LinkedList& x, LinkedList& y) { - //TODO + std::swap(x.head, y.head); } template LinkedList& LinkedList::operator=(const LinkedList& x) { - //TODO + LinkedList temp(x); + std::swap(temp.head, head); return *this; } template bool LinkedList::isEmpty() const { - //TODO - return true; + if(head == nullptr) + return true; + else + return false; } template std::size_t LinkedList::getLength() const { - //TODO - return 0; + int count = 0; + Node* temp = new Node{head}; + while(temp != nullptr) + { + temp = temp->next; + count++; + } + return count; } template bool LinkedList::insert(std::size_t position, const T& item) { - //TODO - return true; + Node* current = new Node; + Node* slack = new Node; + current = head; + int count{0}; + + while(count < position && slack != nullptr) + { + slack = current; + current = current -> next; + count++; + } + + if(slack != nullptr) + { + Node newNode = new Node; + newNode.item = item; + slack -> next = newNode; + newNode -> next = current; + return true; + }else + { + return false; + } + } template bool LinkedList::remove(std::size_t position) { - //TODO + Node* current = new Node; + Node* slack = new Node; + Node* next_node = new Node; + current = head; + int count{0}; + + while(count < position) + { + slack = current; + current = current -> next; + } + + next_node = current ->next; + delete current; + slack -> next = next_node; + return true; } template void LinkedList::clear() { - //TODO + LinkedList::~LinkedList(); } template T LinkedList::getEntry(std::size_t position) const { - //TODO + //TODO Not quiet sure what the purpose of this is? return head? return T(); }