Implementing the List a little better, not performant but it works at least
This commit is contained in:
@@ -16,9 +16,8 @@ namespace Seele
|
|||||||
List()
|
List()
|
||||||
{
|
{
|
||||||
root = nullptr;
|
root = nullptr;
|
||||||
tail = new Node();
|
tail = nullptr;
|
||||||
tail->prev = nullptr;
|
beginIt = Iterator(root);
|
||||||
tail->next = nullptr;
|
|
||||||
endIt = Iterator(tail);
|
endIt = Iterator(tail);
|
||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
@@ -88,7 +87,7 @@ namespace Seele
|
|||||||
}
|
}
|
||||||
T& back()
|
T& back()
|
||||||
{
|
{
|
||||||
return tail->data;
|
return tail->prev->data;
|
||||||
}
|
}
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
@@ -96,40 +95,66 @@ namespace Seele
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Node* tmp = root;
|
for (Node* tmp = root; tmp != tail;)
|
||||||
while (tmp->next != nullptr)
|
|
||||||
{
|
{
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
delete tmp->prev;
|
delete tmp->prev;
|
||||||
}
|
}
|
||||||
|
delete tail;
|
||||||
|
tail = nullptr;
|
||||||
|
root = nullptr;
|
||||||
}
|
}
|
||||||
//Insert at the end
|
//Insert at the end
|
||||||
Iterator add(const T& value)
|
Iterator add(const T& value)
|
||||||
{
|
{
|
||||||
|
if (root == nullptr)
|
||||||
|
{
|
||||||
|
root = new Node();
|
||||||
|
tail = root;
|
||||||
|
}
|
||||||
tail->data = value;
|
tail->data = value;
|
||||||
Node* newTail = new Node();
|
Node* newTail = new Node();
|
||||||
newTail->prev = tail;
|
newTail->prev = tail;
|
||||||
newTail->next = nullptr;
|
newTail->next = nullptr;
|
||||||
|
tail->next = newTail;
|
||||||
Iterator insertedElement(tail);
|
Iterator insertedElement(tail);
|
||||||
tail = newTail;
|
tail = newTail;
|
||||||
|
refreshIterators();
|
||||||
|
size++;
|
||||||
return insertedElement;
|
return insertedElement;
|
||||||
}
|
}
|
||||||
|
Iterator remove(Iterator pos)
|
||||||
|
{
|
||||||
|
size--;
|
||||||
|
Node* prev = pos.node->prev;
|
||||||
|
Node* next = pos.node->next;
|
||||||
|
if (prev == nullptr)
|
||||||
|
{
|
||||||
|
root = next;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
prev->next = next;
|
||||||
|
}
|
||||||
|
next->prev = prev;
|
||||||
|
delete pos.node;
|
||||||
|
refreshIterators();
|
||||||
|
return Iterator(next);
|
||||||
|
}
|
||||||
Iterator insert(Iterator pos, const T& value)
|
Iterator insert(Iterator pos, const T& value)
|
||||||
{
|
{
|
||||||
size++;
|
size++;
|
||||||
if (empty())
|
if (root == nullptr)
|
||||||
{
|
{
|
||||||
root = new Node();
|
root = new Node();
|
||||||
root->data = value;
|
root->data = value;
|
||||||
root->prev = nullptr;
|
tail = new Node();
|
||||||
root->next = tail;
|
root->next = tail;
|
||||||
|
root->prev = nullptr;
|
||||||
tail->prev = root;
|
tail->prev = root;
|
||||||
|
tail->next = nullptr;
|
||||||
refreshIterators();
|
refreshIterators();
|
||||||
return Iterator(root);
|
return beginIt;
|
||||||
}
|
|
||||||
if (pos == endIt)
|
|
||||||
{
|
|
||||||
return add(value);
|
|
||||||
}
|
}
|
||||||
Node* tmp = pos.node->prev;
|
Node* tmp = pos.node->prev;
|
||||||
Node* newNode = new Node();
|
Node* newNode = new Node();
|
||||||
@@ -142,7 +167,7 @@ namespace Seele
|
|||||||
}
|
}
|
||||||
Iterator find(const T& value)
|
Iterator find(const T& value)
|
||||||
{
|
{
|
||||||
for (Node* i = root; root != tail; root = root->next)
|
for (Node* i = root; i != tail; i = i->next)
|
||||||
{
|
{
|
||||||
if (!(i->data < value) && !(value < i->data))
|
if (!(i->data < value) && !(value < i->data))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -32,5 +32,20 @@ BOOST_AUTO_TEST_CASE(basic_insert)
|
|||||||
}
|
}
|
||||||
BOOST_REQUIRE_EQUAL(list.length(), 4);
|
BOOST_REQUIRE_EQUAL(list.length(), 4);
|
||||||
}
|
}
|
||||||
|
BOOST_AUTO_TEST_CASE(basic_remove)
|
||||||
|
{
|
||||||
|
List<int> list;
|
||||||
|
list.add(2);
|
||||||
|
list.add(3);
|
||||||
|
list.add(4);
|
||||||
|
List<int>::Iterator it = list.find(3);
|
||||||
|
it = list.remove(it);
|
||||||
|
BOOST_REQUIRE_EQUAL(*it, 4);
|
||||||
|
for (auto i : list)
|
||||||
|
{
|
||||||
|
std::cout << i << std::endl;
|
||||||
|
}
|
||||||
|
BOOST_REQUIRE_EQUAL(list.length(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
Reference in New Issue
Block a user