Compare commits

...

6 Commits

2 changed files with 83 additions and 22 deletions

View File

@ -9,16 +9,20 @@ namespace Datastructures {
LinkedList();
LinkedList(T value);
~LinkedList();
T operator[](int index);
T& operator[](int index);
void append(T value);
bool insert(T value, int index);
bool remove(int index);
int count() const;
int find(const T &value) const;
enum EFindResult {
eNotFound = -1,
};
private:
std::shared_ptr<Nodes::LinkedListNode<T>> getIndex(int index);
std::shared_ptr<Nodes::LinkedListNode<T>> mHead;
std::shared_ptr<Nodes::LinkedListNode<T>> mTail;
int mCount;
};
@ -27,7 +31,6 @@ namespace Datastructures {
LinkedList<T>::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,53 +38,105 @@ namespace Datastructures {
LinkedList<T>::LinkedList(T value) {
mCount = 1;
mHead = std::make_shared<Nodes::LinkedListNode<T>>(value);
mTail = mHead;
}
template <typename T>
LinkedList<T>::~LinkedList() {}
template <typename T>
T& LinkedList<T>::operator[](int index) {
//Return the value from a given index. (Will return null pointer if out of range)
return this->getIndex(index)->value;
}
template <typename T>
void LinkedList<T>::append(T value) {
mCount++;
if (mHead == nullptr) {
mHead = std::make_shared<Nodes::LinkedListNode<T>>(value);
mTail = mHead;
return;
}
//Add new node and set to tail.
<<<<<<< HEAD:DataStructures/src/linkedlist.h
mTail->mNext = std::make_shared<Nodes::LinkedListNode<T>>(value);
mTail = mTail->mNext;
//Add new node (Count already altered so we need to look a node before the end)
std::shared_ptr<Nodes::LinkedListNode<T>> node = this->getIndex(mCount - 2);
node->next = std::make_shared<Nodes::LinkedListNode<T>>(value);
}
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)
if (index == 0 || this->mCount == 0)
{//If the new item needs to go into the head of the linked list.
mHead = std::make_shared<Nodes::LinkedListNode<T>>(value, mHead);
}
else
{
//Get the node before the one we are going to be inserting
std::shared_ptr<Nodes::LinkedListNode<T>> node = this->getIndex(index-1);
//If node is nullptr, index is out of range
if (node == nullptr)
return false;
node->next = std::make_shared<Nodes::LinkedListNode<T>>(value, node->next);
}
//Append the new value into a new node.
mCount++;
return true;
}
template <typename T>
bool LinkedList<T>::remove(int index) {
//If the list is empty
if (this->mCount == 0)
return false;
//Append the new value into a new node.
node->mNext = std::make_shared<Nodes::LinkedListNode<T>>(value, node->mNext);
//Check if we are looking to remove the head node (0)
if (index == 0)
{
this->mHead = this->mHead->next;
}
else
{
//Get the node before the one that is about to be removed
std::shared_ptr<Nodes::LinkedListNode<T>> node = this->getIndex(index - 1);
//Point this node to the node pointed to by the one about to be removed
node->next = node->next->next;
}
//Reduce the total count of nodes
mCount--;
return true;
}
template <typename T>
int LinkedList<T>::count() const {
return this->mCount;
}
template <typename T>
int LinkedList<T>::find(const T& value) const {
//Start at the head of the list
std::shared_ptr<Nodes::LinkedListNode<T>> node = mHead;
for (int i = 0; i < this->mCount; i++)
{//Loop through list to try and find value
if (node->value == value)
return i;
node = node->next;
}
return EFindResult::eNotFound; //-1
}
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())
if (index < 0 || index >= this->mCount)
return nullptr;
std::shared_ptr<Nodes::LinkedListNode<T>> node = mHead;
for (size_t i = 0; i < index; i++)
for (int i = 0; i < index; i++)
{//Interate through the linked list i times to get to the index.
node = node->mNext;
node = node->next;
}
return node;
=======
(*mTail).next = std::make_shared<Nodes::LinkedListNode<T>>(value);
mTail = (*mTail).next;
>>>>>>> 700d6696c82b4ea8ba783238ef73b7efc630dd54:DataStructures/src/LinkedList/linkedlist.h
}
}

View File

@ -1,8 +1,14 @@
#include <iostream>
#include <linkedlist.h>
int main() {
Datastructures::LinkedList<int> list;
list.append(5);
list.append(200);
list.insert(20, 0);
list.insert(20, 2);
std::cout << list.count() << std::endl; //3
std::cout << list[0] << list[1] << list[2] << std::endl; //520020
list.remove(0);
int x = list.find(10);
std::cout << list.count() << std::endl; //2
}