Compare commits
8 Commits
700d6696c8
...
master
Author | SHA1 | Date | |
---|---|---|---|
596d863a25 | |||
ee0556730d | |||
15c4684857 | |||
84645fad5f | |||
954ea3f949 | |||
e338ddb255 | |||
7cabc13196 | |||
32a5a1c51a |
@ -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 (mHead == nullptr) {
|
||||
mHead = std::make_shared<Nodes::LinkedListNode<T>>(value);
|
||||
mTail = mHead;
|
||||
return;
|
||||
}
|
||||
|
||||
//Add new node and set to tail.
|
||||
(*mTail).next = std::make_shared<Nodes::LinkedListNode<T>>(value);
|
||||
mTail = (*mTail).next;
|
||||
}
|
||||
}
|
142
DataStructures/src/linkedlist.h
Normal file
142
DataStructures/src/linkedlist.h
Normal 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;
|
||||
|
||||
}
|
||||
}
|
@ -1,7 +1,14 @@
|
||||
#include <LinkedList/linkedlist.h>
|
||||
#include <iostream>
|
||||
#include <linkedlist.h>
|
||||
|
||||
int main() {
|
||||
Datastructures::LinkedList<int> list;
|
||||
list.append(5);
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user