Home  >  Article  >  Backend Development  >  How to manage a complete circular queue of events in C++?

How to manage a complete circular queue of events in C++?

WBOY
WBOYforward
2023-09-04 18:41:031132browse

introduce

Circular Queue is an improvement on linear queues, which was introduced to solve the problem of memory waste in linear queues. Circular queues use the FIFO principle to insert and delete elements from it. In this tutorial, we will discuss the operation of a circular queue and how to manage it.

What is a circular queue?

A circular queue is another type of queue in a data structure, with its front end and back end connected to each other. It is also known as circular buffer. It operates similarly to a linear queue, so why do we need to introduce a new queue in the data structure?

When using a linear queue, when the queue reaches its maximum limit, there may be some memory space before the tail pointer. This results in memory loss, and a good algorithm should be able to make full use of resources.

In order to solve the problem of memory waste, developers introduced the concept of circular queue, which has the ability to circularly link to the backend and frontend, and can insert more elements.

How to manage a complete circular queue of events in C++?

Basic functions of circular queue

  • After − It returns the last value of the queue.

  • Front − It returns the front value of the queue.

  • deQueue − This built-in method is used to remove elements from the queue while checking if the queue is empty.

  • enQueue − This method is used to insert new elements while checking the queue size.

In the circular queue, elements are added from the back end and removed from the front end. deQueue and enQueue are queue size-independent functions and are implemented using the modulo operator. Their time complexity is O(1).

Manage Circular Queue

We manage circular queues by using enQueue and deQueue operations. Initially, the front value of the circular queue is 0, the rear value is -1, and all elements in the circular queue are NULL.

Example

C code, using array to implement circular queue

#include <bits/stdc++.h>
using namespace std;
 
class Queue {
   //Initializing front and rear of the queue
   int rear, front;
   int sz;
   int* arr;
 
   public:
   Queue(int s) {
      front = rear = -1;
      sz = s;
      arr = new int[s];
   }
   
   void enQueue(int v);
   int deQueue();
   void displayQueue();
};
 
//Circular queue function
void Queue::enQueue(int v) {
   if ((front == 0 && rear == sz - 1)
      || (rear == (front - 1) % (sz - 1))) {
         printf("\nNo Space Queue is Full");
         return;
      }
   
      //Inserting the front element
      else if (front == -1) {
         front = rear = 0;
         arr[rear] = v;
      }
   
      else if (rear == sz - 1 && front != 0) {
         rear = 0;
         arr[rear] = v;
      }
   
      else {
         rear++;
         arr[rear] = v;
      }
}
 
//Function for deleting queue elements
int Queue::deQueue() {
   if (front == -1) {
      printf("\nQueue needs data it is empty");
      return INT_MIN;
   }
   
   int ele = arr[front];
   arr[front] = -1;
   if (front == rear) {
      front = -1;
      rear = -1;
   }
   else if (front == sz - 1)
      front = 0;
   else
      front++;
   return ele;
}
 
//Printing Circular queue elements
void Queue::displayQueue() {
   if (front == -1) {
      printf("\nQueue Empty");
      return;
   }
   printf("\nCircular Queue elements are: \n");
   if (rear >= front) {
      for (int i = front; i <= rear; i++)
      printf("%d ", arr[i]);
   } else {
      for (int i = front; i < sz; i++)
      printf("%d ", arr[i]);
   
      for (int i = 0; i <= rear; i++)
      printf("%d ", arr[i]);
   }
}
 
int main() {
   Queue q(5);
   //Pushing data in circular queue
   q.enQueue(10);
   q.enQueue(20);
   q.enQueue(3);
   q.enQueue(5);
   //Printing circular queue elements
   q.displayQueue();
   
   //Deleting front elements of circular queue
   printf("\nDeleted element = %d\n", q.deQueue());
   printf("\nDeleted element = %d", q.deQueue());
   q.displayQueue();
   q.enQueue(13);
   q.enQueue(27);
   q.enQueue(50);
   q.displayQueue();
   q.enQueue(22);
   
   return 0;
}

Output

Circular Queue elements are: 
10 20 3 5 
Deleted element = 10

Deleted element = 20
Circular Queue elements are: 
3 5 
Circular Queue elements are: 
3 5 13 27 50 
No Space Queue is Full

in conclusion

Circular queues are used in memory management and CPU scheduling. It uses the displayQueue() function to display queue elements.

We have reached the end of this tutorial. I hope this tutorial helped you understand how to implement a circular queue.

The above is the detailed content of How to manage a complete circular queue of events in C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete