Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>first of all, I would not try to put code like this one in production. Things can be done more simple, clear, performant and less error prone.</p> <p>I've "fixed" your program by changing things in as little as possible places. It must be clear that this doesn't make things more elegant. Only rethinking and rewriting would make things more elegant.</p> <p>The error you have (both in the first and the second program) is the routine popQueue.</p> <ol> <li><p>You return nothing in the else clause. You should "return NULL". This is at least sloppy programming.</p></li> <li><p>The routines return 1 2 3 4 5 5 and 1 2 2 for a queue. This is because Q->CurrentValue points to the first place in the theQueue array, and you shift up all values. This means that CurrentValue defacto points to the next value.</p></li> </ol> <p>The solution (again: it's not elegant, nor would I put it in production, but it is with minimal change to the original) to your problem is:</p> <ol> <li><p>Change in the struct (to hold the real CurrentValue)</p> <pre><code>struct queue { int* pointer; int currentValue; int max, count, theQueue[Q_MAX_SIZE]; }; </code></pre></li> <li><p>Change the routine popQueue</p> <pre><code>int* popQueue(struct queue *Q) { int i; if (Q-&gt;count &gt; 0) { Q-&gt;currentValue = *Q-&gt;theQueue; Q-&gt;pointer--; Q-&gt;count--; for (i = 0; i &lt; Q-&gt;count; i++) { int* currentPtr = Q-&gt;theQueue + i; int* nextPtr = currentPtr + 1; *currentPtr = *nextPtr; } return &amp;(Q-&gt;currentValue); } else return NULL; } </code></pre></li> </ol> <p>Kind regards, PB</p>
    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. This table or related slice is empty.
    1. 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