Queue Data Structure

Queue data structure is an abstract data type. We can also call it a collection. It is primarily a linear and sequential data structure. All the items in the queue are in order. The important operations on the queue are to add an element to the rear end position and to delete an element from the front position. The addition operation is termed as enqueuing an item. And deletion operation is termed as dequeuing an item. This is an effectively First In First Out procedure (FIFO). In other words, the first element that goes into the queue becomes the first element to leave the queue. This is the notion of ordering in the queue.

The removal of a particular element depends on the removal of the prior element. This becomes the primary constraint in the queue data structure.

There is another operation which is optional. This is called peeking into the queue. You can peek the front of the queue, if implemented in code. Peek does not remove the element but it just returns the element at the front of the queue.

One of the instances of a queue is a buffer. A buffer holds the data, which can be read from the opposite end. So buffer receives a data from one end and then flushes from the other end. So if you are asked to implement a buffer, then you can do so with a queue. Queues can be implemented using linked lists. There are other types of queue implementations are e.g. circular buffer where fixed size buffer is allocated by connecting it end-to-end. You can then do data streaming on top of it. I will come to the part of data streaming a bit later. For now, just remember that circular buffers are the implementations of queue i.e. FIFO, but non-circular buffers are the implementation of LIFOs. There is another type of queue which is called bounded queue. A bounded queue has a fixed size, which means if it is full then it does not allow add operations, so we need extra constraint checks on top of the queue. Remember the more checks you introduce the more performance overhead comes in.

We can use doubly linked lists to implement queues. In such cases, addition and deletion of items in/from the queue give us O(1) time complexity at each end. Dynamic arrays can be used to implement queues.

Usability (Advantages/Disadvantages)
  • It can be used to maintain a subset of a certain size. E.g. if you want to maintain a subset of 3 elements from the set of 10 elements. An example of it is Simple Moving Averages. Simple moving averages are mostly used in financial data analysis. If you want to maintain a sliding window into a set then it is highly attractive to think about its implementation using Queue data structure.
  • In bounded-buffer problem or producer-consumer problem, a circular queue might be beneficial. Bounded-buffer problem is introduced because of process synchronization problem. This problem comes when the producer tries to write to a fixed-size buffer, while at some other time, a consumer might try to read an empty buffer. To resolve it further you need a process notification mechanism. If we talk about the benefit, then data queues become the fast method for inter-process communication.
  • Queues can have items of any data types. This is a flexibility aspect. You can have a queue of queues, a queue of ints, a queue of strings, a queue of arrays,  or queue of an object of any type.
  • The queue is not readily searchable. You have to start from the end and might have to maintain another queue. So if you have some data, which later on you would want to be searchable, then don’t even think about using a queue.
  • Adding or deleting elements from the middle of the queue is complex as well.
  • Many of the queues are implemented using pointers, so in that respect, many programmers have the notion that they are difficult to create, maintain, and manipulate.
  • The queue takes less space than most of the non-linear data structures. In later, you might have to maintain adjacency matrices, and lists.
  • You can create a queue using only two stacks as well. There are techniques to implement them using one stack as well. You can add the pushed item to the queue, and then rotate the queue. Remember stack only allow push and pop operations. The one stack approach might be costly, and it might require recursive to pop till the end, and then return it as a first element out of a logical queue.
  • They are used to implement highly beneficial data structure called priority queues, where jobs with same priority follow FIFO rules. If you learn the priority queues, you would be able to understand the internals of heaps and complete binary trees.
  • The queue related questions are a favourite in many software development-related interviews, so learning them can land you a job.


Here are some resources to learn more about queues.

Products from Amazon.co.uk