Queue Data Structure in Java

A queue is an ordered list in which insertions are done at one end (rear) and deletions are done at another end (front). The first element to be inserted is the first one to be deleted. Hence, it is called the First in First out (FIFO) or Last in Last out (LILO) list.
A queue is a data structure used for storing data (similar to Linked Lists and Stacks). In a queue, the order in which data arrives is important. In general, a queue is a line of people or things waiting to be served in sequential order starting at the beginning of the line or sequence.

Real World Examples

A real-world example of a queue can be a single-lane one-way road, where the vehicle enters first, and exits first. More real-world examples can be seen as queues at the ticket windows and bus stops.

Queue Concepts

  • When an element is inserted in a queue, the concept is called EnQueue.
  • When an element is removed from the queue, the concept is called DeQueue.
  • DeQueueing an empty queue is called underflow (treated as Exception).
  • EnQueuing an element in a full queue is called overflow (treated as an Exception).

Queue Operations

Let's list out basic operations associated with queues −
  • enqueue() − add (store) an item to the queue.
  • dequeue() − remove (access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation efficient. These are −
  • peek() − Gets the element at the front of the queue without removing it.
  • isFull() − Checks if the queue is full.
  • isEmpty() − Checks if the queue is empty.
In a queue, we always dequeue (or access) data, pointed by a front pointer and while enqueuing (or storing) data in the queue we take the help of the rear pointer.

Enqueue Operation

Queues maintain two data pointers, front, and rear. Therefore, its operations are comparatively more difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce an overflow error and exit.
Step 3 − If the queue is not full, increment a rear pointer to point to the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Step 5 − Return success.

Dequeue Operation

Accessing data from the queue is a process of two tasks − access the data where the front is pointing and remove the data after access. The following steps are taken to perform the dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce an underflow error and exit.
Step 3 − If the queue is not empty, access the data where the front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Step 5 − Return success.

Exceptions

Similar to other Queue Operations, executing DeQueue on an empty queue throws an “Empty Queue Exception” and executing EnQueueon a full queue throws a “Full Queue Exception”.

Applications of Queue

Following are some of the applications that use queues.

Direct Applications

  • Operating systems schedule jobs (with equal priority) in the order of arrival (e.g., a print queue).
  • Simulation of real-world queues such as lines at a ticket counter, or any other first-come-the-served scenario requires a queue.
  • Multiprogramming.
  • Asynchronous data transfer (file IO, pipes, sockets).
  • Waiting times of customers at a call center.
  • Determining the number of cashiers to have at a supermarket.

Indirect Applications

  • Auxiliary data structure for algorithms
  • Component of other data structures

Queue Implementation in Java

There are many ways (similar to Stacks) of implementing queue operations and some of the commonly used methods are listed below.
  1. Queue Implementation using Circular Array in Java
  2. Dynamic Queue Implementation using Array
  3. Queue Implementation using Linked List in Java

Comments