Queue Data Structure: Practical Applications & Operations

Learn how queues are used in real applications and it’s implementation in Python and C++

Queue is one of the simplest and most useful data structure, not just in computer science, queues are relevant in our day to day life in lot of different fields.

Watch the video version if you prefer that !

Practical Applications

So what makes queues special ? A queue is a data structure that follows First In First Out — which means the first item to enter the queue is the first to leave the queue.

  • Process Scheduling – All the processes running on your computer now, first wait in a queue called ready queue, waiting to be executed by your processor. There are various scheduling algorithms that decide which process should be executed next based on certain criteria like priority.
  • Data Buffers – A buffer implements a queue and is typically used in communication when there is difference between the rate at which data is received and the rate at which it can be processed.
    For example in video streaming applications, the video is streamed in bursts and stored in a buffer so that even if the network becomes slow for a while, the playback is not interrupted. Say for example the video is playing at 24fps, then the streaming app may store 240 frames in its buffer, so that it can continue playing for the next 10 seconds even if the network is interrupted. The buffer is also used for sequencing frames, ie, if frames come out of order, they are sorted in the buffer before being played. Buffers also used in disk drives, input/output devices and communication over networks.
  • It is also used in several algorithms like the Breadth First Search or BFS algorithm, and round robin algorithm.

Queue Operations

Queue operations
  • Dequeue – removing an element from a queue is call dequeue. On calling dequeue, the element from the front of the queue (more generally, the opposite end where enqueue was performed) is removed. In the example, 15 is at the front, and on dequeuing it is removed and the value is returned.
  • Front – this function simply returns the value at the front of the queue. This will be the element that will be dequeued next.
  • Back/Rear – this function returns the value at the back of the queue. This will be the element that was just added to the queue.

Implementing Queue

Here are common implementations of queue in Python and C++, especially useful if you are preparing for coding interviews.

Python

In python the inbuilt list data structure can be used to implement a queue.

C++

In C++, the STL (Standard Template Library) already has an implementation of queue.

I make websites and teach machines to predict stuff. I also make YouTube videos — https://www.youtube.com/adarshmenon