Note that there are some explanatory texts on larger screens.

plurals
  1. POarray in circular queue not getting initialised
    text
    copied!<p>I am having problem in the c implementation of circular queue. The enqueue operation is not working properly. The value is not getting initialised in the array.</p> <p>the structure for queue is:</p> <pre><code>typedef struct { int a[5]; int tail; int head; int cap; }Que; </code></pre> <p>the enqueue function:</p> <pre><code> int enque(Que *q,int num) { if((q-&gt;head)==(q-&gt;tail )) return -1; if(q-&gt;head==-1) q-&gt;head=0; q-&gt;a[q-&gt;tail]=num; (q-&gt;tail) ++; if(q-&gt;tail ==q-&gt;cap) q-&gt;tail=0; return 0; } </code></pre> <p>the dequeue function:</p> <pre><code> int deque(Que *q) { if((q-&gt;head)==-1) return -1; int b= q-&gt;a[q-&gt;head]; (q-&gt;head)++; if(q-&gt;head==q-&gt;cap) q-&gt;head=0; if(q-&gt;head==q-&gt;tail) q-&gt;head=-1; return b; } </code></pre> <p>the head is position of the index to be dequeued and tail is the index where the element has to be added. The initial values of head is -1 , tail is 0 and cap is 5. These values have been set by another function.</p> <p>@Klas Lindbäck Here is the complete code:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; typedef struct { int a[5]; int tail; int head; int cap; }Que; int deque(Que *q) { if((q-&gt;head)==-1) return -1; int b= q-&gt;a[q-&gt;head]; (q-&gt;head)++; if(q-&gt;head==q-&gt;cap) q-&gt;head=0; if(q-&gt;head==q-&gt;tail) q-&gt;head=-1; return b; } void initialize(Que *q) { q-&gt;head=-1; q-&gt;tail=0; q-&gt;cap=5; } int enque(Que *q,int num) { if((q-&gt;head)==(q-&gt;tail )) return -1; if(q-&gt;head==-1) q-&gt;head=0; q-&gt;a[q-&gt;tail]=num; printf("%d \n",q-&gt;a[q-&gt;tail]); (q-&gt;tail) ++; if(q-&gt;tail ==q-&gt;cap) q-&gt;tail=0; return 0; } int main() { Que q; initialize(&amp;q); int i=1; for(;i&lt;=5;i++) { int num; printf("input a number: "); scanf("%d",num); enque(&amp;q,num); } int c=q.a[0]; printf("%d %d %d\n",q.a[2],c,q.a[3]); } </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