Different Types of Queue

Aug 20, 2022dsadata-structures

Queue is an abstract data structure specifically designed to operate in a FIFO context, where elements are inserted into one end of the container and extracted from the other.

Queue supports at least following operations:

  • peek() returns the value of the next element to be dequeued without dequeuing it.
  • enqueue() adds an element to the back of the queue.
  • dequeue() removes an element from the front.
  • isEmpty() checks if queue is empty.
  • size() return number of elements in queue.

Queues are often implemented using linked lists or dynamic arrays, can perform two core operations enqueue() and dequeue() in O(1)O(1) time.

Regular Queue

In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principle (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure.

In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.

More info: Wikipedia, Youtube

Priority Queue

In computer science, a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a “priority” associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.

While priority queues are often implemented with heaps, they are conceptually distinct from heaps. A priority queue is an abstract concept like “a list” or “a map”; just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods such as an unordered array.

More info: Wikipedia, Youtube

Double-ended Queue (Deque)

Deques are sequence containers with dynamic sizes, for which elements can be added to or removed from either the front (head) or back (tail).

There are at least two common ways to efficiently implement a deque: with a modified dynamic array or with a doubly linked list.

In a doubly-linked list implementation and assuming no allocation/deallocation overhead, the time complexity of all deque operations is O(1). Additionally, the time complexity of insertion or deletion in the middle, given an iterator, is O(1); however, the time complexity of random access by index is O(n).

In a growing array, the amortized time complexity of all deque operations is O(1). Additionally, the time complexity of random access by index is O(1); but the time complexity of insertion or deletion in the middle is O(n).

More info: Wikipedia

Circular Queue

A circular queue is an extended version of a linear queue as it follows the First In First Out principle with the exception that it connects the last node of a queue to its first by forming a circular link. Hence, it is also called a Ring Buffer.

Implementation of a linear queue brings the drawback of memory wastage. However, memory is a crucial resource that you should always protect by analyzing all the implications while designing algorithms or solutions. In the case of a linear queue, when the rear pointer reaches the MaxSize of a queue, there might be a possibility that after a certain number of dequeue() operations, it will create an empty space at the start of a queue.

Applications:

  • Memory Management: The unused memory locations in the case of ordinary queues can be utilized in circular queues.
  • Traffic system: In computer controlled traffic system, circular queues are used to switch on the traffic lights one by one repeatedly as per the time set.
  • CPU Scheduling: Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur.

More info: Simplilearn, Geeksforgeeks, Programiz, Wikipedia