|
|
@ -1,5 +1,5 @@
|
|
|
|
#pragma once
|
|
|
|
#pragma once
|
|
|
|
#include "linkedlistnode.h"
|
|
|
|
#include "Nodes/linkedlistnode.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Datastructures {
|
|
|
|
namespace Datastructures {
|
|
|
|
template <typename T>
|
|
|
|
template <typename T>
|
|
|
@ -9,12 +9,14 @@ namespace Datastructures {
|
|
|
|
LinkedList();
|
|
|
|
LinkedList();
|
|
|
|
LinkedList(T value);
|
|
|
|
LinkedList(T value);
|
|
|
|
~LinkedList();
|
|
|
|
~LinkedList();
|
|
|
|
|
|
|
|
T operator[](int index);
|
|
|
|
void append(T value);
|
|
|
|
void append(T value);
|
|
|
|
bool insert(T value, int index);
|
|
|
|
bool insert(T value, int index);
|
|
|
|
bool remove(int index);
|
|
|
|
bool remove(int index);
|
|
|
|
int count() const;
|
|
|
|
int count() const;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
|
|
|
|
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;
|
|
|
|
std::shared_ptr<Nodes::LinkedListNode<T>> mTail;
|
|
|
|
int mCount;
|
|
|
|
int mCount;
|
|
|
@ -49,7 +51,37 @@ namespace Datastructures {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Add new node and set to tail.
|
|
|
|
//Add new node and set to tail.
|
|
|
|
|
|
|
|
<<<<<<< HEAD:DataStructures/src/linkedlist.h
|
|
|
|
|
|
|
|
mTail->mNext = std::make_shared<Nodes::LinkedListNode<T>>(value);
|
|
|
|
|
|
|
|
mTail = mTail->mNext;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
|
|
|
bool LinkedList<T>::insert(T value, int index) {
|
|
|
|
|
|
|
|
std::shared_ptr<Nodes::LinkedListNode<T>> node = this->getIndex(index);
|
|
|
|
|
|
|
|
//If node is nullptr, index is out of range
|
|
|
|
|
|
|
|
if (node == nullptr)
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Append the new value into a new node.
|
|
|
|
|
|
|
|
node->mNext = std::make_shared<Nodes::LinkedListNode<T>>(value, node->mNext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
|
|
|
std::shared_ptr<Nodes::LinkedListNode<T>> LinkedList<T>::getIndex(int index) {
|
|
|
|
|
|
|
|
//Check if the value lies within the range of the list.
|
|
|
|
|
|
|
|
if (index < 0 || index >= this->count())
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Nodes::LinkedListNode<T>> node = mHead;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < index; i++)
|
|
|
|
|
|
|
|
{//Interate through the linked list i times to get to the index.
|
|
|
|
|
|
|
|
node = node->mNext;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
|
|
|
=======
|
|
|
|
(*mTail).next = std::make_shared<Nodes::LinkedListNode<T>>(value);
|
|
|
|
(*mTail).next = std::make_shared<Nodes::LinkedListNode<T>>(value);
|
|
|
|
mTail = (*mTail).next;
|
|
|
|
mTail = (*mTail).next;
|
|
|
|
|
|
|
|
>>>>>>> 700d6696c82b4ea8ba783238ef73b7efc630dd54:DataStructures/src/LinkedList/linkedlist.h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|