Skip to content
Snippets Groups Projects
Commit 7d6cfaa7 authored by Eric Elfving's avatar Eric Elfving
Browse files

Changes to account for sentinel

parent 09c3f6fa
No related branches found
No related tags found
No related merge requests found
......@@ -3,4 +3,4 @@ a.out
#*#
*.swp
*.gch
*.o
......@@ -3,64 +3,79 @@
#include <utility>
#include <stdexcept>
struct List::Node
struct List::Node
{
Node(int v, Node* p = nullptr, Node* n = nullptr)
: value{v}, prev{p}, next{n} {}
Node() = default;
Node(int v, Node* p, Node* n)
: value{v}, prev{p}, next{n} {}
int value {};
Node * prev {};
Node * next {};
};
List::List()
: head{ new Node{} }, tail{head}, sz{}
{}
List::List(List const & other)
: sz{other.sz}
{
if ( other.size() == 0 )
return;
Node * tmp {other.head};
head = new Node{other.head->value};
tail = head;
while ( tmp != nullptr )
: List{}
{
Node * tmp = other.head;
while ( tmp != other.tail )
{
tail->next = new Node{tmp->value, tail};
tail = tail->next;
push_back(tmp->value);
tmp = tmp->next;
}
}
List::List(List && tmp) noexcept
:List{}
{
swap(tmp);
}
List::List(std::initializer_list<int> lst)
: head{new Node{*lst.begin()}}, tail{head}, sz{1}
: List{}
{
for ( auto it = std::next(lst.begin()); it != end(lst); ++it )
for ( auto val : lst )
{
tail->next = new Node{*it, tail};
tail = tail->next;
++sz;
push_back(val);
}
}
void List::push_front(int value)
{
head->prev = new Node{value, nullptr, head};
head = head->prev;
head = new Node{value, nullptr, head};
if ( sz == 0 )
{
tail->prev = head;
}
++sz;
}
void List::push_back(int value)
{
tail->next = new Node{value, tail};
tail = tail->next;
++sz;
if ( empty() )
{
push_front(value);
}
else
{
tail->prev->next = new Node{value, tail->prev, tail};
tail->prev = tail->prev->next;
++sz;
}
}
bool List::empty() const
{
return head == tail;
}
int List::back() const
{
return tail->value;
return tail->prev->value;
}
int & List::back()
{
return tail->value;
return tail->prev->value;
}
int List::front() const
......
#ifndef LIST_H
#define LIST_H
#include <initializer_list>
class List
{
public:
List() = default;
List();
List(List const &);
List(List &&) noexcept;
List(std::initializer_list<int>);
......@@ -24,6 +27,7 @@ public:
int const & at(int idx) const;
int size() const;
bool empty() const;
void swap(List & other) noexcept;
private:
......@@ -32,3 +36,5 @@ private:
Node * tail {};
int sz {};
};
#endif //LIST_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment