Compare commits

...

11 Commits

6 changed files with 160 additions and 69 deletions

View File

@ -8,17 +8,17 @@ namespace Generic {
public: public:
DirectedNode(T value, std::shared_ptr<NodeType> left = nullptr, std::shared_ptr<NodeType> right = nullptr); DirectedNode(T value, std::shared_ptr<NodeType> left = nullptr, std::shared_ptr<NodeType> right = nullptr);
~DirectedNode(); ~DirectedNode();
T value;
std::shared_ptr<NodeType> left;
std::shared_ptr<NodeType> right;
protected: protected:
T mValue;
std::shared_ptr<NodeType> mLeft;
std::shared_ptr<NodeType> mRight;
}; };
template <typename T, typename NodeType> template <typename T, typename NodeType>
DirectedNode<T, NodeType>::DirectedNode(T value, std::shared_ptr<NodeType> left, std::shared_ptr<NodeType> right) { DirectedNode<T, NodeType>::DirectedNode(T value, std::shared_ptr<NodeType> left, std::shared_ptr<NodeType> right) {
this->mValue = value; this->value = value;
this->mLeft = left; this->left = left;
this->mRight = right; this->right = right;
} }
template <typename T, typename NodeType> template <typename T, typename NodeType>

View File

@ -8,15 +8,14 @@ namespace Generic {
public: public:
UndirectedNode(T value, std::shared_ptr<NodeType> next = nullptr); UndirectedNode(T value, std::shared_ptr<NodeType> next = nullptr);
~UndirectedNode(); ~UndirectedNode();
protected: T value;
T mValue; std::shared_ptr<NodeType> next;
std::shared_ptr<NodeType> mNext;
}; };
template <typename T, typename NodeType> template <typename T, typename NodeType>
UndirectedNode<T, NodeType>::UndirectedNode(T value, std::shared_ptr<NodeType> next) { UndirectedNode<T, NodeType>::UndirectedNode(T value, std::shared_ptr<NodeType> next) {
this->mValue = value; this->value = value;
this->mNext = next; this->next = next;
} }
template <typename T, typename NodeType> template <typename T, typename NodeType>

View File

@ -1,55 +0,0 @@
#pragma once
#include "linkedlistnode.h"
namespace Datastructures {
template <typename T>
class LinkedList
{
public:
LinkedList();
LinkedList(T value);
~LinkedList();
void append(T value);
bool insert(T value, int index);
bool remove(int index);
int count() const;
private:
std::shared_ptr<Nodes::LinkedListNode<T>> mHead;
std::shared_ptr<Nodes::LinkedListNode<T>> mTail;
int mCount;
};
//Generic Class constructor for the Linked List
template <typename T>
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
template <typename T>
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>
void LinkedList<T>::append(T value) {
mCount++;
if (mCount == 0) {
mHead = std::make_shared<Nodes::LinkedListNode<T>>(value);
mTail = mHead;
return;
}
//Add new node and set to tail.
(*mTail).mNext = std::make_shared<Nodes::LinkedListNode<T>>(value);
mTail = (*mTail).mNext;
}
}

View File

@ -9,8 +9,6 @@ namespace Datastructures {
public: public:
//Inherit Constructor and destructor from generic Undirectetd Node //Inherit Constructor and destructor from generic Undirectetd Node
using Generic::UndirectedNode<T, LinkedListNode<T>>::UndirectedNode; using Generic::UndirectedNode<T, LinkedListNode<T>>::UndirectedNode;
template <typename T>
friend class LinkedList;
private: private:
}; };
} }

View File

@ -0,0 +1,142 @@
#pragma once
#include "Nodes/linkedlistnode.h"
namespace Datastructures {
template <typename T>
class LinkedList
{
public:
LinkedList();
LinkedList(T value);
~LinkedList();
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;
int mCount;
};
//Generic Class constructor for the Linked List
template <typename T>
LinkedList<T>::LinkedList() {
mCount = 0;
mHead = nullptr;
}
//Class constructor for the Linked List, taking a preset value to go into the head of the list
template <typename T>
LinkedList<T>::LinkedList(T value) {
mCount = 1;
mHead = std::make_shared<Nodes::LinkedListNode<T>>(value);
}
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);
return;
}
//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) {
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;
//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->mCount)
return nullptr;
std::shared_ptr<Nodes::LinkedListNode<T>> node = mHead;
for (int i = 0; i < index; i++)
{//Interate through the linked list i times to get to the index.
node = node->next;
}
return node;
}
}

View File

@ -1,7 +1,14 @@
#include <LinkedList/linkedlist.h> #include <iostream>
#include <linkedlist.h>
int main() { int main() {
Datastructures::LinkedList<int> list; Datastructures::LinkedList<int> list;
list.append(5); list.append(5);
list.append(200); list.append(200);
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
} }