title: Circular Queue Implementation
body: <p>The below circular queue implementation is from a data structures book.</p>
  
  <pre><code>struct circqueue {
     int front,rear;
     int capacity;
     int *array;
  };
  
  
  
  
  struct circqueue *q(int size) {
     struct circqueue *q=malloc(sizeof(struct circqueue));
     if(!q)return NULL;
     q-&gt;capacity=size;
     q-&gt;front=-1;
     q-&gt;rear=-1;
     q-&gt;array=malloc(q-&gt;capacity*sizeof(int));
     if(!q-&gt;array)return NULL;
    return q;
  }
  
  int isemptyqueue(struct circqueue *q) {
     return(q-&gt;front==-1);
  }
  
  int isfullqueue(struct circqueue *q) {
     return((q-&gt;rear+1)%q-&gt;capacity==q-&gt;rear);
  }
  
  int queuesize(struct circqueue *q) {
     return(q-&gt;capacity-q-&gt;rear+q-&gt;front+1)%q-&gt;capacity;
  }
  
  
  void enqueue(struct circqueue *q,int x) {
  
     if(isfullqueue(q))
        printf("queue overflow\n");
     else{
        q-&gt;rear=(q-&gt;rear+1)%q-&gt;capacity;
        q-&gt;array[q-&gt;rear]=x;
        if(q-&gt;front==-1) {
           q-&gt;front=q-&gt;rear;
        }
     }
  }
  
  int dequeue(struct circqueue *q) {
     int data=0;
  
     if(isemptyqueue(q)) {
        printf("queue underflow");
        return 0;
     }
     else {
        data=q-&gt;array[q-&gt;front];
        if(q-&gt;front==q-&gt;rear)
           q-&gt;front=q-&gt;rear=-1;
        else
           q-&gt;front=(q-&gt;front+1)%q-&gt;capacity;
     }
  
     return data;
  }
  </code></pre>
  
  <p>I have not been able to understand the precise use of <code>struct circqueue *q(int size)</code>?
  Is it a function to initialize the queue? Also how to invoke it from main()? Can anybody explain it to me.Thanks</p>