Circular Queue using Linked List in C++

Circular Queue in C++

 

Circular Queue is a category of Queue data structure. If we equal the linear queue with the linear single linked list, the Circular queue can be equaled by the circular linked list. Here is an article about how to create a circular queue using a circular linked list in C++. Here the circular linked list will be implemented using class.

Circular Queue using Linked List 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.

Algorithm to Create Circular Queue (using a Linked List Class) 

In the circular queue, the back (Where elements will be pushed) will be pointing the front (From where elements are to be popped out). That is the main logic behind circular queues, like circular linked lists. Here is the algorithm to implement it in C++.

  • A class is made with the name Node, that represents the nodes in the queue. In our code, the nodes are actually holding an integer value, and a pointer of the same type as the node class.
  • A class is made with the named Queue or Circular_Queue. It is actually a class that will make an array of the Node type.
  • In the default constructor, the front and back will be NULL pointer, to show the queue is empty.
  • The enqueue function (Pushing a node into the queue) will be getting a value or a node value in the argument, and it will be pushing the element in the circular queue using the back pointer. And the new back pointer will be pointing to the front.
  • The Dequeue function will be deleting the node in the front position. And then the back pointer will be pointing to the new front. If the queue becomes null, the front and back both will point to null.
  • The show function will be showing the total circular queue starting from the back to the front.
Circular Queue using Linked List in C++

C++ Program for the implementation of circular queue using Linked Lists

Run
//Circular queue  C++ program 
#include<iostream>

using namespace std;

class Node
{
public:
  int data;
  Node *next;
};

class CircularQueue
{
private:
  Node * front;
  Node *rear;

public:
    CircularQueue ()
  {
    front = rear = nullptr;
  }

  void enqueue (int data)
  {
    Node *newNode = new Node ();
    newNode->data = data;
    newNode->next = nullptr;

    if (front == nullptr)
      {
	front = rear = newNode;
	rear->next = front;
      }
    else
      {
	rear->next = newNode;
	rear = newNode;
	rear->next = front;
      }

    cout << data << " has been enqueued." << endl;
  }

  void dequeue ()
  {
    if (front == nullptr)
      {
	cout << "Queue is empty." << endl;
      }
    else if (front == rear)
      {
	Node *temp = front;
	front = rear = nullptr;
	delete temp;
      }
    else
      {
	Node *temp = front;
	front = front->next;
	rear->next = front;
	delete temp;
      }
  }

  void display ()
  {
    if (front == nullptr)
      {
	cout << "Queue is empty." << endl;
      }
    else
      {
	Node *temp = front;
	cout << "Circular Queue: ";
	do
	  {
	    cout << temp->data << " ";
	    temp = temp->next;
	  }
	while (temp != front);
	cout << 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:

10 has been enqueued.
20 has been enqueued.
30 has been enqueued.
40 has been enqueued.
Circular Queue: 10 20 30 40 
Circular Queue: 30 40 
50 has been enqueued.
60 has been enqueued.
Circular Queue: 30 40 50 60