Queue Data Structure and Its Operations

What is Queue ?

  • A Queue is a linear data structure in which the elements are added from the rear and removed from the front.
  • Hence, A Queue is also called a FIFO (First-In, First-Out) data structure.
  • FIFO means the element that was inserted first is the first one to be taken out and the element that was inserted Last is the last one to be taken out.

The process of inserting an element in the queue is called enqueue, and the process of deleting an element from the queue is called dequeue.

For example:

At Bus Stop, People is in queue and waiting for a bus. The person who is standing in the line at the first position will go first into the bus. And people who is at last in the line will go in last into the bus.
Similarly People standing outside the ticket counter of a cinema hall. The person who is first in the line will get the ticket first and then second will get and then third so on.

Basic Queue Operations


1). enqueue

2). dequeue

3). peek

4). display

1). enqueue

  • Using enqueue operation Individual items can be added to a queue from the rear.
  • When we add an item to a queue, we must check for overflow conditions.
  • An overflow is a condition that occurs when an item is inserted into a queue that is already full.
  • When REAR = MAX – 1, where MAX is the size of the queue, we have an overflow condition. Note that we have written MAX – 1 because the index starts from 0.

Example

Imagine you’re in a line (queue) to buy tickets. As new people arrive, they join the end of the line. Each person added to the line is like performing an enqueue operation in a queue. The first person to join the line will be the first to buy a ticket and leave the line, demonstrating the First In, First Out (FIFO) principle of queues.

2). dequeue

  • Using dequeue operation Individual items can be deleted from the queue from the front.
  • When we add any remove any item from a queue, we must check for underflow conditions.
  • Underflow is a condition that occurs when an item is queue is empty.
  • When FRONT = –1 and REAR = –1, it means there is no element in the queue.
  • if the queue has some values, FRONT is incremented so that it now points to the next value in the queue.

Example

Dequeue is the operation of removing an element from the front of a queue. Using the same analogy of a line for buying tickets: when it’s someone’s turn, they leave the front of the line to buy their ticket. This action of leaving the line is like performing a dequeue operation in a queue. The first person who joined the line (or was enqueued) will be the first to leave it (or be dequeued), maintaining the First In, First Out (FIFO) principle of queues.

3). peek

  • Using the peek operation front element of the Queue will be retrieved without deleting it.
  • However, the Peek operation first checks if the peek is empty, i.e., if FRONT = –1 and REAR = –1, then an appropriate message is printed, else the value is returned.

Example

Imagine you’re at a coffee shop, and there’s a line of orders waiting to be prepared. Each order is placed in a queue. The barista uses a screen to “peek” at the next order to know what coffee to make next without removing it from the list. This way, the barista can prepare without losing track of the order sequence. The peek operation allows for a quick check to ensure that the next action (making the coffee) is based on the correct order, aligning with the principle of serving customers in the order they were placed, ensuring fairness and efficiency.

4). display

Print all element of Queue.

Example


A real-time example of the display operation can be seen in flight information display systems at airports. These systems show a list of flights, including arrivals and departures, along with their statuses, gates, and times.

When passengers look at the display, they see the entire list or a portion of it, providing them with up-to-date information about their flights. This act of looking at the flight information on screens is similar to the display operation in data structures, where the contents of a structure, like a list or queue, are shown to the user. Just as passengers use the display to plan their actions (like heading to the gate), a program can use display operations to understand the state of a data structure and decide the next steps