circular queue

 Circular queue


It is the type of queue in the data structure. The problem of the memory wastage faced in the normal queue is resolved in the circular.


 


How does a circular queue work?

In this structure, we enqueue the value until the end of the queue is reached then again start from the front of the structure. The increment of the structure is to work on the module division with the size of the queue.

Photo:

Implementation of the circular queue:

    




Circular operations:


Two pointer front and rear are used.

Front is used to track the first element and initially, set value with -1.

Rear is used to track the last element and initially, set value with -1.


Enqueue operation:


  • Firstly,check if the queue is full

  • If not full then increase the value of the front from -1 to 0.

  • Also, increase the value of the rear by 1, if the rear already pointed to the end then its next would be the start of the structure.


Dequeue operation

  • Firstly,check if the queue is empty or not.

  • Return the value of the front pointer.

  • Likewise in enqueue,increase the index of the front to 1.

  • Reset the value of the front and rear pointer.




If the queue is full then additional cases would be added in the code.

  • Front =0 && Rear == size-1

  • Front = rear =rear+1






Implementation of the circular queue:


#include <iostream>
using namespace std;
class Queue {
   private:
    int SIZE=5;
  int items[5], front, rear;

   public:
  Queue() {
    front = -1;
    rear = -1;
  }
//function for check queue full or not

  bool isFull() {
    if (front == 0 && rear == SIZE - 1) {
      return true;
    }
    if (front == rear + 1) {
      return true;
    }
    return false;
  }
  
  // function for check empty or not
  bool isEmpty() {
    if (front == -1)
      return true;
    else
      return false;
  }
  //enqueue function
  void enQueue(int element) {
    if (isFull()) {
      cout << "Queue is full";
    } 
else {
      if (front == -1) front = 0;
      rear = (rear + 1) % SIZE;
      items[rear] = element;
      cout << endl<< "enqueue " << element << endl;
    }
  }
  // dequeue function
  
  int deQueue() {
    int element;
    if (isEmpty()) {
      cout << "Queue is empty" << endl;
      return (0);
    } 
else {
      element = items[front];
      if (front == rear) {
        front = -1;
        rear = -1;
      }
      // Q has only one element,
      // so we reset the queue after deleting it.
      else {
        front = (front + 1) % SIZE;
      }
      return (element);
    }
  }

  void display() {
    // Function to display status of Circular Queue
    int i;
    if (isEmpty()) {
      cout << endl<< "Empty Queue" << endl;
    } 
else {
      cout << "Front = " << front;
      cout << endl<< "Items = ";
      for (i = front; i != rear; i = (i + 1) % SIZE)
        cout << items[i];
      cout << items[i];
      cout << endl<< "Rear = " << rear;
    }
  }
};

int main() {
  Queue obj;

  
  obj.deQueue();

  obj.enQueue(1);
  obj.enQueue(2);
  obj.enQueue(3);
  obj.enQueue(4);
  obj.enQueue(5);

  
  obj.enQueue(6);

  obj.display();

  int var = obj.deQueue();

  if (var != -1)
    cout << endl<< "Deleted Element " << var;

  obj.display();

  

  return 0;
}

Application:


  • Memory management:It is used in memory management.

  • Process Scheduling: A CPU used in scheduling.

  • Traffic Systems.


Comments

Popular posts from this blog

Flutter layout

how to cout in cpp

input by cin in c++