Circular Queue | Circular Queue Functions | Top 5 method of circular queue| Circular Queue Complete Code

Circular Queue Method
Circular Queue Method

Overview:

Circular Queue is the advanced version of a simple static queue. We can insert data after removing data from the array. In a simple queue, we can’t insert data once we remove data. Top circular queue functions are listed below.

Circular Queue Function:

  1. isEmpty()
  2. isFull()
  3. enQueue()
  4. deQueue()
  5. display() or Show()

1-    isEmpty()

This function work like an indicator. To check whether a queue is empty or not.

bool Circular_Queue::isEmpty()

{

    if (count == 0)

        return true;

    else

        return false;

}

IsEmpty
IsEmpty


2-   
isFull()

This function also acts as an indicator. To check whether a queue is full or not

bool Circular_Queue::isFull()

{

     if (count == queueSize)

          return true;

     else

          return false;

}

isFull()
isFull()

3-    enQueue(itemType data)

This function use to add data in the queue. We can insert data in the queue according to the size of the queue. When we reach the size of the queue then is a full() function called and we don’t able to insert more data.

void Circular_Queue::enQueue(itemType data)

{

     if (isFull())

          cout << "\nQueue is Full\n";

     else

     {

          myArray[rear] = data;

          rear = (rear + 1) % queueSize;

          count++;

     }

}

Working

queueSize=6;

At initial stage rear, count, and front are same position like;

rear=0;

count=0;

front=0;

when we call enQueue() method with data(12). You can understand all step how it work through diagram.

enQueue(12);

rear= 0;

count=0;

myArray[rear]=12;

myArray[0]=12;

rear=(rear+1)%queueSize;

rear=(0+1)%6;

rear=1;

++count=1;

enQueue
enQueue


enQueue(13);

rear=1;

count=1;

myArray[rear]=13;

myArray[1]=13;

rear=(rear+1)%queueSize;

rear=(1+1)%6;

rear=2;

++count=2;

enQueue
enQueue


enQueue(14);

rear=2;

count=2;

myArray[rear]=14;

myArray[2]=14;

rear=(rear+1)%queueSize;

rear=(2+1)%6;

rear=3;

++count=3;

enQueue
enQueue


enQueue(15);

rear=3;

count=3;

myArray[rear]=15;

myArray[3]=15;

rear=(rear+1)%queueSize;

rear=(3+1)%6;

rear=4;

++count=4;

enQueue
enQueue


enQueue(16);

rear=4;

count=4;

myArray[rear]=16;

myArray[4]=16;

rear=(rear+1)%queueSize;

rear=(4+1)%6;

rear=5;

++count=5;

enQueue
enQueue


enQueue(17);

rear=5;

count=5;

myArray[rear]=17;

myArray[5]=17;

rear=(rear+1)%queueSize;

rear=(5+1)%6;

rear=0;

++count=6;

enQueue
enQueue


enQueue(18);

count=6;

count==queueSize;

so, Queue is Full. No more data will insert.

4- deQueue()

This method use to remove data from the queue. When all data remove then the isEmpty() function called which indicates the queue is empty.

int Circular_Queue::deQueue()

{

     int value = -1;

     if (isEmpty())

          return value;

     else

     {

          value = myArray[front];

          front = (front + 1) % queueSize;

          count--;

     }

     return value;

}

deQueue
deQueue


Working

queueSize=6;

count=6;

deQueue();

front=0;

count=6;

front=(front+1)%queueSize;

front=(0+1)%6;

front=1;

--count=5;

deQueue
deQueue


deQueue();

front=1;

count=5;

front=(front+1)%queueSize;

front=(1+1)%6;

front=2; --count=4;

deQueue
deQueue


deQueue();

front=2;

count=4;

front=(front+1)%queueSize;

front=(2+1)%6;

front=3;

--count=3;

deQueue
deQueue


deQueue();

front=3;

count=3;

front=(front+1)%queueSize;

front=(3+1)%6;

front=4;

--count=2;

deQueue
deQueue


deQueue();

front=4;

count=2;

front=(front+1)%queueSize;

front=(4+1)%6;

front=5;

--count=1;

deQueue
deQueue


deQueue();     

front=5;

count=1;

front=(front+1)%queueSize;

front=(5+1)%6;

front=0;

--count=0;

deQueue
deQueue


deQueue();

isEmpty().

deQueue
deQueue


5- display()

This method use to display data from queue.

void Queue::display()

{

     int i=count;

     int j=front;

     while(i!=0)

     {

          cout<< myArray[j]<<endl;

          j=(j+1)%count;

          i--;

     }

}

0 Comments