title: Improper Insertion into Queue in C
body: <p>Hi I am trying to make a simple linked list priority queue where elements are ordered based on their f value. When I print the queue after inserting a few elements I notice that there is always just one element in the queue (the one recently inserted). It does not contain the other elements. I am not sure what I am doing wrong.</p>
  
  <pre><code>int insertPriorityQueue(struct queueNode* head, struct randomNode* e)
  {
  
  struct queueNode* newNode = (struct queueNode*)malloc(sizeof(struct queueNode));
  
  newNode-&gt;element = e;
  
  newNode-&gt;next = NULL;
  
  if(head==NULL) 
  {
      head = newNode;
  }
  
  else if(head-&gt;element-&gt;f &gt; e-&gt;f)    
  {
          newNode-&gt;next = head;
          head = newNode;
  }
  
  else
  {        
          struct queueNode* temp1 = head;
          struct queueNode* temp2 = NULL;
  
          while(temp1!= NULL)
          {
              if (temp1-&gt;element-&gt;f &gt; e-&gt;f)               
          {
                      newNode-&gt;next = temp1;
                      temp2-&gt;next = newNode;
              }
              temp2 = temp1;
              temp1 = temp1-&gt;next;
          }
  
          if(temp2==NULL) 
          {
          head-&gt;next = newNode;
      } 
  
          else if(temp1==NULL)
      {
          temp2-&gt;next = newNode;
      } 
  }
  
   printQueue(head);
   return 1;
  }
  </code></pre>
  
  <p>The function I use to print the queue is:</p>
  
  <pre><code>void printQueue(struct queueNode* head)
  {
      struct queueNode* temp = head;
      printf("\n Queue: ");
      while(temp!=NULL)
      {
      printf("[%0.2f]”,temp-&gt;element-&gt;f);
          temp = temp-&gt;next;
  }
  }
  </code></pre>