Home     Blog

Circular Queue

The difficulty of managing front and rear in an array-based non-circular queue can be overcome if we treat the queue position with index 0 as if it comes after the last position (in our case, index 9), i.e., we treat the queue as circular. Note that we use the same array declaration of the queue.

Empty queue:

circular queue clip image002 Circular Queue

Non-empty queues:

circular queue clip image004 Circular Queue

circular queue clip image006 Circular Queue

circular queue clip image008 Circular Queue

Implementation of operations on a circular queue:

Testing a circular queue for overflow

There are two conditions:

  • (front=0) and (rear=capacity-1)
  • front=rear+1

If any of these two conditions is satisfied, it means that circular queue is full.

.cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; } .cl { margin: 0px; } .cb1 { color: green; } .cb2 { color: blue; }

bool isfull(queue *q)
{
 if (((q->front==0)&&( q->rear==CAPACITY-1))||( q->front==q->rear+1))
 return true;
 else
 return false;
}

Note: bool can be defined as typedef enum {false, true} bool;

The enqueue Operation on a Circular Queue

There are three scenarios which need to be considered, assuming that the queue is not full:

  1. If the queue is empty, then the value of the front and the rear variable will be -1 (i.e., the sentinel value), then both front and rear are set to 0.
  2. If the queue is not empty, then the value of the rear will be the index of the last element of the queue, then the rear variable is incremented.
  3. If the queue is not full and the value of the rear variable is equal to capacity -1 then rear is set to 0.

void enqueue(queue *q, int value) /*assumes queue is not full*/
{
 /*adjust rear variable*/
 if (q->front==-1)
 q->front=q->rear=0;
 else if (q->rear==CAPACITY-1)
 q->rear=0;
 else
 q->rear++;
 /*store element at new rear */
 q->elements[q->rear]=value;
}

The dequeue Operation on a Circular Queue

Again, there are three possibilities:

  1. If there was only one element in the circular queue, then after the dequeue operation the queue will become empty. This state of the circular queue is reflected by setting the front and rear variables to -1.
  2. If the value of the front variable is equal to CAPACITY-1, then set front variable to 0.
  3. Ifneither of the above conditions hold, then the front variable is incremented

int dequeue(queue *q)
{ 
 int temp=q->elements[q->front]; /*store the front element in a temporary variable, so that we return this value later */
 
 /*adjust front variable */ 
 if (q->front == q->rear)
 q->front = q->rear=-1;
 else if (q->front == CAPACITY-1)
 q->front = 0;
 else
 q->front++;
 
 return temp; /*return the dequeued element*/
}

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Daniel Memetic on

Comments (3)

 

  1. rishi says:

    any body knows how to identify front and rear in circular queqe

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
    • suraj says:

      initially we can assume any index as front & rear…but keep rear is one less than the front…(eg if f=0,r=-1)….it is not compulsory …but it will give u better result….try it …im waiting for ur reply…..thanx… 

      VA:F [1.9.17_1161]
      Rating: 0.0/5 (0 votes cast)
  2. kibrom says:

    I think buzzzzzz on testing for empty and full

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

Related Posts

  • Linear Queue

    Implementation of operations on a linear queue: Creating an Empty Queue Before we can use a queue, it must be created. The purpose of initializing the queue is served by assigning -1 (as a sentinel value) to the front and rear variables. Note that the valid range of an index for the array is 0 [...]...


  • Queues

    A queue is a line of persons waiting for their turn at some service counter. The service counter can be a ticketing window of a cinema hall, a railway station, etc. Depending on the type of service provided by the service counter and number of persons interested in service, there could be queues of varying [...]...


  • Breadth First Search Algorithm

    A breadth first search traversal method, visits all the successors of a visited node before visiting any successor of any of its child nodes. This is a contradiction to depth first traversal method; which visits the successor of a visited node before visiting any of its brothers, i.e., children of the same parent. A depth [...]...


  • Priority Queues

    Stack and queue are data structures whose elements are ordered based on the sequence in which they have been inserted. The pop operation retrieves the last element inserted, and the remove operation retrieves the first element inserted. If there is an intrinsic order among the elements themselves (for example, numeric order or alphabetic order), it [...]...


  • Inserting in a Doubly Linked List

    Inserting an Element To insert an element in the list, the first task is to allocate memory for a new node, assign the element to be inserted to the info field of the node, and then the new node is placed at the appropriate position by adjusting appropriate pointers. Insertion in the list can take [...]...