
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Circular Queue Data Structure in C++
A queue is an abstract data structure that contains a collection of elements. Queue implements the FIFO mechanism i.e the element that is inserted first is also deleted first.
Queue cane be one linear data structure. But it may create some problem if we implement queue using array. Sometimes by using some consecutive insert and delete operation, the front and rear position will change. In that moment, it will look like the queue has no space to insert elements into it. Even if there are some free spaces, that will not be used due to some logical problems. To overcome this problem, we will use the circular queue data structure.
A circular queue is a type of queue in which the last position is connected to the first position to make a circle.
Example
#include <iostream> using namespace std; int cqueue[5]; int front = -1, rear = -1, n=5; void insertCQ(int val) { if ((front == 0 && rear == n-1) || (front == rear+1)) { cout<<"Queue Overflow \n"; return; } if (front == -1) { front = 0; rear = 0; } else { if (rear == n - 1) rear = 0; else rear = rear + 1; } cqueue[rear] = val ; } void deleteCQ() { if (front == -1) { cout<<"Queue Underflow\n"; return ; } cout<<"Element deleted from queue is : "<<cqueue[front<<endl; if (front == rear) { front = -1; rear = -1; } else { if (front == n - 1) front = 0; else front = front + 1; } } void displayCQ() { int f = front, r = rear; if (front == -1) { cout<<"Queue is empty"<<endl; return; } cout<<"Queue elements are :\n"; if (f <= r) { while (f <= r){ cout<<cqueue[f]<<" "; f++; } } else { while (f <= n - 1) { cout<<cqueue[f]<<" "; f++; } f = 0; while (f <= r) { cout<<cqueue[f]<<" "; f++; } } cout<<endl; } int main() { int ch, val; cout<<"1)Insert\n"; cout<<"2)Delete\n"; cout<<"3)Display\n"; cout<<"4)Exit\n"; do { cout<<"Enter choice : "<<endl; cin>>ch; switch(ch) { case 1: cout<<"Input for insertion: "<<endl; cin>>val; insertCQ(val); break; case 2: deleteCQ(); break; case 3: displayCQ(); break; case 4: cout<<"Exit\n"; break; default: cout<<"Incorrect!\n"; } } while(ch != 4); return 0; }
Output
1)Insert 2)Delete 3)Display 4)Exit Enter choice : 1 Input for insertion: 10 Enter choice : 1 Input for insertion: 20 Enter choice : 1 Input for insertion: 30 Enter choice : 1 Input for insertion: 40 Enter choice : 1 Input for insertion: 50 Enter choice : 3 Queue elements are : 10 20 30 40 50 Enter choice : 2 Element deleted from queue is : 10 Enter choice : 2 Element deleted from queue is : 20 Enter choice : 3 Queue elements are : 30 40 50 Enter choice : 4 Exit
- Related Articles
- Queue Data Structure in Javascript
- Explain linear data structure queue in C language
- Basic Operations for Queue in Data Structure
- Differences between stack and queue data structure
- Java Program to Implement the queue data structure
- Golang program to implement the Queue data structure
- C++ Program to Implement Circular Queue
- Python Program to Implement Queue Data Structure using Linked List
- Difference Between Linear Queue and Circular Queue
- How to Manage Full Circular Queue Event in C++?
- STL Priority Queue for Structure or Class in C++
- Design a queue data structure to get minimum or maximum in O(1) time
- Implementing circular queue ring buffer in JavaScript
- Rectangle Data in Data Structure
- Operations on Queue in Data Structures

Advertisements