Linkedlist bugfixes

This commit is contained in:
2019-05-01 21:29:51 +02:00
parent 64fa2f30ab
commit e1138917b4

View File

@@ -27,33 +27,35 @@ protected:
public: public:
void Append(T const value) void Append(T const value)
{ {
++size; if(size == 0)
if(tailPtr == nullptr)
{ {
root = std::make_unique<Node<T>>(value); root = std::make_unique<Node<T>>(value);
tailPtr = root.get(); tailPtr = root.get();
++size;
return; return;
} }
tailPtr->next = std::make_unique<Node<T>>(value); tailPtr->next = std::make_unique<Node<T>>(value);
tailPtr = tailPtr->next.get(); tailPtr = tailPtr->next.get();
++size;
} }
void Prepend(T const value) void Prepend(T const value)
{ {
++size; if(size == 0)
if(root == nullptr)
{ {
root = std::make_unique<Node<T>>(value); root = std::make_unique<Node<T>>(value);
tailPtr = root.get(); tailPtr = root.get();
++size;
return; return;
} }
auto newRoot = std::make_unique<Node<T>>(value); auto newRoot = std::make_unique<Node<T>>(value);
newRoot->next.swap(root); newRoot->next.swap(root);
root.swap(newRoot); root.swap(newRoot);
++size;
} }
void Insert(T const value, std::size_t const index) void Insert(T const value, std::size_t const index)
@@ -69,12 +71,6 @@ public:
return; return;
} }
if(index == size - 1u)
{
Append(value);
return;
}
++size; ++size;
Node<T> * prevPtr = root.get(); Node<T> * prevPtr = root.get();
@@ -83,7 +79,8 @@ public:
while(currentIndex < index) while(currentIndex < index)
{ {
prevPtr = curPtr; prevPtr = curPtr;
curPtr = curPtr->next().get(); curPtr = curPtr->next.get();
++currentIndex;
} }
auto newNode = std::make_unique<Node<T>>(value); auto newNode = std::make_unique<Node<T>>(value);
@@ -102,33 +99,43 @@ public:
if(index == 0u) if(index == 0u)
{ {
root.swap(root->next); if(size == 0u)
{
root.release();
tailPtr = nullptr;
}
else
{
root = std::move(root->next);
}
return; return;
} }
if(index == size - 1u) // Is index last element? Note that we subtracted 1 from size above
if(index == size)
{ {
Node<T> * curPtr = root.get(); Node<T> * curPtr = root.get();
std::size_t currentIndex = 0u; while(curPtr->next.get() != tailPtr)
while(currentIndex < index - 2u)
{ {
curPtr = curPtr->next().get(); curPtr = curPtr->next.get();
} }
curPtr->next.release(); tailPtr = curPtr;
tailPtr->next.release();
return; return;
} }
Node<T> * prevPtr = root.get(); Node<T> * prevPtr = root.get();
Node<T> * curPtr = root->next.get(); Node<T> * curPtr = prevPtr->next.get();
std::size_t currentIndex = 1u; std::size_t currentIndex = 1u;
while(currentIndex < index) while(currentIndex < index)
{ {
++currentIndex;
prevPtr = curPtr; prevPtr = curPtr;
curPtr = curPtr->next().get(); curPtr = curPtr->next.get();
} }
prevPtr->next.swap(curPtr->next); prevPtr->next = std::move(curPtr->next);
} }
T & Front() T & Front()
@@ -159,18 +166,25 @@ public:
} }
Node<T> * currentNode = root.get(); Node<T> * currentNode = root.get();
std:size_t currentIndex = 0u; std::size_t currentIndex = 0u;
while(currentIndex < index) while(currentIndex < index)
{ {
currentNode = currentNode->next.get(); currentNode = currentNode->next.get();
++currentIndex;
} }
return currentNode->value; return currentNode->value;
} }
std::size_t GetSize() const
{
return size;
}
List() List()
: root(nullptr), : root(nullptr),
tailPtr(nullptr) tailPtr(nullptr),
size(0)
{} {}
}; };
} }