What are the drawbacks of linear queue? How this problem overcome can be overcome? Or, Circular queue short note.

By the definition of a queue, when we add an element in queue, rear pointer is increased by 1 whereas when we remove an element front pointer is increased by 1. But in array implementation of queue this may cause problem as shown in figure. Here front = 2 and rear = 4. Now, actually we have deleted 2 elements from queue so, there should be space for another 2 elements in the queue, but as rear pointer is pointing at last position and queue overflow condition (REAR == SIZE-1) is true, we cannot insert new element in the queue even if it has an empty space. To overcome this problem there is another variation of queue called circular queue.

Algorithm for insert in Circular queue

Algorithm for remove from Circular queue

Step 1: Initialize FRONT = -1, REAR = 1

Step 2: REAR = (REAR + 1) % SIZE

Step 3: if(FRONT == REAR)

  1. Display “Queue is full”
  2. Exit

Else

Input the value to be inserted and assign to variable “DATA”

Step 4: if(FRONT == -1)

  1. FRONT = 0
  2. REAR = 0

Step 5: Q[REAR] = DATA

Step 6: Exit

Step 1: if(FRONT == -1)

  1. Display “Queue is empty”
  2. Exit

Else

DATA = Q[FRONT]

Step 2: if(REAR == FRONT)

  1. FRONT = -1
  2. REAR = -1

Else

FRONT = (FRONT + 1) % SIZE

Step 3: Exit

 

 

 

30

40

50

 

 

 

Linear queue

 

 

                                                                                                                                           

Share with

Comments 0

Add your comment