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

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.

Suppose We have a queue that has FRONT = 0 and REAR = 5. And we want to add another element with value 45, then we check REAR not equal to MAX – 1 if condition false REAR would be incremented by 1 and the value would be stored at the position pointed by REAR. After addition of new element queue will look like FRONT = 0 and REAR = 6. Every time whenever a new element has to be added, the same procedure will be repeated.

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.

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.

display

Print all element of Queue.


[wpusb]