CodesJava

Easy learning with example program codes

CPP Queue

Queue is a data structure in CPP that works on FIFO (First In First Out) technique, which means that the first inserted element will be deleted first in a queue.

Syntax:

template<class T, class Container = deque<T> > class queue;

Member Types of CPP Queue:

MEMBER TYPEUSES
container_typeTo specify underlying container type.
const_referenceTo specify the reference type of constant container.
size_typeTo specify the size range of the elements.
value_typeTo specify the element type.
referenceTo specify the reference type of container.

 

CPP Queue Functions:

FUNCTIONUSES
backTo access the rear element of the queue.
(constructor)To construct a queue container.
emptyTo test for the emptiness of a queue and to get a true value if the queue is empty.
emplaceTo insert a new element in the queue above the current rear end element.
frontTo access the front element of the queue.
pushTo insert a new element at the rear end of the queue.
popTo delete the element in the queue from the front end.
swapTo interchange the contents of two containers in reference.
sizeTo return the size of the queue container, which is a measure of the number of elements stored in the queue.

 

Example:

#include <iostream.h>  
#include <queue.h>  
using namespace std;  
void display(queue <int> ds)  
{  
queue <int> dis = ds;  
while (!dis.empty())  
{  
	cout << dis.front() << " ";  
	dis.pop();  
}  
}  
 
int main()  
{  
queue <int> getqueue;  
getqueue.push(80);  
getqueue.push(60);  
getqueue.push(50);  
getqueue.push(30);  
 
cout << "The Queue element from first to last are  : ";  
display(getqueue);  
 
cout << "\nThe value of getqueue.size() : " << getqueue.size();  
cout << "\nThe value of getqueue.front() : " << getqueue.front();  
cout << "\nThe value of getqueue.back() : " << getqueue.back();  
 
cout << "\nThe value of getqueue.pop() : ";  
getqueue.pop();  
display(getqueue);  
 
return 0;  
}

Output

The Queue element from first to last are : 80 60 50 30                                          
The value of getqueue.size() : 4                      
The value of getqueue.front() : 80                    
The value of getqueue.back() : 30   
The value of getqueue.pop() : 60 50 30
Please follow and like us:
error
Posted in CPP   








Copyright © 2019 CodesJava DMCA.com Protection Status