A queue C++ is a linear data structure that follows FIFO manner, i.e. first-in, first-out manner. A queue is a linked list in which the elements are inserted from the rear end and deleted from the front end. That means, unlike stack, the queue has two ends.
Table of Contents
How to use queue in C++?
If you are using the queue header file, then include the header file in your program and call the built-in function to make operations like insert, delete, etc.
If you are implementing the queue without using a header file, then declare a queue class in C++. In that class, declare an array and two pointers that will point to two locations. The first is rear of queue and the second is front of queue. Use these data members to add and delete elements from the queue.
What are the types of C++ queue?
C++ provides many types of queue. The types of queue are listed below.
1. Simple queue
A simple queue is a queue in which the enqueue operation takes place from the rear end, and the dequeue operation takes place from the front end.
2. Circular queue
A circular queue is a linear data structure in which both ends of the queue are connected. It is also called ‘Ring Buffer’.
3. Dequeue (double-ended queue)
A dequeue is the same as a queue. But in dequeue, we can add and remove elements from both ends.
4. C++ priority queue
Priority queue in C++, the elements are arranged as per their priority. That means the element having a higher priority is served before the element having less priority.
Queue operations
- Insert – To add an element in the queue.
- Delete – To remove an element from the queue.
- Isempty – To check element is empty or not.
- Isfull – To check the limit of the queue.
In-built functions of the queue in a queue header file
The in-built functions in the queue header file are listed below.
- isempty() – This function is used to check the queue is empty or not.
- size() – This function returns the size of queue.
- swap() – The swap function swap yeo queues if and only if both are of the same type no matter what is their size.
- emplace() – This function is to add the element in the queue. Emplace function will add the element at the rear end.
- front() – This function will return the reference of the element present at the front of the queue.
- rear() – This function will return the reference of the element present at the rear of a queue.
- push(element) – This element works the same as emplace function. It will add the element to the rear end of a queue.
- pop() – This function is used to remove the element from the front end.
Check out C++ queue examples
1. Implementation of C++ queue without using a queue header file.
Example
#include <iostream>
using namespace std;
class queue
{
int queue[100], n = 100, front = - 1, rear = - 1;
public:
void insert()
{
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else
{
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void dlt()
{
if (front == - 1 || front > rear)
{
cout<<"Queue is full";
return ;
}
else
{
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void display()
{
if (front == - 1)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
};
main()
{
queue obj;
int ch;
cout<<"1. Insert element"<<endl;
cout<<"2. Delete element"<<endl;
cout<<"3. Display elements"<<endl;
cout<<"4. Exit"<<endl;
do
{
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch)
{
case 1: obj.insert();
break;
case 2: obj.dlt();
break;
case 3: obj.display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
}while(ch!=4);
}
Output
1. Insert element
2. Delete element
3. Display elements
4. Exit
Enter your choice :
1
Insert the element in queue :
5
Enter your choice :
1
Insert the element in queue :
7
Enter your choice :
1
Insert the element in queue :
9
Enter your choice :
3
Queue elements are : 5 7 9
Enter your choice :
2
Element deleted from queue is : 5
Enter your choice :
2
Element deleted from queue is : 7
Enter your choice :
3
Queue elements are : 9
Enter your choice :
4
Exit
2. Implementation of C++ queue using queue header file.
Example
#include <iostream>
#include <queue>
using namespace std;
class queue1
{
queue <int> q;
public:
void insert(int n)
{
q.push(n);
cout<<"Element inserted"<<endl;
}
void dlt()
{
q.pop();
cout<<"Element removed"<<endl;
}
void display()
{
cout<<"Elements of the queue:";
while (!q.empty())
{
cout << '\t' << q.front();
q.pop();
}
cout << '\n';
}
void qsize()
{
cout<<"Size of queue is :"<<q.size()<<endl;
}
void chfront()
{
cout<<"Element present at front is :"<<q.front()<<endl;
}
void chback()
{
cout<<"Element present at rear is :"<<q.back()<<endl;
}
void emt()
{
if(q.empty())
cout<<"Queue is empty\n";
else
cout<<"Queue is not empty\n";
}
};
main()
{
queue1 obj;
int n;
int ch;
cout<<"1. Insert element"<<endl;
cout<<"2. Delete element"<<endl;
cout<<"3. Display elements"<<endl;
cout<<"4. Size of queue"<<endl;
cout<<"5. Check queue is empty or not"<<endl;
cout<<"6. Exit"<<endl;
do
{
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch)
{
case 1:
cout<<"Enter element to be inserted : ";
cin>>n;
obj.insert(n);
break;
case 2: obj.dlt();
break;
case 3: obj.display();
break;
case 4: obj.qsize();
break;
case 5: obj.emt();
break;
case 6: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=6);
}
Output
1. Insert element
2. Delete element
3. Display elements
4. Size of queue
5. Check queue is empty or not
6. Exit
Enter your choice :
1
Enter element to be inserted : 8
Element inserted
Enter your choice :
1
Enter element to be inserted : 5
Element inserted
Enter your choice :
1
Enter element to be inserted : 0
Element inserted
Enter your choice :
4
Size of queue is :3
Enter your choice :
5
Queue is not empty
Enter your choice :
2
Element removed
Enter your choice :
2
Element removed
Enter your choice :
3
Elements of the queue: 0
Enter your choice :
6
Exit
To use a priority queue in C++. you can use the in-built functions predefined in the queue header file to perform all the operations on it.
Code to iterate queue
while (!Q.empty())
{
Person p = Q.top();
Q.pop();
}
Do You Know? |
---|
1. For Loop in C++ |
2. Vectors in C++ |
3. Difference Between C and C++ |
4. C++ Swap |
5. C++ List |
6. C++ Hello World |
7. C++ Map |
8. Converting String to Int C++ |
9. Stack C++ |
10. Queue C++ |