I am Implementing a Queue using circular arrays in C language .The Implementation uses one empty position to indicate that the queue is full.That is if the rear is two position behind front. The condition is we cant use a variable to count the entries in the queue as we insert or delete . I have created this function to find the size of queue with Time Complexity Big-Oh(n) because it starts with front and goes upto rear in a loop .int QueueSize(Queue *q){    if(QueueEmpty(q))    {        return 0;    }    else{        int count=0;    for(int i=q->front;;i=(i+1)%MAXQUEUE)    {        count++;        if(i==q->rear) break;    }        return count;}}Is there a way to find the size of queue in constant time ?Elements are inserted from rear and deleted from front !My Queue starts from 0 and assume maximum queue size is 6 in below example 10 Appended at position 020 Appended at position 130 Appended at position 240 Appended at position 350 Appended at position 410 Removed from position 020 Removed from position 160 Appended at position 570 Appended at position 030 Removed from position 240 Removed from position 380 Appended at position 190 Appended at position 2Queue Size:5Position:4  Element:50 (front)Position:5  Element:60Position:0  Element:70Position:1  Element:80Position:2  Element:90 (rear)
Size of Circular Queue
algorithm analysis;data structures;queues
Suppose that the queue consists of elements $Q[a],\ldots,Q[b]$, where $Q = Q[1],\ldots,Q[n]$ is an array of length $n$. If $a \leq b$ then the queue contains $b-a+1$ elements, and if $a > b$ then the queue contains $b-a+1+n$ elements.