From 954ea3f949cd55c6b15c0bd8a1390639398946e1 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 21 Nov 2022 14:23:39 +0000 Subject: [PATCH] #1 Removed tail property from Linked List --- DataStructures/src/linkedlist.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/DataStructures/src/linkedlist.h b/DataStructures/src/linkedlist.h index 9312013..29822cb 100644 --- a/DataStructures/src/linkedlist.h +++ b/DataStructures/src/linkedlist.h @@ -18,7 +18,6 @@ namespace Datastructures { private: std::shared_ptr> getIndex(int index); std::shared_ptr> mHead; - std::shared_ptr> mTail; int mCount; }; @@ -27,7 +26,6 @@ namespace Datastructures { LinkedList::LinkedList() { mCount = 0; mHead = nullptr; - mTail = nullptr; } //Class constructor for the Linked List, taking a preset value to go into the head of the list @@ -35,7 +33,6 @@ namespace Datastructures { LinkedList::LinkedList(T value) { mCount = 1; mHead = std::make_shared>(value); - mTail = mHead; } template @@ -46,13 +43,12 @@ namespace Datastructures { mCount++; if (mHead == nullptr) { mHead = std::make_shared>(value); - mTail = mHead; return; } - //Add new node and set to tail. - mTail->next = std::make_shared>(value); - mTail = mTail->next; + //Add new node (Count already altered so we need to look a node before the end) + std::shared_ptr> node = this->getIndex(mCount - 2); + node->next = std::make_shared>(value); } template