You are on page 1of 9

DATA STRUCTURES AND

ALGORITHM
Queues
QUEUES

 As the name implies: a first-in,first-out(FIFO) list


 Items can only be inserted at the backend deleted from
the front
QUEUES - BASIC OPERATIONS
 enqueue(el): put the element el at the end of the list.
 dequeue(): returns the first element in the queue.

 isEmpty(): checks if the queue is empty


QUEUES - IMPLEMENTATION

 Linked List Implementation


 enqueue: We insert elements at the tail
 dequeue: We delete elements from the head
QUEUES - IMPLEMENTATION(2)
 Array Implementation
 We save the “front” and “back” of the queue in the array.

 enqueue(item): we increment “back” and set


array[back]=item
 dequeue(): we return array[front] and increment “front”
QUEUES - IMPLEMENTATION(3)
 Array Implementation Drawback
 After several elements have been enqueued and dequeued,
the queue may appear to be full, since “back” is now at the
last array index
 However, there might only be a few elements in the queue.
QUEUES - IMPLEMENTATION(4)
 “Circular” Array Implementation
 Whenever front or back gets to the end of the array, it is
wrapped around to the beginning.

 This requires a little extra code.


QUEUES - APPLICATIONS

 In software application where a single resource has to be


shared
 Printers, Storage Devices ...Example: if multiple programs
send print jobs to the printer one after the other, their request
is queued since the printer cannot server the requests all at
once.
 Using queueing theory to model and
 simulate
 Customer service in banks and other places where there are
queues.
 Traffic in communication networks
PRIORITY QUEUE

• Much like an ordinary


queue
• Items are removed from
the front
• But, Items are NOT
added at the back higher
priority items are
inserted at the front

You might also like