title: Confused on how to implement the end function for doubly linked list
body: <pre><code>template &lt;typename T&gt;
  class LinkedList {
    struct node;
    class Iterator;
  
   public:
    LinkedList() {}
    LinkedList(std::initializer_list&lt;T&gt; init_list) {
      this-&gt;operator=(init_list);
    }
    template &lt;typename InputIterator&gt;
    LinkedList(InputIterator first, InputIterator last) {
      for (; first != last; ++first)
        this-&gt;push_back(*first);
    }
    LinkedList(const LinkedList&amp; another) {
      this-&gt;operator=(another);
    }
    ~LinkedList() {
      while (this-&gt;head) {
        node* old_head = this-&gt;head;
        this-&gt;head = old_head-&gt;next;
        delete old_head;
      }
    }
    Iterator begin() {
      return Iterator(this-&gt;head);
    }
    Iterator end() {
      return Iterator(this-&gt;tail-&gt;next);
    }
  </code></pre>
  <p>I tried to add an empty node at the tail-&gt;next, however I can't get the result that I want. And without the empty node I just get a segmentation fault when I run the code.</p>
  <pre><code>  class Iterator {
     friend class LinkedList;
     public:
      using iterator_category = std::bidirectional_iterator_tag;
      using value_type = T;
      using difference_type = int;
      using pointer = T*;
      using reference = T&amp;;
      Iterator(node* ptr) : ptr(ptr) {}
      Iterator(const Iterator&amp; other) {
        this-&gt;operator=(other);
      }
  
      Iterator&amp; operator=(const Iterator&amp; that) {
        this-&gt;ptr = that.ptr;
        return *this;
      }
      Iterator&amp; operator++() {
        this-&gt;ptr = ptr-&gt;next;
        return *this;
      }
      Iterator operator++(int) {
        Iterator tmp(*this);
        this-&gt;operator++();
        return tmp;
      }
      Iterator&amp; operator--() {
        this-&gt;ptr = ptr-&gt;prev;
        return *this;
      }
      Iterator operator--(int) {
        Iterator tmp(*this);
        this-&gt;operator--();
        return tmp;
      }
      bool operator!=(Iterator that) const { return !(this-&gt;operator==(that)); }
      bool operator==(Iterator that) const { return this-&gt;ptr == that.ptr; }
      T&amp; operator*() const { return ptr-&gt;data; }
      Iterator* operator-&gt;() { return this; }
    private:
      node* ptr = nullptr;
    };
  </code></pre>
  <p>Here is the main function I used to test and when I print the stl_list end() it prints the size of the list. I'm confused what should be returned for this function. I thought it was supposed to be an empty nullptr that points to the location after the tail.</p>
  <pre><code>int main() {
    std::list&lt;int&gt; stl_list{1, 2, 3, 4, 5};
    cs19::LinkedList&lt;int&gt; our_list{1, 2, 3, 4, 5};
    std::cout &lt;&lt; *stl_list.begin() &lt;&lt; '\n';
    std::cout &lt;&lt; *our_list.begin() &lt;&lt; '\n';
    std::cout &lt;&lt; *our_list.end() &lt;&lt; '\n';
    std::cout &lt;&lt; *stl_list.end() &lt;&lt; '\n';
  }
  </code></pre>