Compare commits
No commits in common. "7cabc13196537b114d472703cb37adb1a8b83f41" and "700d6696c82b4ea8ba783238ef73b7efc630dd54" have entirely different histories.
7cabc13196
...
700d6696c8
@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Nodes/linkedlistnode.h"
|
#include "linkedlistnode.h"
|
||||||
|
|
||||||
namespace Datastructures {
|
namespace Datastructures {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -9,14 +9,12 @@ 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;
|
||||||
@ -51,37 +49,7 @@ 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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,7 @@
|
|||||||
#include <linkedlist.h>
|
#include <LinkedList/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, 0);
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user