Note that there are some explanatory texts on larger screens.

plurals
  1. POissue in making Queue data structure as array implementation
    text
    copied!<p>this code to make simple queue data structure as array implementation</p> <pre><code>#include &lt;stdio.h&gt; #define Q_MAX_SIZE 255 #include &lt;stdbool.h&gt; struct queue { int* pointer; int* currentValue; int max, count, theQueue[Q_MAX_SIZE]; }; void initQueue(struct queue*); bool pushQueue(struct queue*, int); int* popQueue(struct queue*); int main(void) { int i, j, num = 0; struct queue obj[5]; for(i=0; i&lt;5; i++) { initQueue(&amp;obj[i]); for(j = 0; j&lt;3; j++) { num++; pushQueue(&amp;obj[i], num); } num = 0; } for(i=0; i&lt;5; i++) { printf("Queue[%d]:\n", i); int* inputobj; inputobj = popQueue(&amp;obj[i]); while(inputobj != NULL) { printf("Queue[No.%d] = %d\n", i, *inputobj); inputobj = popQueue(&amp;obj[i]); } putchar('\n'); } puts("done..!"); return 0; } //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ void initQueue(struct queue *Q) { Q-&gt;pointer = Q-&gt;theQueue; Q-&gt;max = Q_MAX_SIZE; Q-&gt;count = 0; } bool pushQueue(struct queue *Q, int input) { if(Q-&gt;count &lt; Q-&gt;max) { *Q-&gt;pointer = input; Q-&gt;pointer++; Q-&gt;count++; return 1; } else return 0; } //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 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 Q-&gt;currentValue; } else NULL; } </code></pre> <p>there is a problem with the code in the function <code>popQueue()</code> in this line: </p> <p><code>Q-&gt;currentValue = Q-&gt;theQueue;</code></p> <p>it is work put the output is not correct</p> <pre><code>output: Queue[0]: Queue[No.0] = 2 Queue[No.0] = 3 Queue[No.0] = 3 Queue[1]: Queue[No.1] = 2 Queue[No.1] = 3 Queue[No.1] = 3 Queue[2]: Queue[No.2] = 2 Queue[No.2] = 3 Queue[No.2] = 3 Queue[3]: Queue[No.3] = 2 Queue[No.3] = 3 Queue[No.3] = 3 Queue[4]: Queue[No.4] = 2 Queue[No.4] = 3 Queue[No.4] = 3 done..! </code></pre> <p>but after i change the pointer (<code>currentValue</code>) in the <code>queue</code> struct to make it of type integer and edit some lines in the function <code>popQueue()</code> every thing work fine.</p> <p><strong>--here is the function after editing:</strong></p> <pre><code>int* popQueue(struct queue *Q) { int i; if(Q-&gt;count &gt; 0) { Q-&gt;currentValue = Q-&gt;theQueue[0]; 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; } </code></pre> <p>-- and this is the correct output:</p> <pre><code>Queue[0]: Queue[No.0] = 1 Queue[No.0] = 2 Queue[No.0] = 3 Queue[1]: Queue[No.1] = 1 Queue[No.1] = 2 Queue[No.1] = 3 Queue[2]: Queue[No.2] = 1 Queue[No.2] = 2 Queue[No.2] = 3 Queue[3]: Queue[No.3] = 1 Queue[No.3] = 2 Queue[No.3] = 3 Queue[4]: Queue[No.4] = 1 Queue[No.4] = 2 Queue[No.4] = 3 </code></pre> <h2>The Question is: what makes the first code provide wrong output?</h2>
 

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