Open In App

Circular Linked List Implementation of Circular Queue

Last Updated : 05 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The task is to implement the circular queue with the following operations using a circular linked list.

Operations on Circular Queue:

  • Front: Get the front item from the queue.
  • Rear: Get the last item from the queue.
  • enQueue(value): This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at the Rear position.
  • deQueue(): This function is used to delete an element from the circular queue. In a queue, the element is always deleted from the front position.

Prerequisite – Circular Singly Linked List 

Circular-Linked-List-Implementation-of-Circular-Queue


Approach:

We have discussed basics and how to implement circular queue using array. Please refer to Introduction to Circular Queue.

 The idea is to initialize two pointers front and rear. front will point to the first node of the circular linked list, and rear will point to the last node of the circular linked list.

Operations:

  1. Front(): return the front node’s value if not null. Otherwise return -1.
  2. Rear(): return the rear node’s value if not null. Otherwise return -1.
  3. EnQueue(int value): Create a new node and set its value. If front is null, then set front = rear = newNode. Otherwise, set tail->next = newNode, tail = newNode, tail->next = front.
  4. DeQueue(): If list is empty, return -1. Otherwise get the front node’s value and remove it from the list. If the list contains only one node, then set front = rear = null. Otherwise update head = head->next and rear->next = head.
C++
// C++ program for insertion and
// deletion in Circular Queue
#include <bits/stdc++.h>
using namespace std;

class Node {
  public:
    int data;
    Node *next;
    Node(int newdata) {
        data = newdata;
        next = nullptr;
    }
};

class Queue {
  public:
    Node *front, *rear;
    Queue() {
        front = rear = nullptr;
    }
};

// Function to insert element in a Circular queue
void enQueue(Queue *q, int value) {
    struct Node *newNode = new Node(value);

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

    q->rear = newNode;
    q->rear->next = q->front;
}

// Function to delete element from Circular Queue
int deQueue(Queue *q) {

    // if queue is empty
    if (q->front == nullptr) {
        return -1;
    }

    int value;

    // If this is the last node to be deleted
    if (q->front == q->rear) {
        value = q->front->data;
        delete (q->front);
        q->front = nullptr;
        q->rear = nullptr;
    }
    else {
        Node *temp = q->front;
        value = temp->data;
        q->front = q->front->next;
        q->rear->next = q->front;
        delete (temp);
    }

    return value;
}

// Function to return the front value
int front(Queue *q) {
    Node *front = q->front;

    if (front == nullptr) {
        return -1;
    }

    return front->data;
}

// Function to return the rear value
int rear(Queue *q) {
    Node *rear = q->rear;

    if (rear == nullptr) {
        return -1;
    }

    return rear->data;
}

void printQueue(Queue *q) {
    Node *curr = q->front;

    do {
        cout << curr->data << " ";
        curr = curr->next;
    } while (curr != q->front);

    cout << endl;
}

int main() {
  
    Queue *q = new Queue;

    enQueue(q, 14);
    enQueue(q, 22);
    enQueue(q, 6);
    enQueue(q, 20);

    cout << "Front value: " << front(q) << endl;
    cout << "Rear value: " << rear(q) << endl;

    printQueue(q);

    cout << "Deleted value = " << deQueue(q) << endl;
    cout << "Deleted value = " << deQueue(q) << endl;

    printQueue(q);

    return 0;
}
C Java Python C# JavaScript

Output
Front value: 14
Rear value: 20
14 22 6 20 
Deleted value = 14
Deleted value = 22
6 20 

Time Complexity: O(1), for enQueue, deQueue, front, rear and O(n) for printQueue.
Auxiliary Space: O(n), where n is the maximum number of elements that can be stored in the queue.



Similar Reads

three90RightbarBannerImg