Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What gave you the wrong output in the first case was the fact that the pointer <code>Q-&gt;currentValue</code> never changed its value (the address which it was holding). It was always pointing to the first element in the queue.</p> <p>Say the queue contained <code>{1, 2, 3 |,&lt;garbage&gt;}</code>.</p> <p>That means, that after the first pop, the queue became:</p> <p><code>{2, 3 |, 3, &lt;garbage&gt;}</code></p> <p>and <code>currentValue</code> still held the address of the first element in the array, which is <code>2</code>.</p> <p>After the second pop:</p> <p><code>{3 |, 3, 3, &lt;garbage&gt;}</code></p> <p>and <code>currentValue</code> points to the first element, which value is <code>3</code>,</p> <p>The last time, the array is unchanged (as <code>Q-&gt;count--</code> changes <code>Q-&gt;count</code>'s value to 0), so the conent is </p> <p><code>{| 3, 3, 3, &lt;garbage&gt;}</code> and <code>currentValue</code> is still pointing to a <code>3</code>.</p> <p>I assume that you changed the second example to make <code>Queue-&gt;currentValue</code> an <code>int</code>.</p> <p>This way, it retains the original first element (which is popped).</p> <p>That makes your prints work properly in your test case.</p> <p><strong>However,</strong></p> <ul> <li>Your implementation will fail if you had a <code>0</code> in your queue.</li> <li>Your implementation adds needless complexity to the <code>pop</code> operation (O(n)). It would be much better to implement a cyclic queue, with <code>head</code> and <code>tail</code>.</li> <li>Retaining a copy of the popped element in order to return it would not be my first choice. I would suggest implementing an <code>isEmpty()</code> method, check its result in the <code>while</code> loop and while the queue is not empty, simply have <code>pop()</code> return the queue advance its <code>head</code> and return the previous head element.</li> </ul>
    singulars
    1. This table or related slice is empty.
    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