Updated Linked List to have constructors

This commit is contained in:
Luke Else 2022-11-13 13:10:21 +00:00
parent 7a75d22514
commit 462d1ccc83
2 changed files with 28 additions and 10 deletions

View File

@ -7,10 +7,34 @@ namespace Datastructures {
{ {
public: public:
LinkedList(); LinkedList();
LinkedList(T value);
~LinkedList(); ~LinkedList();
void append(T value);
bool insert(T value, int index);
bool remove(int index);
int count() const;
private: private:
Nodes::LinkedListNode<T> mHead; std::shared_ptr<Nodes::LinkedListNode<T>> mHead;
Nodes::LinkedListNode<T> mTail; std::shared_ptr<Nodes::LinkedListNode<T>> mTail;
int mCount; 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;
}
} }

View File

@ -7,16 +7,10 @@ namespace Datastructures {
class LinkedListNode : public Generic::UndirectedNode<T, LinkedListNode<T>> class LinkedListNode : public Generic::UndirectedNode<T, LinkedListNode<T>>
{ {
public: public:
//Inherit Constructor 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;
std::shared_ptr<LinkedListNode<T>> create(T value, std::shared_ptr<LinkedListNode<T>> next = nullptr); using Generic::UndirectedNode<T, LinkedListNode<T>>::~UndirectedNode;
private: private:
}; };
//Creates a new node, returning a smart pointer to a stack allocated object
template <typename T>
std::shared_ptr<LinkedListNode<T>> LinkedListNode<T>::create(T value, std::shared_ptr<LinkedListNode<T>> next) {
return std::make_shared<LinkedListNode<T>>(value, next);
}
} }
} }