implementing queue in c++ and java using inbuild classes

 Queue in C++ STL:

 C++  stl provides a built-in implementation of the Queue data structure.


queue<data_type > queue_name;

The functions supported by std::queue are :

empty() – Returns whether the queue is empty.

size() – Returns the size of the queue.

swap(): Exchange the contents of two queues but the queues must be of same type, although sizes may differ.

emplace(): Insert a new element into the queue container, the new element is added to the end of the queue.

front() and back(): front() function returns a reference to the first element of the queue. back() function returns a reference to the last element of the queue.

push(g) and pop(): The push() function adds the element ‘g’ at the end of the queue. The pop() function deletes the first element of the queue.

c++ :

// Queue in Standard Template Library (STL) 
#include <iostream> 
#include <queue> 

using namespace std; 

void show(queue <int> q) 
    while (!q.empty()) 
        cout << '\t' << q.front(); 
    cout << '\n'; 

int main() 
    queue <int>q;
    cout << "The queue q is: "; 

    cout << "\q.size() : " << q.size(); 
    cout << "\q.front() : " << q.front(); 
    cout << "\q.back() : " << q.back(); 

    cout << "\q.pop() : "; 
    q.pop();  // 5 is poped out here 
    return 0; 

The queue q is : 5 2 32

q.size() : 3
q.front() : 5
q.back() : 32
q.pop() : 2 32

Queue interface in Java :

The Queue interface is available in java.util package and extends the Collection interface. The queue collection is used to hold the elements about to be processed and provides various operations like the insertion, removal etc. It is an ordered list of objects with its use limited to insert elements at the end of the list and deleting elements from the start of list i.e. it follows the FIFO or the First-In-First-Out principle. Being an interface the queue needs a concrete class for the declaration and the most commonly used classes are the PriorityQueue and LinkedList in Java. It is to be noted that both the implementations are not thread safe. PriorityBlockingQueue is one alternative implementation if thread safe implementation is needed.

Methods in Queue:
add()- This method is used to add elements at the tail of the queue. More specifically, at the last of linkedlist if it is used, or according to the priority in case of priority queue implementation.
peek()- This method is used to view the head of a queue without removing it. It returns Null if the queue is empty.
element()- This method is similar to peek(). It throws NoSuchElementException when the queue is empty.
remove()- This method removes and returns the head of the queue. It throws NoSuchElementException when the queue is empty.
poll()- This method removes and returns the head of the queue. It returns null if the queue is empty.
size()- This method returns the no. of elements in the queue.


// Java orogram to demonstrate working of Queue 
// interface in Java 
import java.util.LinkedList; 
import java.util.Queue; 

public class QueueExample 
    public static void main(String[] args) 
        Queue<Integer> q = new LinkedList<>(); 

        // Adds elements {0, 1, 2, 3, 4} to queue 
        for (int i=0; i<5; i++) 

        // Display contents of the queue. 
        System.out.println("Elements of queue-"+q); 

        // To remove the head of queue. 
        int removedele = q.remove(); 
        System.out.println("removed element-" + removedele); 


        // To view the head of queue 
        int head = q.peek(); 
        System.out.println("head of queue-" + head); 

        // Rest all methods of collection interface, 
        // Like size and contains can be used with this 
        // implementation. 
        int size = q.size(); 
        System.out.println("Size of queue-" + size); 
Elements of queue-[0, 1, 2, 3, 4]
removed element-0
[1, 2, 3, 4]
head of queue-1
Size of queue-4