title: C++ Priority Queue Class with Linked List
body: <p>I am having two issues with my c++ code (The test file is below): </p>
  
  <p>I can't seem to figure out why its not breaking out of the while loop, when running, its stuck on the loop "7 versus 325".</p>
  
  <p>So it should go into the next node of temp, which would be null, and then jump into the section where it adds it to the end of the queue.  But its just looping and looping.  </p>
  
  <p>My second issue is with the the function I have commented out, queue1.back, whenever that is ran, it just errors out and gives what appears to be the address, but the .front() function works just fine.  </p>
  
  <p>The Test File I am working with is like this:</p>
  
  <p><code>89 Alex</code></p>
  
  <p><code>325 Rob</code></p>
  
  <p><code>72 Joy</code></p>
  
  <p><code>91 Bob</code></p>
  
  <pre><code>using namespace std;
  
  class Person
  {
      friend class Pqueue;
      public:
          int priority;
          string name;
      };
  
  
      class PQueue
      {
          friend class Person;
  
          private:
  
          //Structure for my linked list.
          typedef struct node {
              Person data;
              struct node *next;
          }Node, *NodePtr;
  
          Node *head, *tail;
  
          public:
          //Prototype Functions
          PQueue(void);              //Initializer function
          bool empty(void);          //Test if empty
          int size(void);            //Return size
          void enqueue(Person *);    //Insert Node
          void dequeue(void);        //Remove Node
          Person* front(void);       //Access Next Node
          Person* back(void);        //Access last node
  
      };
  
  
  
      PQueue::PQueue()
     {
         head = NULL;
         tail = NULL;
     }
  
  
  
      bool PQueue::empty(){
          return (head == NULL);
      }
  
  
  
      void PQueue::enqueue(Person *myPerson){
          NodePtr np = (NodePtr) malloc(sizeof(Node));
          np-&gt;data = *myPerson;
          np-&gt;next = NULL;
  
          if(empty())
          {
               cout &lt;&lt; "Making into creating the first node, of the linked list" &lt;&lt;endl;
               head = np;
               tail = np;
           }
           else { //Queue has more the one node
                 Node* temp = head;
                 if(np-&gt;data.priority &gt; temp-&gt;data.priority) //If the priority is greater then the rest.
                 {
                     head = temp;                            //Saving my head pointer
                     head-&gt;data = np-&gt;data;                  //Assigning new Data to the head pointer
                     head-&gt;next = temp;                      //Assigning the rest of the linked list back into head.
                     cout &lt;&lt; "Making into creating the first node again, having to reassign." &lt;&lt;endl;
                 }
                 else{
                      //Searching where to place the node.
                      while(temp-&gt;data.priority &gt; np-&gt;data.priority) //Searching if the next priority is higher then the passed.
                      {
                          cout &lt;&lt; "Inside the while loop: " &lt;&lt; np-&gt;data.priority &lt;&lt; " versus "&lt;&lt;temp-&gt;data.priority &lt;&lt;endl;
                          if(temp-&gt;next == NULL)
                              break;
                          temp = temp-&gt;next;
                       }
  
              if(temp-&gt;next == NULL &amp;&amp; np-&gt;data.priority &lt; temp-&gt;data.priority) //Inserting at the end.
              {
                  cout &lt;&lt; "Making into creating the last node" &lt;&lt;endl;
                  tail-&gt;next = np;
                  cout &lt;&lt; "Passing the function of creating the last node" &lt;&lt;endl;
              }
              else   //Inserting into the middle of the function.
              {
                  cout &lt;&lt; "Inserting in the middle of the queue" &lt;&lt;endl;
                  np-&gt;next = temp-&gt;next;
                  temp-&gt;next = np;
              }
          }
      }
  }
  
  
  
  void PQueue::dequeue(){
      if(empty()){
          cout &lt;&lt; "\nAttempt to remove from an empty list." &lt;&lt; endl;
          exit(1);
      }
  
      Person hold = head-&gt;data;
      NodePtr temp = head;
      head=head-&gt;next;
      if (head == NULL) tail = NULL;
      free(temp);
  }
  
  Person* PQueue::front(){
      //Person &amp;temp = head-&gt;next-&gt;data;
      //Person &amp;temp = head-&gt;data;
      Person &amp;temp = head-&gt;data;
      return &amp;temp;
  }
  
  Person* PQueue::back(){
      if(empty()){
          cout &lt;&lt; "\nNo entries in list." &lt;&lt; endl;
          exit(1);
      }
      Person &amp;temp = tail-&gt;data;
      return &amp;temp;
  }
  
  int main() {
      cout &lt;&lt; "Starting main" &lt;&lt; endl;
      PQueue queue1; //Creating my queue.
      cout &lt;&lt; "Created Queue" &lt;&lt; endl;
      Person tempPerson;
      ifstream inFile;
      inFile.open("/tmp/temp");
      cout &lt;&lt; "going into while loop" &lt;&lt; endl;
  
      while (inFile &gt;&gt; tempPerson.priority &gt;&gt; tempPerson.name){
          cout &lt;&lt; "The priority is " &lt;&lt;  tempPerson.priority &lt;&lt; " the name is " &lt;&lt; tempPerson.name &lt;&lt;endl;
          queue1.enqueue(&amp;tempPerson);
      }
  
  
      //Testing Section, trying to get .front and .back to work.
      Person *testPerson;
      testPerson = queue1.front();
      cout &lt;&lt; "The TEST priority is " &lt;&lt;  testPerson-&gt;priority &lt;&lt; " the TEST name is " &lt;&lt; testPerson-&gt;name &lt;&lt;endl;
      /**
      Person *tailPerson;
      testPerson = queue1.back();
      cout &lt;&lt; "The TEST priority is " &lt;&lt;  tailPerson-&gt;priority &lt;&lt; " the TEST  name is " &lt;&lt; tailPerson-&gt;name &lt;&lt;endl;
      **/
  
      queue1.dequeue();
      queue1.dequeue();
      queue1.dequeue();
  
  
      return 0;
  }
  </code></pre>