title: Circular Queue Operations Using Array
body: <blockquote>
  <p>Code shows basic operations on Circular Queue.</p>
  </blockquote>
  <pre><code>#define maxsize 10
  typedef struct queue
  {
      int data[maxsize];
      int f,r;
  }myQueue;
  myQueue q;  
  
  void init(myQueue *q);
  int full(myQueue *q);
  int empty(myQueue *q);
  void enqueue(myQueue *q,int num);
  void dequeue(myQueue *q);
  void print(myQueue *q);
  
  void main()
  {
      init(&amp;q);
      int op;
      do
      {
          printf(&quot;\nCircular queue operations: Press:\n&quot;);
          printf(&quot;1 for enqueue\n&quot;);
          printf(&quot;2 for dequeue\n&quot;);
          printf(&quot;3 to print Circular Queue\n&quot;);
  
          int num,choice;
          printf(&quot;\nEnter choice:\n&quot;);
          scanf(&quot;%d&quot;,&amp;choice);
          switch(choice)
          {
               case 1: printf(&quot;Enter number to insert :\n&quot;);
                       scanf(&quot;%d&quot;,&amp;num);
                       if(full(&amp;q))
                       {
                           printf(&quot;\nQueue is full\n&quot;);
                           exit(0);
                       }
                       enqueue(&amp;q,num);
                       break;
               case 2: if(empty(&amp;q))
                       {
                           printf(&quot;\nQueue is empty\n&quot;);
                           exit(0);
                       }
                       dequeue(&amp;q);
                       break;
               case 3: printf(&quot;Printing current queue: \n&quot;);
                       print(&amp;q);
                       break;
               default:break;
          }
          printf(&quot;Press 1 to continue or 0 to exit:\n&quot;);
          scanf(&quot;%d&quot;,&amp;op);
      }
      while(op);
  }
  
  void init(myQueue *q)
  {
      q-&gt;f=-1;
      q-&gt;r=-1;
  }
  
  int full(myQueue *q)
  {
      if((q-&gt;r+1)%maxsize==q-&gt;f)
      {
          return 1;
      }
      else
          return 0;
  }
  
  int empty(myQueue *q)
  {
      if(q-&gt;r==-1)
      {
          return 1;
      }
      else
          return 0;
  }   
  </code></pre>
  <blockquote>
  <p>enqueue function is used to add the elements into queue.</p>
  </blockquote>
  <pre><code>void enqueue(myQueue *q,int num)
  {
      if(empty(&amp;q))
      {
          q-&gt;f=0;
          q-&gt;r=0;
      }
      else
      {
          q-&gt;r=(q-&gt;r+1)%maxsize;
      }
      q-&gt;data[q-&gt;r]=num;
      printf(&quot;\n%d is enqueued\n&quot;,q-&gt;data[q-&gt;r]);
  }
  </code></pre>
  <blockquote>
  <p>Dequeue function is used to delete elements from the stack.</p>
  </blockquote>
  <pre><code>void dequeue(myQueue *q)
  {
      int del_num;
      del_num=q-&gt;data[q-&gt;f];
      if(q-&gt;f==q-&gt;r)
      {
          init(&amp;q);
      }
      else
      {  //To move front to the next position in the circular array.
          q-&gt;f=(q-&gt;f+1)%maxsize;
      }
      printf(&quot;\n%d is dequeued\n&quot;,del_num);
  }
  
  void print(myQueue *q)
  {
      int i;
      for(i=q-&gt;f;i!=q-&gt;r;i=(i+1)%maxsize)
      {
          printf(&quot;%d\n&quot;,q-&gt;data[i]);
      }
      printf(&quot;%d\n&quot;,q-&gt;data[q-&gt;r]);
  }
      
  </code></pre>
  <blockquote>
  <p>Issue: Circular queue is automatically enqueuing the 0 element in it initially.
  However, rest of the operations are working just fine.</p>
  <p>I am not able to identify, why it is automatically inserting 0 in the circular queue, without me enqueuing it.</p>
  </blockquote>