title: Sorted Doubly Linked Sentinel List merge function issue
body: <p>I am working on a templated data structure class which is a doubly linked list which uses sentinel nodes.
  The list is sorted based on insert() and I am working on a merge function wherein the nodes of each list must be combined into this-&gt;list. The nodes must be moved and not have new ones created. and if the same value exists in both lists the other node value must come after the current node value.</p>
  <p>I coded what I thought was a logical implementation however the output I get is not as expected and the results do not make sense to me, so if anyone could explain how i am getting the results I have it would be greatly appreciated.</p>
  <p>Class Definition</p>
  <pre><code>class SortedList {
      struct Node {
          T data_;
          Node* next_;
          Node* prev_;
          Node(const T&amp; data = T{}, Node* nx = nullptr, Node* pr = nullptr) {
              data_ = data;
              next_ = nx;
              prev_ = pr;
          }
      };
      Node* front_;
      Node* back_;
  
  public:
      class const_iterator {
          friend class SortedList;
          Node* current_;
          const_iterator(Node* n)
          {
              current_ = n;
          }
      public:
          const_iterator() {
              //Set to safe state         
              current_ = nullptr;
          }
          const_iterator&amp; operator++() {
              current_ = current_-&gt;next_;
              return *this;
          }
          const_iterator operator++(int) {
              const_iterator old = *this;
              current_ = current_-&gt;next_;
              return old;
          }
          const_iterator&amp; operator--() {
              current_ = current_-&gt;prev_;
              return *this;
          }
          const_iterator operator--(int) {
              const_iterator old = *this;
              current_ = current_-&gt;prev_;
              return old;
          }
          bool operator==(const_iterator rhs) {
              return (current_ == rhs.current_) ? true : false;
          }
          bool operator!=(const_iterator rhs) {
              return !(*this == rhs);
          }
          bool operator&gt;(const_iterator rhs) {
              return current_-&gt;data_ &gt; rhs-&gt;current_-&gt;data_;
          }
          const T&amp; operator*()const {
              return current_-&gt;data_;
          }
      };
      class iterator :public const_iterator {
          friend SortedList;
          iterator(Node* n) :const_iterator(n) {};
      public:
          iterator() : const_iterator() {};
          //prefix
          iterator&amp; operator++() {
              this-&gt;current_ = this-&gt;current_-&gt;next_;
              return *this;
          }
          //post-fix
          iterator operator++(int) {
              iterator old = *this;
              this-&gt;current_ = this-&gt;current_-&gt;next_;
              return old;
          }
          iterator&amp; operator--() {
              this-&gt;current_ = this-&gt;current_-&gt;prev_;
              return *this;
          }
          iterator operator--(int) {
              iterator old = *this;
              this-&gt;current_ = this-&gt;current_-&gt;prev_;
              return old;
          }
          T&amp; operator*() {
              return this-&gt;current_-&gt;data_;
          }
          const T&amp; operator*()const {
              return this-&gt;current_-&gt;data;
          }
      };
      SortedList();                                   //done
      ~SortedList();
      SortedList(const SortedList&amp; rhs);
      SortedList&amp; operator=(const SortedList&amp; rhs);
      SortedList(SortedList&amp;&amp; rhs);
      SortedList&amp; operator=(SortedList&amp;&amp; rhs);
      iterator begin() {
          return iterator(front_-&gt;next_);
      }
      iterator end() {
          return iterator(back_);
      }
      const_iterator cbegin() const {
          return const_iterator(front_-&gt;next_);
      }
      const_iterator cend() const {
          return const_iterator(back_);
      }
      iterator insert(const T&amp; data);
      iterator search(const T&amp; data);
      const_iterator search(const T&amp; data) const;
      iterator erase(iterator it);
      void merge(SortedList&amp; other);
      bool empty() const;
      int size() const;
  };
  
  </code></pre>
  <p>merge function</p>
  <pre><code>
  template &lt;typename T&gt;
  void SortedList&lt;T&gt;::merge(SortedList&amp; other) {
      
      iterator it = this-&gt;begin();
      iterator oit = other.begin();
      while (oit !=  other.end()) {               
          std::cout &lt;&lt; *oit &lt;&lt; &quot; &quot; &lt;&lt; *it &lt;&lt; std::endl;
          if (*oit &lt; *it) {
              oit.current_-&gt;prev_-&gt;next_ = oit.current_-&gt;next_;
              oit.current_-&gt;next_-&gt;prev_ = oit.current_-&gt;prev_;
              oit.current_-&gt;next_ = it.current_;
              oit.current_-&gt;prev_ = it.current_-&gt;prev_;
              it.current_-&gt;next_ = oit.current_;
          }
          else {
              oit.current_-&gt;prev_-&gt;next_ = oit.current_-&gt;next_;
              oit.current_-&gt;next_-&gt;prev_ = oit.current_-&gt;prev_;
              oit.current_-&gt;next_ = it.current_-&gt;next_;
              oit.current_-&gt;prev_ = it.current_;
              it.current_-&gt;prev_ = oit.current_;
          }
          oit++;
          it++;
      }
  }
  </code></pre>
  <p>main tester</p>
  <pre><code>int main() {
      int num[] = { 3,5,1,2,6,8,9,11 };
      int num2[] = { 1,5,4,6,12,7,8,9 };
  
      SortedList&lt;int&gt; l;
      SortedList&lt;int&gt; l2;
      for (int i = 0; i &lt; 8; i++)
      {
          l.insert(num[i]);
          l2.insert(num2[i]);
      }
       SortedList&lt;int&gt;::iterator result;
       SortedList&lt;int&gt;::iterator result2 = l2.begin();
       result = l.begin();
       while (result != l.end()) {
           std::cout &lt;&lt; *result &lt;&lt; &quot;    &quot; &lt;&lt; *result2 &lt;&lt; std::endl;
           ++result;
           ++result2;
       }
       l.merge(l2);
  </code></pre>
  <p>output</p>
  <pre><code>1    1
  2    4
  3    5
  5    6
  6    7
  8    8
  9    9
  11    12
  
  1 1
  2 2
  3 3
  5 5
  6 6
  8 8
  9 9
  11 11
  0 0
  
  </code></pre>
  <p>I dont understand why my second output is showing same the same values for *it and *oit I am pretty certain the error is in how I assign the the oit.current_-&gt;next &amp; prev but i am unsure.</p>
  <p>any insight is appriciated.</p>