title: Traversing queue in C++
body: <p>I have a C++ queue that I need to print. It's easy to print the first node and then delete it, then print the first node again which would then be the second node. But that would erase the entire list just to print it once... As a work around I created a temporary queue object which I passed to my print method and did the same thing as the first object, this would have been great except it's using pointers to make the queue dynamic, so deleting them from any object copied from the first is still deleting the same data. I'm not good with pointers yet but I'm sure there must be an easy way to do this, any suggestions? </p>
  
  <p>Here's the code:</p>
  
  <pre><code>queue2 = queue1; // Temporary queue is assigned values of main queue
  queue2.printQueue(); // Temporary queue is passed to print method
  </code></pre>
  
  <p>Here's my print method:</p>
  
  <pre><code>int numberPrinted = 0;
  while (!isEmptyQueue())
  {
   cout &lt;&lt; numberPrinted + 1 &lt;&lt; ": " &lt;&lt; front() &lt;&lt; "\n";
   deleteQueue();    
   numberPrinted++;
   }
  </code></pre>
  
  <p>Queue class file:</p>
  
  <pre><code>#ifndef H_linkedQueue
  #define H_linkedQueue
  
  #include &lt;iostream&gt;
  #include &lt;cassert&gt;
  #include "queueADT.h"
  
  using namespace std;
  
  //Definition of the node
  template &lt;class Type&gt;
  struct nodeType
  {
      Type info;
      nodeType&lt;Type&gt; *link;
  };
  
  
  template &lt;class Type&gt;
  class linkedQueueType: public queueADT&lt;Type&gt;
  {
  public:
      bool operator==
                      (const linkedQueueType&lt;Type&gt;&amp; otherQueue); 
  
      bool isEmptyQueue() const;
        //Function to determine whether the queue is empty. 
        //Postcondition: Returns true if the queue is empty,
        //               otherwise returns false.
  
      bool isFullQueue() const;
        //Function to determine whether the queue is full. 
        //Postcondition: Returns true if the queue is full,
        //               otherwise returns false.
  
      void initializeQueue();
        //Function to initialize the queue to an empty state.
        //Postcondition: queueFront = NULL; queueRear = NULL
  
      Type front() const;
        //Function to return the first element of the queue.
        //Precondition: The queue exists and is not empty.
        //Postcondition: If the queue is empty, the program 
        //               terminates; otherwise, the first 
        //               element of the queue is returned. 
  
      Type back() const;
        //Function to return the last element of the queue.
        //Precondition: The queue exists and is not empty.
        //Postcondition: If the queue is empty, the program 
        //               terminates; otherwise, the last 
        //               element of the queue is returned.
  
      void addQueue(const Type&amp; queueElement);
        //Function to add queueElement to the queue.
        //Precondition: The queue exists and is not full.
        //Postcondition: The queue is changed and queueElement
        //               is added to the queue.
  
      void deleteQueue();
        //Function  to remove the first element of the queue.
        //Precondition: The queue exists and is not empty.
        //Postcondition: The queue is changed and the first 
        //               element is removed from the queue.
      int numberOfNodes();
      // Return number of nodes in the queue.
      void printQueue();
      //Print the queue.
  
      linkedQueueType(); 
        //Default constructor
  
      linkedQueueType(const linkedQueueType&lt;Type&gt;&amp; otherQueue); 
        //Copy constructor
  
      ~linkedQueueType(); 
        //Destructor
  
  private:
      nodeType&lt;Type&gt; *queueFront; //pointer to the front of 
                                  //the queue
      nodeType&lt;Type&gt; *queueRear;  //pointer to the rear of 
                                  //the queue
      int count;
  };
  
      //Default constructor
  template&lt;class Type&gt;
  linkedQueueType&lt;Type&gt;::linkedQueueType() 
  {
      queueFront = NULL; //set front to null
      queueRear = NULL;  //set rear to null
  } //end default constructor
  
  template&lt;class Type&gt;
  bool linkedQueueType&lt;Type&gt;::isEmptyQueue() const
  {
      return(queueFront == NULL);
  } //end 
  
  template&lt;class Type&gt;
  bool linkedQueueType&lt;Type&gt;::isFullQueue() const
  {
      return false;
  } //end isFullQueue
  
  template &lt;class Type&gt;
  void linkedQueueType&lt;Type&gt;::initializeQueue()
  {
      nodeType&lt;Type&gt; *temp;
  
      while (queueFront!= NULL)  //while there are elements left
                                 //in the queue
      {
          temp = queueFront;  //set temp to point to the 
                              //current node
          queueFront = queueFront-&gt;link;  //advance first to  
                                          //the next node
          delete temp;    //deallocate memory occupied by temp
      }
  
      queueRear = NULL;  //set rear to NULL
  } //end initializeQueue
  
  
  template &lt;class Type&gt;
  void linkedQueueType&lt;Type&gt;::addQueue(const Type&amp; newElement)
  {
      nodeType&lt;Type&gt; *newNode;
  
      newNode = new nodeType&lt;Type&gt;;   //create the node
  
      newNode-&gt;info = newElement; //store the info
      newNode-&gt;link = NULL;  //initialize the link field to NULL
  
      if (queueFront == NULL) //if initially the queue is empty
      {
          queueFront = newNode;
          queueRear = newNode;
      }
      else        //add newNode at the end
      {
          queueRear-&gt;link = newNode;
          queueRear = queueRear-&gt;link;
      }
      count++;
  }//end addQueue
  
  template &lt;class Type&gt;
  Type linkedQueueType&lt;Type&gt;::front() const
  {
      assert(queueFront != NULL);
      return queueFront-&gt;info; 
  } //end front
  
  template &lt;class Type&gt;
  Type linkedQueueType&lt;Type&gt;::back() const
  {
      assert(queueRear!= NULL);
      return queueRear-&gt;info;
  } //end back
  
  template &lt;class Type&gt;
  void linkedQueueType&lt;Type&gt;::deleteQueue()
  {
      nodeType&lt;Type&gt; *temp;
  
      if (!isEmptyQueue())
      {
          temp = queueFront;  //make temp point to the 
                              //first node
          queueFront = queueFront-&gt;link; //advance queueFront 
  
          delete temp;    //delete the first node
  
          if (queueFront == NULL) //if after deletion the 
                                  //queue is empty
              queueRear = NULL;   //set queueRear to NULL
          count--;
      }
      else
          cout &lt;&lt; "Cannot remove from an empty queue" &lt;&lt; endl;
  }//end deleteQueue
  
  
      //Destructor
  template &lt;class Type&gt;
  linkedQueueType&lt;Type&gt;::~linkedQueueType() 
  {
      //Write the definition of the destructor
  } //end destructor
  
  template &lt;class Type&gt; 
  bool linkedQueueType&lt;Type&gt;::operator==
                      (const linkedQueueType&lt;Type&gt;&amp; otherQueue)
  {
      bool same = false;
  
      if (count == otherQueue.count)
          same = true;
  
      return same;
  
  } //end assignment operator
  
      //copy constructor
  template &lt;class Type&gt;
  linkedQueueType&lt;Type&gt;::linkedQueueType
                   (const linkedQueueType&lt;Type&gt;&amp; otherQueue) 
  {
      //Write the definition of the copy constructor
  }//end copy constructor
  template &lt;class Type&gt; 
  int linkedQueueType&lt;Type&gt;::numberOfNodes()
  {
      return count;
  
  } 
  
  template &lt;class Type&gt; 
  void linkedQueueType&lt;Type&gt;::printQueue()
  {
      int numberPrinted = 0;
  while (!isEmptyQueue())
  {
   cout &lt;&lt; numberPrinted + 1 &lt;&lt; ": " &lt;&lt; front() &lt;&lt; "\n";
   deleteQueue();    
   numberPrinted++;
   }
  }
  #endif
  </code></pre>