Note that there are some explanatory texts on larger screens.

plurals
  1. POsingle Queue and multiple Queue using same program not work
    text
    copied!<p>here is two c program to implement queue data structure in simple form</p> <ul> <li><p><em>the first:</em></p> <p>define one queue and it's work perfectly</p></li> <li><p>the second:</p> <p>define multiple queues and it's crash at execution</p></li> </ul> <p>functions are the same in both programs except the <code>main()</code> were the implementation are different little bit.</p> <p>So the question here: why the second code not working?</p> <p><strong>* here is the codes *</strong></p> <p><em>code 1:</em></p> <pre><code>/* Single queue -- this work perfectly */ #include &lt;stdio.h&gt; #define Q_MAX_SIZE 255 struct queue { int* pointer; int* currentValue; int max, count, theQueue[Q_MAX_SIZE]; }; //prototyps void initQueue(struct queue*); unsigned short pushQueue(struct queue*, int); int* popQueue(struct queue*); int main(void) { int j; struct queue q; initQueue(&amp;q); for (j = 0; j &lt; 6; j++) pushQueue(&amp;q, j); int* inputobj = popQueue(&amp;q); while (inputobj != NULL) { printf("%d ", *inputobj); inputobj = popQueue(&amp;q); } printf("\n\ndone..Queue is empty\n"); return 0; } //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ void initQueue(struct queue *Q) { Q-&gt;pointer = Q-&gt;theQueue; Q-&gt;max = Q_MAX_SIZE; Q-&gt;count = 0; } unsigned short 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><em>code 2:</em></p> <pre><code>/* Multiple queues -- this not work and crash at execution */ #include &lt;stdio.h&gt; #define Q_MAX_SIZE 255 struct queue { int* pointer; int* currentValue; int max, count, theQueue[Q_MAX_SIZE]; }; //prototyps void initQueue(struct queue*); unsigned short pushQueue(struct queue*, int); int* popQueue(struct queue*); int main(void) { int i, j; struct queue obj[5]; for(i=0; i&lt;5; i++) { initQueue(&amp;obj[i]); for(j = 0; j&lt;3; j++) { pushQueue(&amp;obj[i], j); } } 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'); } return 0; } //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ void initQueue(struct queue *Q) { Q-&gt;pointer = Q-&gt;theQueue; Q-&gt;max = Q_MAX_SIZE; Q-&gt;count = 0; } unsigned short 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><strong>Update:</strong> the problem was in <code>initQueue()</code> and it's solved by allocating memory for <code>Q-&gt;currentValue</code> here is the function after editing:</p> <pre><code>void initQueue(struct queue *Q) { Q-&gt;currentValue = malloc(sizeof(int)); Q-&gt;pointer = Q-&gt;theQueue; Q-&gt;max = Q_MAX_SIZE; Q-&gt;count = 0; } </code></pre>
 

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