Operations on Queue in Data Structures


Queue is First In First Out data structure. The queue is used in different area for graph traversal algorithms Breadth First Search etc. The queue has some primitive operations. Here we will see those operations of queue, and see one example using the queue ADT.

The ADT (abstract datatype) is special kind of datatype, whose behavior is defined by a set of values and set of operations. The keyword “Abstract” is used as we can use these datatypes, we can perform different operations. But how those operations are working that is totally hidden from the user. The ADT is made of with primitive datatypes, but operation logics are hidden.

These are few operations or functions of the queue ADT.

  • isFull(), This is used to check whether queue is full or not
  • isEmpry(), This is used to check whether queue is empty or not
  • enqueue(x), This is used to push x at the end of the queue
  • delete(), This is used to delete one element from the front end of the
  • front(), This is used to get the front most element of the queue
  • size(), this function is used to get number of elements present into the queue

Example

 Live Demo

#include<iostream>
#include<queue>
using namespace std;
main(){
   queue<int> que;
   if(que.empty()){
      cout << "Queue is empty" << endl;
   } else {
      cout << "Queue is not empty" << endl;
   }
   //insert elements into queue
   que.push(10);
   que.push(20);
   que.push(30);
   que.push(40);
   que.push(50);
   cout << "Size of the queue: " << que.size() << endl;
   //delete and dispay elements
   while(!que.empty()) {
      int item = que.front(); // read element from first position
      que.pop();
      cout << item << " ";
   }
}

Output

Queue is empty
Size of the queue: 5
10 20 30 40 50

Updated on: 27-Aug-2019

480 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements