Unit 2 - QUEUE
Unit 2 - QUEUE
QUEUE
Introduction
• It is a non primitive data structure, designed with
restricted random access.
• This also stores data in continuous memory locations.
• Even though it seems like arrays and stacks, it has its
own uniqueness in organizing and accessing data.
• This is the most common data structure that can be
seen in our day to day life.
• Queue is an abstract data structure, somewhat similar
to stack.
• In contrast to stack, queue is opened at both end.
• 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 queue is a data structure that a linear collection of items in which
access is restricted to a first-in first-out basis.
• New items are inserted at the back and existing items are removed
from the front.
• The items are maintained in the order in which they are added to the
structure.
• A new element is added at one end
called rear end and the existing
elements are deleted from the other
end called front end.
• Enqueue is done at the front of the
queue and dequeue is done at the end
of the queue.
• Total no of elements
in queue= rear-front+1
• Queue operations may involve initializing or defining the queue,
utilizing it and then completing erasing it from memory.
• Here we shall try to understand 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 above mentioned queue
operation efficient. These are
peek − get the element at front of the queue without removing
it.
isfull − checks if queue is full.
isempty − checks if queue is empty.
• In queue, we always dequeue or access data, pointed by front
pointer and while enqueing or storing data in queue we take help
of rear pointer.
• The practical examples of queues are
– The consumer who comes first to a shop will be
served first.
– CPU task scheduling and disk scheduling.
– Waiting list of tickets in case of bus and train
tickets.
Usage Scenario
FRONT REAR
BUS STOP
Usage Scenario
FRONT REAR
BUS STOP
Usage Scenario
FRONT REAR
BUS STOP
Usage Scenario
FRONT REAR
BUS STOP
Types of Queue
Queue in data structure is of the following types
1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Deque (Double Ended Queue)
1. Single ended Queue (Simple)
• A simple queue is the most basic queue. In this queue, the
enqueue operation takes place at the rear, while the
dequeue operation takes place at the front:
• It’s used to switch on and off the lights of the traffic signal systems. Apart
from that, it can be also used in place of a simple queue in all the
applications mentioned above.
3. Priority Queue
• A priority queue is a special kind of queue in which each item has a
predefined priority of service. In this queue, the enqueue operation takes
place at the rear in the order of arrival of the items, while the dequeue
operation takes place at the front based on the priority of the items.
• That is to say that an item with a high priority will be dequeued before an
item with a low priority.
• In the case, when two or more items have the same priority, then they’ll be
dequeued in the order of their arrival. Hence, it may or may not strictly
follow the FIFO rule: