Queue

Queue

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.

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

Queue Representation

As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure −

As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.

What is Queue?

  • Queue is a linear data structure where the first element is inserted from one end called REAR and deleted from the other end called as FRONT.
  • Front points to the beginning of the queue and Rear points to the end of the queue.
  • Queue follows the FIFO (First - In - First Out) structure.
  • According to its FIFO structure, element inserted first will also be removed first.
  • In a queue, one end is always used to insert data (enqueue) and the other is used to delete data (dequeue), because queue is open at both its ends.
  • The enqueue() and dequeue() are two important functions used in a queue.

Working of Queue

Queue operations work as follows:

Two pointers FRONT and REAR

FRONT track the first element of the queue

REAR track the last element of the queue

initially, set value of FRONT and REAR to -1

  • Enqueue Operation

  1. Check if the queue is full
  2. For the first element, set the value of FRONT to 0
  3. Increase the REAR index by 1
  4. Add the new element in the position pointed to by REAR

  • Dequeue Operation

  1. Check if the queue is empty
  2. Return the value pointed by FRONT
  3. Increase the FRONT index by 1
  4. For the last element, reset the values of FRONT and REAR to -1

Applications of queue

Due to the fact that queue performs actions on first in first out basis which is quite fair for the ordering of actions. There are various applications of queues discussed as below.

1.  Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.

2.  Queues are used in asynchronous transfer of data (where data is not being transferred at the same rate between two processes) for eg. pipes, file IO, sockets.

3.  Queues are used as buffers in most of the applications like MP3 media player, CD player, etc.

4.  Queue are used to maintain the play list in media players in order to add and remove the songs from the play-list.

5. Queues are used in operating systems for handling interrupts.

Operations on Queue

Following are the basic operations performed on a Queue.

Complexity

Popular posts from this blog

DATA STRUCTURES-II

DATA STRUCTURES FOR BCA

DATA STRUCTURES FOR B.TECH