Queue in C++ With Examples


We worked on the working as well as the implementation of a Stack in C++ in our previous tutorial. Today in this tutorial, we are going to discuss another data structure, Queue in C++.

What is a Queue?

Basically, a queue is also a linear data structure that follows the First In First Out (FIFO) pattern in the insertion or deletion of data. As far as the pattern is concerned, the first element inserted is deleted first, and the last one to enter the queue is deleted at the last.

Unlike stack, Queue operations take place on both sides. But, note that one operation should be performed on one end, and the other on the other end. Hence, both insertion and deletion operations do not take place on the same side.

Working of Queue in C++

A Queue is analogous to a real-world queue. It works on a first come first serve basis. Hence as mentioned earlier, the first element to enter the queue is deleted first, and the last one to enter is deleted after all the previous members are already deleted. Now let us take a look at the basic operations that can be performed on a queue,

In the next section, we are going to elaborate on the above-mentioned techniques.

1. enqueue() in Queue

enqueue() does the insertion of an element into the queue. It is simply done by adding the element at the end of the queue. So, as we can infer, this operation is performed at the end.

The Algorithm for enqueue is given below,

2. dequeue() in Queue

On the other hand dequeue() removes or deletes and accesses the first element present in the queue. Note, this element is the one which was inserted before all the other ones hence is the first one to get dequeued. This dequeue operation occurs at the front of the present queue. Hence, the opposite side to the one where enqueue was performed.

The Algorithm for dequeue is given below,

3. Show

The show is basically the operation in which the corresponding elements of the queue is shown to the user or in other words, is printed. This allows the user to check the current status of the queue at any point in time.

The Algorithm for show is given below,

Implementation of a Queue in C++

Now let us implement the whole concept of a Queue in C++, this will give us a clear understanding of its working.



Queue in C++ Output

Understanding the code,

  • Firstly have initially declared two variables, front=-1 and rear=-1, to signify the queue is empty. The size of the queue is stored inside the constant max
  • enqueue() – as the name suggests, we use this function to perform the enqueue operation. It adds the passed value to the queue at the rear position
  • dequeue() – similarly is used to remove or access the front element from the queue. This function returns the first value removed from the queue
  • show() – This function prints the whole queue, all of its elements. Starting from the front up to the rear.

As we saw in our stack tutorial, the repetitive checking the top was important to avoid errors. Similarly in case of a queue too, we need to check whether the given queue is full or empty and print OVERFLOW and UNDERFLOW errors respectively.


Queues find application in various programming methods and also are used to solve many real-time problems. Some of them are,

  1. CPU sharing
  2. Breadth-First Search algorithm
  3. Waiting lists
  4. IO buffers,
  5. File IO
  6. Keyboard Buffer
  7. And many more…


In this tutorial, we have discussed the concept of a queue in C++ as well as the different operations that we perform on it. We have also taken the various applications of a Queue into consideration.


By admin

Leave a Reply

%d bloggers like this: