Circular Queue using Array in C++

Circular Queue in C++

A basic linear data structure is Linear queue, where you can get First in First out feature for nodes. Circular Queue is a implementation of that very linear queue in which you can overcome the problems regarding linear fixed length queues. Here is an article on how to implement a Circular Queue using array in C++.

Circular Array using Queue in C++

Queue VS Circular Queue?

Let’s say, the queue is implemented using an array. So everytime you pop out one element, the front pointer in the array will be increasing by one. After popping out many products, the queue may be impossible to access for storage reasons, even if you don’t have any element in it.
 
That’s why at first the concept of Circular queue came in. In Circular queue, even if you change the front, after some time if the last pointer is accessed with the front pointer, after popping out again, the front pointer will again reach the first position.

Members of Circular Queue:

Circular queues are almost like Linear regular Queue with some modifications.
 
  • Front : From where nodes are to be popped out.
  • Back : Back or Rear, where nodes are pushed into.
  • Enqueue : Pushing a new node at the back.
  • Dequeue : Popping out nodes from front, if any.
  • Overflow : If the Circular Queue has predefined storage and is full.
  • Underflow : If the Circular Queue is empty.
  • Size : Number of nodes present.

If we create a queue using an array, where enqueuing and dequeuing takes O(1) time and space complexity each, one the queue is full, you cannot insert new elements, also sometimes when you delete elements, the queue may be totally empty but still the front will be in the back most position, so you can’t insert any value over there.

In this meantime, a circular queue comes into the picture. In Circular queue, the front and back only overlap when the queue is empty, but there is no problem when you want to delete elements and insert them again. The front pointer wanders around the queue in the circular path.

Also : Circular Queue using Array

Circular Queue using Array in C

C++ Program for the implementation of circular queue using Array

Run
//Circular queue  C++ program 
#include<iostream>
#define MAX_SIZE 100		// Maximum size of the queue

using namespace std;

class CircularQueue
{
private:
  int front, rear;
  int arr[MAX_SIZE];

public:
    CircularQueue ()
  {
    front = -1;
    rear = -1;
  }

  // Function to check if the queue is full
  bool isFull ()
  {
    if ((front == 0 && rear == MAX_SIZE - 1)
	|| (rear == (front - 1) % (MAX_SIZE - 1)))
      {
	return true;
      }
    return false;
  }

  // Function to check if the queue is empty
  bool isEmpty ()
  {
    if (front == -1)
      {
	return true;
      }
    return false;
  }

  // Function to add an element to the queue
  void enQueue (int value)
  {
    if (isFull ())
      {
	cout << "Queue is full." << endl;
      }
    else
      {
	if (front == -1)
	  {
	    front = 0;
	  }
	rear = (rear + 1) % MAX_SIZE;
	arr[rear] = value;
	cout << "Enqueued element: " << value << endl;
      }
  }

  // Function to remove an element from the queue
  int deQueue ()
  {
    int element;
    if (isEmpty ())
      {
	cout << "Queue is empty." << endl;
	return -1;
      }
    else
      {
	element = arr[front];
	if (front == rear)
	  {
	    front = -1;
	    rear = -1;
	  }
	else
	  {
	    front = (front + 1) % MAX_SIZE;
	  }
	cout << "Dequeued element: " << element << endl;
	return element;
      }
  }

  // Function to display the elements in the queue
  void display ()
  {
    if (isEmpty ())
      {
	cout << "Queue is empty." << endl;
      }
    else
      {
	cout << "Elements in the queue: ";
	int i;
	for (i = front; i != rear; i = (i + 1) % MAX_SIZE)
	  {
	    cout << arr[i] << " ";
	  }
	cout << arr[i] << endl;
      }
  }
};

int main ()
{
  CircularQueue q;

  q.enQueue (10);
  q.enQueue (20);
  q.enQueue (30);
  q.enQueue (40);

  q.display ();

  q.deQueue ();
  q.deQueue ();

  q.display ();

  q.enQueue (50);
  q.enQueue (60);

  q.display ();

  return 0;
}
Output:

Enqueued element: 10
Enqueued element: 20
Enqueued element: 30
Enqueued element: 40
Elements in the queue: 10 20 30 40
Dequeued element: 10
Dequeued element: 20
Elements in the queue: 30 40
Enqueued element: 50
Enqueued element: 60
Elements in the queue: 30 40 50 60

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java