Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Starting at the beginning, if you do not set <code>front</code> and <code>rear</code> somewhere to NULL, then none of this works right.</p> <p>Then lets look at <code>push()</code>:</p> <p>When the list is empty, you set <code>front</code> and <code>rear</code> equal to <code>temp</code>. That's good. When there is a <code>front</code> you set <code>front-&gt;next= temp ;</code> This is not good if you already added something else in the list. The idea should be to use <code>rear</code> to always point to the last thing added to the list, and always add to the end of <code>rear</code>.</p> <p>So you should do:</p> <pre><code>void push(int value) { struct Node * temp= (struct Node *) malloc( sizeof( struct Node)) ; temp-&gt; Data= value ; temp-&gt; next= NULL ; fprintf(stderr, "Calling push with %d, temp at %lx.\n", value, temp) ; if ( rear) { rear-&gt; next= temp ; rear= temp ; } else { front= rear= temp ; } fprintf(stderr, "push done: front = %lx, rear = %lx\n", (long) front, (long) rear) ; } </code></pre> <p>Similarly, you have things a bit backwards on <code>pop()</code>. You look at <code>rear</code> and also check <code>rear-&gt;next</code>. <code>rear-&gt;next</code> should <strong>always</strong> be <code>NULL</code>. Instead, just take the first thing off of <code>front</code>. Worse than that though is that you look at the value of <code>rear</code> before even checking that its empty. This is going to do bad things. So test the pointer for being valid first, then read its value:</p> <pre><code>int pop() { int retval= -1 ; if ( front ) { struct Node * temp= front ; front= front-&gt; next ; if ( ! front ) { rear= front ; } retval= temp-&gt; Data ; free( temp) ; } return retval ; } </code></pre> <p>This should get you operating. }</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload