From 7d6cfaa7f2ca12a2fd75bc9134b166c94e3a7a3c Mon Sep 17 00:00:00 2001
From: Eric Elfving <eric.elfving@liu.se>
Date: Thu, 23 Feb 2017 11:30:41 +0100
Subject: [PATCH] Changes to account for sentinel

---
 .gitignore |  2 +-
 List.cc    | 65 +++++++++++++++++++++++++++++++++---------------------
 List.h     |  8 ++++++-
 3 files changed, 48 insertions(+), 27 deletions(-)

diff --git a/.gitignore b/.gitignore
index 5facd34..9039f68 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,4 @@ a.out
 #*#
 *.swp
 *.gch
-
+*.o
diff --git a/List.cc b/List.cc
index db73e00..be4f22d 100644
--- a/List.cc
+++ b/List.cc
@@ -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
diff --git a/List.h b/List.h
index c3fb354..21705b8 100644
--- a/List.h
+++ b/List.h
@@ -1,9 +1,12 @@
+#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
-- 
GitLab