Amazon Prime




 
Tagged
  • Data Structure
  • Queue
  •  

    Queue

    Introduction

    Queue is a linear data structure to store and manipulate data which follows First In First Out (FIFO) order during adding and removing elements in it.

    For instance if we goto any ticket counter, there will be two open endpoints (or gates), one end point is called front and the other is called back (or rear). At the front gate tickets will be issued and the person who is near the front gate will collect the ticket and leave the premises. If we notice, whoever enters first will leave first. This is called First In First Out (FIFO) order. Here people can enter using only one point which is the back gate and they will collect tickets and leave from the other gate called front.

     

    Ticket Counter – Queue Example

    Let us understand the concept of Queue using the Ticket Counter example as shown in the picture.

    • There are two open endpoints, one is called front of the queue and the other is called back of the queue.
    • Tickets will be issued by an authorized person at the front gate.
    • Persons who want to buy tickets will enter the premises using the back gate.
    • Person will stand in a row (which forms a queue).
    • Person who is at the front gate gets a chance to buy a ticket first and come out of the queue.
    • As per example shown in (Ticket Counter – Queue Example) picture, P1 purchased ticket and leaving the premises
    • As P2 is near the front gate, he gets a chance to buy tickets.
    • P5 is about to enter the queue, and P5 gets a chance to buy a ticket only if all the people standing before him (P4, P3, P2) get the tickets.
    • P5 is about to enter the queue, and P5 gets a chance to buy a ticket only if all the people standing before him (P4, P3, P2) get the tickets. P5 will leave at last as he is the last person in the queue, It means whoever enters Last, will Leave last. This is also known as Last In Last Out (LILO) order.

    Operations

    Queue is an abstract data type which allows following operations :

    • Enqueue : Add element into the queue. Enqueue operation always takes place at the back or rear point.
    • Dequeue : Remove element from the queue. Dequeue operation always takes place at front point
    • IsEmpty : Check if there is any element in the queue.
    • Peek : Returns the element at the front of the queue.

    Let us see how we can relate queue operations using the Ticket Counter example.

    • Enqueue : Person entering into the queue at back gate.
    • Dequeue : Person collects ticket leaving the queue from the front gate.
    • IsEmpty : It checks any person waiting for tickets in the queue?
    • Peek : gets the person at the front gate of the queue. The person who gets a chance to buy a ticket.

    Applications

    In real life we see many examples of queue data structure like Ticket Counter, People waiting in a queue at ATM centres etc. Similarly to build few applications, queue data structure is used, and few of the use cases are as follows:

    • Customer Care Systems : Whenever we call/message any customer care system, our call is almost in the waiting state and we will see what the current waiting number is. So internally when we make a call, it will be added to the queue until the customer care executive is free to serve us.
    • Messaging Queue Systems : In messaging systems, messages will be stored in a queue until they are processed and removed. Examples : Apache Kafka, RabbitMq etc.
    • Process Schedulers: Process schedulers like round robin load balancing technique, queue data structure is being used. In this technique, once an item in queue is processed, the item gets removed and added back to the queue which will inturn gets chance again.
     

  • Data Structure
  • Queue
  •  
    ...