Understanding Circular Queues in C++: A Practical Implementation

Introduction:

Below is a C++ code snippet that demonstrates the implementation of a circular queue using an array. The code showcases basic operations like enqueueing (adding elements) and dequeuing (removing elements) from the circular queue. This code aims to provide a practical example of how a circular queue works and how its operations are performed.

#include 
using namespace std;

int Q[10];
int size=10;
int count=0;
int rear=0;
int front=0;
void enQ(int element)
{
   if(count==size)
   {
       cout<<"Queue Overflow"<<endl;
   }
   else
   {
       Q[rear]=element;
       rear=(rear+1)%size;
       count++;
   }
}
void DeQ()
{
    if(count==0)
    {
        cout<<"Under Flow"<<endl;
    }
    else
    {
        Q[front]=0;
        front=(front+1)%size;
        count--;
    }
}
bool isempty()
{
    if(count==0)
    {
        return true;
    }
}

int main()
{
    int element;
    int e;

    if(isempty()==true)
    {
        cout<<"Queue is empty"<<endl;
    }
    /*************Enqueue*************/
    do
    {
    cout<<"Enter element in queue:"<<endl; cin>>element;
    enQ(element);
    cout<<"Do you want enqueue again?" <<endl; cin>>e;
    }while(e==1);
    /************display*************/
    cout<<"After inserting value in circular queue"<<endl;
    for(int i=front;i<=count;i++)
    {
        cout<<Q[i]<<" ";
    }
    /***************deletion***************/
    int d;
    cout<<"Do you want to delete from the queue(1=y): "<<endl; cin>>d;

    if(d==1)
    {
        int c;
        do
        {

        DeQ();

        cout<<"DO you want to enqueue again?(1=y)"<<endl; cin>>c;
        }while(c==1);
    }

    /************display*************/

    cout<<"After Dequeuing"<<endl;
    for(int i=front;i<=count;i++)
    {
        cout<<Q[i]<<" ";
    }

    return 0;
}

Code Explanation:

The provided code implements a circular queue using an array Q, with a specified size of 10. It includes functions to enqueue (enQ), dequeue (DeQ), and check if the queue is empty (isempty).

All Steps:

  1. The code defines the array Q, its size, count to keep track of the number of elements, rear to point to the rear of the queue, and front to point to the front of the queue.
  2. The enQ function adds an element to the queue if it’s not full. It updates the rear and count variables, while handling the circular aspect using the modulo operator.
  3. The DeQ function removes an element from the front of the queue if it’s not empty. It updates the front and count variables, effectively “deleting” the element.
  4. The isempty function checks if the queue is empty and returns a boolean value accordingly.
  5. The main function demonstrates the usage of these functions:
    • It checks if the queue is empty using isempty and displays a message if it is.
    • It enqueues elements in a loop and prompts the user if they want to enqueue more.
    • It displays the queue contents after enqueuing.
    • It prompts the user if they want to delete elements from the queue.
    • If the user chooses to delete, the code dequeues elements in a loop and prompts if they want to enqueue more after each dequeue.
    • Finally, it displays the queue contents after dequeuing.

FAQs (Frequently Asked Questions):

Q1: What is the purpose of using a circular queue?

A circular queue efficiently utilizes memory by reusing space after dequeuing elements, making it suitable for scenarios where a queue needs to be implemented with a fixed-size array.

Q2: Why is the modulo operator used in the enQ and DeQ functions?

The modulo operator ensures that the indices (rear and front) wrap around when they reach the end of the array, creating the circular behavior essential for a circular queue.

Q3: How can I modify the code for a larger queue size?

You can change the size variable to any desired value in order to increase or decrease the size of the circular queue.

Q4: Can this code handle different data types?

Yes, the data type of the queue elements can be changed by modifying the type of the Q array and the element variable.

Q5: What happens if the queue is full and I try to enqueue more elements?

In this code, attempting to enqueue when the queue is full will display a “Queue Overflow” message.

Feel free to reach out if you have more questions or need further clarification on any aspect of the code.

Conclusion:

Understanding circular queues is essential for efficient data management. This C++ code snippet offers a practical insight into implementing and using a circular queue structure. By breaking down the code and its functions, we’ve explored the fundamental operations that make circular queues a valuable tool in computer science. Feel free to adapt this code for your projects or explore further applications of circular queues in various programming scenarios.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top