Home
Aptitude
Categories


Home » C++ STL

Queue in C++ Standard Template Library (STL)



C++ STL Queue: Here, we are going to learn about Queue in C++ Standard Template Library (STL) and its basic functions.
Submitted by Radib Kar, on September 27, 2018

C++ provides the awesome feature of STL where we can use the basic ADTs without knowing the code behind the implementations. STL helps a lot to use ADTs efficiently without actually implementing them. This article provides basic knowledge to represent a Queue using C++ STL.

Queue:

A queue is the data structure where the order of elements is important and the queue is maintained as FIFO (First In First Out).

The basic ADT operations for queue are...

  1. EnQueue (int x): to enqueuer an element at the rear end
  2. int DeQueue(): to delete an element from the front end and returns the element deleted
  3. bool empty(): a Boolean function to check whether the queue is empty or not
  4. int size(): returns no of elements presented in the queue

Queue in STL

The basic ADT operations for queue are available in the STL.

Queue is the container and the available functions for implanting the ADT operations are mentioned following:

  1. empty() – Returns 1 if the queue is empty else 0
  2. size() – Returns the size of the queue
  3. front() – Returns a reference to the first element of the queue (front end)
  4. back() – Returns a reference to the last element of the queue (rear end)
  5. push(g) – Adds the element ‘g’ at the end of the queue (conventional EnQueue(g) ADT operation)
  6. pop() – Deletes the first element of the queue ( similar to DeQueue(), but it only deletes, doesn’t return anything.)

So to complete the DeQueue operation we need to write

  1. int item= front();
  2. pop();
  3. return item;

To declare queue in C++ STL:

    queue<datatype> queue_name;

Example:

    queue<int> q; //a queue of integers

C++ program to illustrate queue in STL

#include <iostream> 
#include <queue>  // include header file for STL

using namespace std; 

int main() 
{ 
	queue <int> q;
	int x,item;

	cout<<"enter integers to EnQueue and 0 to stop EnQuing"<<endl;
	cin>>x;

	while(x){
		q.push(x);       //push(int x) function for EnQueueing
		cin>>x;
	} 

	cout << "The size of the queue is  : "<<q.size()<<endl; //size() function 
	cout<<"The first element that entered the queue is : "<<q.front()<<endl;  //front() function
	cout<<"The last element that entered the queue is : "<<q.back()<<endl;    //back() function	 

	cout<<"DeQueuing......."<<endl;
	while(!q.empty()){
		item=q.front();            // DeQueing
		cout<<item<<" ";
		q.pop();
	}
	
	cout<<"\n executed successfully\n";
	
	return 0; 
}

Output

enter integers to EnQueue and 0 to stop EnQuing
12
4
45
3
6
7
0
The size of the queue is  : 6
The first element that entered the queue is : 12
The last element that entered the queue is : 7
DeQueuing.......
12 4 45 3 6 7
executed successfully





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.