#1 Removed tail property from Linked List

This commit is contained in:
Luke Else 2022-11-21 14:23:39 +00:00
parent e338ddb255
commit 954ea3f949

View File

@ -18,7 +18,6 @@ namespace Datastructures {
private: private:
std::shared_ptr<Nodes::LinkedListNode<T>> getIndex(int index); std::shared_ptr<Nodes::LinkedListNode<T>> getIndex(int index);
std::shared_ptr<Nodes::LinkedListNode<T>> mHead; std::shared_ptr<Nodes::LinkedListNode<T>> mHead;
std::shared_ptr<Nodes::LinkedListNode<T>> mTail;
int mCount; int mCount;
}; };
@ -27,7 +26,6 @@ namespace Datastructures {
LinkedList<T>::LinkedList() { LinkedList<T>::LinkedList() {
mCount = 0; mCount = 0;
mHead = nullptr; mHead = nullptr;
mTail = nullptr;
} }
//Class constructor for the Linked List, taking a preset value to go into the head of the list //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<T>::LinkedList(T value) { LinkedList<T>::LinkedList(T value) {
mCount = 1; mCount = 1;
mHead = std::make_shared<Nodes::LinkedListNode<T>>(value); mHead = std::make_shared<Nodes::LinkedListNode<T>>(value);
mTail = mHead;
} }
template <typename T> template <typename T>
@ -46,13 +43,12 @@ namespace Datastructures {
mCount++; mCount++;
if (mHead == nullptr) { if (mHead == nullptr) {
mHead = std::make_shared<Nodes::LinkedListNode<T>>(value); mHead = std::make_shared<Nodes::LinkedListNode<T>>(value);
mTail = mHead;
return; return;
} }
//Add new node and set to tail. //Add new node (Count already altered so we need to look a node before the end)
mTail->next = std::make_shared<Nodes::LinkedListNode<T>>(value); std::shared_ptr<Nodes::LinkedListNode<T>> node = this->getIndex(mCount - 2);
mTail = mTail->next; node->next = std::make_shared<Nodes::LinkedListNode<T>>(value);
} }
template <typename T> template <typename T>