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.
Queue VS Circular Queue?
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.
C++ Program for the implementation of circular queue using Linked Lists
//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
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
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
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- 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)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- 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
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java
Login/Signup to comment