title: Reversing a linked list - Iterative
body: <p>Need to reverse the list and then print it out, as of right now it prints the list in the same order, I've been searching everywhere, but I'm still stuck. I don't even know if it is reversing properly. Any help is greatly appreciated.</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;cassert&gt;
  #include &lt;list&gt;
  
  using namespace std;
  
  class List;
  class Iterator;
  
  class Node{
  public:
     /** 
        Constructs a node with a given data value.
        @param s the data to store in this node
     */
     Node(string s);
  private:
     string data;
     Node* previous;
     Node* next;
  friend class List;
  friend class Iterator;
  };
  
  class List{
  public:
     /**
        Constructs an empty list;
     */
     List();
     /**
        Appends an element to the list.
        @param data the value to append
     */
     void push_back(string data);
     /**
        Inserts an element into the list.
        @param iter the position before which to insert
        @param s the value to append
     */
     void insert(Iterator iter, string s);
     /**
        Reverses the order of the elements in the list.
     */
     void reverse(Node *&amp;head);
     /**
        Removes an element from the list.
        @param iter the position to remove
        @return an iterator pointing to the element after the
        erased element
     */
     Iterator erase(Iterator iter);
     /**
        Gets the beginning position of the list.
        @return an iterator pointing to the beginning of the list
     */
     Iterator begin();
     /**
        Gets the past-the-end position of the list.
        @return an iterator pointing past the end of the list
     */
     Iterator end();
  private:
     Node* first;
     Node* last;
  friend class Iterator;
  };
  
  class Iterator{
  public:
     /**
        Constructs an iterator that does not point into any list.
     */
     Iterator();
     /**  
        Looks up the value at a position.
        @return the value of the node to which the iterator points
     */
     string get() const;
     /**
        Advances the iterator to the next node.
     */
     void next();
     /**
        Moves the iterator to the previous node.
     */
     void previous();
     /**
        Compares two iterators
        @param b the iterator to compare with this iterator
        @return true if this iterator and b are equal
     */
     bool equals(Iterator b) const;
  private:
     Node* position;
     List* container;
  friend class List;
  };
  
  Node::Node(string s){  
     data = s;
     previous = NULL;
     next = NULL;
  }
  
  List::List(){  
     first = NULL;
     last = NULL;
  }
  
  void List::push_back(string data){  
     Node* new_node = new Node(data);
     if (last == NULL) // List is empty
     {  
        first = new_node;
        last = new_node;
     }
     else{  
        new_node-&gt;previous = last;
        last-&gt;next = new_node;
        last = new_node;
     }
  }
  
  void List::insert(Iterator iter, string s){  
     if (iter.position == NULL)   {  
        push_back(s);
        return;
     }
  
     Node* after = iter.position;
     Node* before = after-&gt;previous;
     Node* new_node = new Node(s);
     new_node-&gt;previous = before;
     new_node-&gt;next = after;
     after-&gt;previous = new_node;
     if (before == NULL) // Insert at beginning
        first = new_node;
     else
        before-&gt;next = new_node;
  }
  
  void List::reverse(Node *&amp;head){
      if((head == NULL)||(head-&gt;next=NULL))
          return;
      Node *top = head;
      Node *tail = NULL;
      Node *curr = head;
      Node *temp = NULL;
  
      while(curr != NULL){
          temp = curr-&gt;next;
          curr-&gt;next = curr-&gt;previous;
          curr-&gt;previous = temp;
          if(temp == NULL){
              tail = curr;
          }
          curr = temp;
      } 
      temp = head;
      head = tail;
      tail = temp;
  }
  
  Iterator List::erase(Iterator iter){  
     assert(iter.position != NULL);
     Node* remove = iter.position;
     Node* before = remove-&gt;previous;
     Node* after = remove-&gt;next;
     if (remove == first)
        first = after;
     else
        before-&gt;next = after;
     if (remove == last)
        last = before;
     else
        after-&gt;previous = before;
     delete remove;
     Iterator r;
     r.position = after;
     r.container = this;
     return r;
  }
  
  Iterator List::begin(){  
     Iterator iter;
     iter.position = first;
     iter.container = this;
     return iter;
  }
  
  Iterator List::end(){  
     Iterator iter;
     iter.position = NULL;
     iter.container = this;
     return iter;
  }
  
  Iterator::Iterator(){  
     position = NULL;
     container = NULL;
  }
  
  string Iterator::get() const{  
     assert(position != NULL);
     return position-&gt;data;
  }
  
  void Iterator::next(){  
     assert(position != NULL);
     position = position-&gt;next;
  }
  
  void Iterator::previous(){  
     assert(position != container-&gt;first);
     if (position == NULL)
        position = container-&gt;last;
     else 
        position = position-&gt;previous;
  }
  
  bool Iterator::equals(Iterator b) const{  
     return position == b.position;
  }
  
  int main(){
  
     List staff;
  
     // Add orginal values to staff
     staff.push_back("Tom");
     staff.push_back("Dick");
     staff.push_back("Harry");
     staff.push_back("Juliet");
  
     Iterator pos;
  
     // Print all values
     for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
        cout &lt;&lt; pos.get() &lt;&lt; "\n";
  
     // Reverse the values
      Node* head = NULL;
      staff.reverse(head);
  
     // Print all values to show reverse
     cout &lt;&lt; endl;
     for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
        cout &lt;&lt; pos.get() &lt;&lt; "\n";
  
     return 0;
  }
  </code></pre>