title: Implementation of Queues in Linked Lists
body: <p>I am given these structure declarations in order to implement a queue collection that uses a circular linked list.</p>
  
  <pre><code>typedef struct intnode {
      int value;
      struct intnode *next;
  } intnode_t;
  
  typedef struct {
      intnode_t *rear;   // Points to the node at the tail of the 
                         // queue's linked list
      int size;          // The # of nodes in the queue's linked list
  } intqueue_t;
  
  intnode_t *intnode_construct(int value, intnode_t *next)
  {
      intnode_t *p = malloc(sizeof(intnode_t));
      assert (p != NULL);
      p-&gt;value = value;
      p-&gt;next = next;
      return p;
  }
  
  
  /* Return a pointer to a new, empty queue.
   * Terminate (via assert) if memory for the queue cannot be allocated.
   */
  
  intqueue_t *intqueue_construct(void)
  {
      intqueue_t *queue = malloc(sizeof(intqueue_t));
      assert(queue != NULL);
  
      queue-&gt;rear = NULL;
      queue-&gt;size = 0;
      return queue;
  }
  </code></pre>
  
  <p>I'm trying to create a function that will enqueue at a specified value (append it to the rear of the queue), and I need to consider the two cases in which the queue is empty and when the queue has one or more elements. This is the code I have so far:</p>
  
  <pre><code>void intqueue_enqueue(intqueue_t *queue, int value)
  {
  
      intnode_t *p = intnode_construct(value, NULL);
  
      if(queue-&gt;rear-&gt;next == NULL) {
          //the queue is empty
          queue-&gt;rear-&gt;next =p;
      } else {
          //the queue is not empty
          queue-&gt;rear=p;
      }
      queue-&gt;rear=p;
      queue-&gt;size++;
  }
  </code></pre>
  
  <p>This code gives me a runtime error so I'm not sure whats wrong. In the code, I'm assuming queue->rear->next is the front, however I think this is where the problem might be. All help is greatly appreciated. Thanks!</p>