algorithms

Algorithms Handbook

View on GitHub

Queue

Overview

A Queue is a linear structure which follows a FIFO order for operations.

In a queue, the element deleted is always that one that has been in the set for the longest time: first-in, first-out, or FIFO.

There are several implementations, we will focus on a simple array to implement each.

Application

Queue is used when things don’t have to be processed immediately, but have to be processed in *FIFO order like Breadth First Search. This property of Queue makes it also useful in following other scenarios:

Common Procedures

ENQUEUE(Q, x)
  Q[Q.tail] = x
  if Q.tail == Q.length
    Q.tail = 1
  else Q.tail = Q.tail + 1

DEQUEUE(Q)
  x = Q[Q.head]
  if Q.head == Q.length
    Q.head = 1
  else Q.head = Q.head + 1
  return x

Design & Implementation

Array Implementation

Linked List Implementation

Stack Implementation

Enqueue costly O(N)

This method makes sure the oldest entered element is always at the top of stack 1, so that the dequeue operation just pops from stack1. To put the element at top of stack1, stack2 is used.

Dequeue costly O(N)

This method push new elements at the top of stack1. In dequeue operation, if stack2 is empty, then all the elements are moved to stack2 and finally top of stack2 is returned.

Implementation Examples

Java

java.util provides a Queue interface that extends the Collection interface, and the most common concrete classes that implement it are PriorityQueue and LinkedList (both implementations are not thread safe, PriorityBlockingQueue is one alternative implementation if thread safe implementation is needed)

See more examples in my [Datastructures in Java repositroy]((https://github.com/herrera-ignacio/datastructures-in-java/tree/master/src/main/java/linear/queue).