Queue implementation in C++

A queue is a linear data structure that serves as a container of objects that are inserted & removed according to the FIFO (first-in first-out) principle.

Queue has three main operations: enqueue, dequeue and peek. We have discussed about these operations in previous post and also covered C implementation of queue data structure using an array and linked list. In this post, we will cover queue implementation in C++ using class and STL.

Below queue implementation in C++ covers below operation –

  • enqueue: Inserts a new element at the rear of the queue.
  • dequeue: Removes the front element of the queue.
  • peek: Returns the front element present in the queue without dequeuing it.
  • isEmpty: Checks if the queue is empty.
  • isFull: Checks if the queue is full.
  • size: Returns the number of elements present in the queue.


Download   Run Code


Inserting 1
Inserting 2
Inserting 3
Front element is: 1
Removing 1
Inserting 4
Queue size is 3
Removing 2
Removing 3
Removing 4
Queue Is Empty


The time complexity of all above queue operations is O(1).



C++’s STL provides a std::queue templated class which is restricted to only enqueue/dequeue operations. It also provides std::list whcih have push_back and pop_front operations with LIFO semantics. Java’s library contains a Queue interface that specifies queue operations.


Download   Run Code


Download   Run Code


Queue size is 4
Front element is: A
Rear element is: D
Queue size is 2
Queue is not Empty

Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of