0% found this document useful (0 votes)
37 views31 pages

Unit VI Queues

Uploaded by

yedegog697
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views31 pages

Unit VI Queues

Uploaded by

yedegog697
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Queues

Contents
• Basic concept: Queue as Abstract Data Type,
Representation of Queue using Sequential
organization, Queue Operations, Circular Queue and
its advantages, Multi-queues, Linked Queue and
Operations.
• Deque: Basic concept, types (Input restricted and
Output restricted), Priority Queue- Basic concept,
types (Ascending and Descending).
• Case Study: Priority queue in bandwidth
management
Quque
Quque
Queues
• Linear list.
• One end is called front.
• Other end is called rear.
• Additions are done at the rear only.
• Removals are made from the front only
• This mechanism is called First-In-First-Out
(FIFO).
Queues
• Placing an item in a queue is called “insertion
or enqueue”, which is done at the end of the
queue called “rear”.
• Removing an item from a queue is called
“deletion or dequeue”, which is done at the
other end of the queue called “front”.
• Some of the applications are: printer queue,
keystroke queue, etc.
Queue Operations
– IsEmpty … return true iff queue is empty
– Front … return front element of queue
– Rear … return rear element of queue
– Push/enqueue … add an element at the rear of the
queue
– Pop /dequeue… delete the front element of the
queue
Queue as ADT
• A queue is an ordered collection of items which are added at one end,
called the “rear,” and removed from the other end, called the “front.”
Queues maintain a FIFO ordering property. The queue operations are
given below.
• Queue() creates a new queue that is empty. It needs no parameters and
returns an empty queue.
• enqueue(item) adds a new item to the rear of the queue. It needs the
item and returns nothing.
• dequeue() removes the front item from the queue. It needs no
parameters and returns the item. The queue is modified.
• isEmpty() tests to see whether the queue is empty. It needs no
parameters and returns a boolean value.
• size() returns the number of items in the queue. It needs no parameters
and returns an integer.
Queue in an Array
– Use a 1D array to represent a queue.
– Suppose queue elements are stored with the front
element in queue[0], the next in queue[1], and so
on.
Queue in an Array
n-1 3 2 1 0
D C B A

Max_Size rear front

After A leaves,
n-1 3 2 1 0
D C B

Max_Size rear front


Problem
• An array has limited size, once rear is at the
end of this array, and there is new item
coming in, what can we do?
Solution
• Shifting all items to front in the array when
dequeue operation. ( Too Costly… )

n-1 3 2 1 0 A leaves n-1 3 2 1 0


…… C BA …… C B

rear=3 front=0 rear=2 front=0

• Wrapped around array ---- Circular Array


Circular quque
• When a new item is inserted at the rear, the
pointer to rear moves upwards.
• Similarly, when an item is deleted from the
queue the front arrow moves downwards.
• After a few insert and delete operations the
rear might reach the end of the queue and no
more items can be inserted although the
items from the front of the queue have been
deleted and there is space in the queue.
Circular queue
• To solve this problem, queues implement
wrapping around. Such queues are called
Circular Queues.
• Both the front and the rear pointers wrap
around to the beginning of the array.
• It is also called as “Ring buffer”.
• Items can inserted and deleted from a queue
in O(1) time.
Circular Queue
• The circular queue work as follows:
• two pointers FRONT and REAR
• FRONT track the first element of the
queue
• REAR track the last elements of the
queue
• initially, set value
of FRONT and REAR to -1
Operations on circular queue
Enqueue()

Dequeue()
Remedies
• Remedies.
– Don’t let the queue get full.
• When the addition of an element will cause the queue to be
full, increase array size.

– Define a boolean variable lastOperationIsPush.


• Following each push set this variable to true.
• Following each pop set to false.
• Queue is empty iff (front == rear) && !lastOperationIsPush
• Queue is full iff (front == rear) && lastOperationIsPush
Remedies.
• Remedies (continued).
– Define an integer variable size.
• Following each push do size++.
• Following each pop do size--.
• Queue is empty iff (size == 0)
• Queue is full iff (size == arrayLength)
Various Queues
• Normal queue (FIFO)
• Circular Queue (Normal Queue)
• Double-ended Queue (Deque)
• Priority Queue
Deque
• It is a double-ended queue.
• Items can be inserted and deleted from either
ends.
• More versatile data structure than stack or
queue.
• E.g. policy-based application (e.g. low priority
go to the end, high go to the front)
Input restricted double ended queue
• Insertion is allowed at only one end while
deletion is allowed at both ends.
Output restricted double ended queue
• Deletion is allowed at only one end while
Insertion is allowed at both ends
Priority Queues
• More specialized data structure.
• Similar to Queue, having front and rear.
• Items are removed from the front.
• Items are ordered by key value so that the item
with the lowest key (or highest) is always at the
front.
• Items are inserted in proper position to
maintain the order.
• If two elements have the same priority, they are
served according to their order in the queue.
Priority Queues
• Main operations
insert (i.e., enqueue)
Dynamic insert
specification of a priority level (0-high, 1,2.. Low)
deleteMin (i.e., dequeue)
Finds the current minimum element (read:
“highest priority”) in the queue, deletes it from the
queue, and returns it
Performance goal is for operations to be “fast”
Implementations
• Unordered linked list
• O(1) insert
• deleteMinO(n)
• Ordered linked list
• O(n) insert
• O(1) deleteMin
• Ordered array
• O(lg n + n) insert
• O(n) deleteMin
• Balanced BST
• O(log2n) insert and deleteMin
Heaps
Multi queues
• Multi queue is data structure in which
multiple queues are maintained

You might also like