0% found this document useful (0 votes)
56 views37 pages

Queue

Uploaded by

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

Queue

Uploaded by

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

Introduction to Queue ADT

Department of Computer Science Dr. Tahir Maqsood


COMSATS University Islamabad
Lahore Campus
Outline
 Queue Abstract Data Type (ADT)
 Queue ADT with Array Implementation
 Operations
 Insert a new element in the list
 insert at the beginning of the list
 insert at the end of the list
 insert anywhere in the list
 Delete an element from the list
 delete from the beginning of the list
 delete from the end of the list
 delete any element in the list
 Display print the elements of list

Department of Computer Science


What is Queue?
 It is an ordered group of homogeneous items of
elements.
 Queues have two ends:
 Elements are added at one end.
 Elements are removed from the other end.
 The element added first is also removed first
(FIFO: First In, First Out).

Department of Computer Science


QUEUE
 Upshot
 A queue is a linear data structure into which items
can only be inserted at rear and removed from front.
 Difference between Stack and Queue

Stack Queue

Stack is a LIFO (Last In queue is a FIFO (First


First Out) structure In First Out) structure
Stack has one end(Top) Queues have two
ends(Front and Rear)

Department of Computer Science 4


Examples

For example, we queue up while depositing a utility bill or purchasing a


ticket. The objective of that queue is to serve persons in their arrival order;
the first coming person is served first. The person, who comes first, stands at
the start followed by the person coming after him and so on. At the serving
side, the person who has joined the queue first is served first.
teller 1 teller 2 teller 3 teller 4

Department of Computer Science 5


Example

 Example 2: Suppose there are four computers and one


printer is connected. Computer B send print at time
9:30 am which takes 15 minutes for printing, Computer
D send print command at time 9:35 which takes 7
minutes, computer A send print command at 9: 36
which takes 2 minutes and computer C send print
command at 9:37 which takes 30 seconds. How
printers organized these jobs in its buffer?
 In which order
job will prints?

Department of Computer Science


Queue Specification
 Definitions: (provided by the user)
 MAX_ITEMS: Max number of items that might be on
the queue
 ItemType: Data type of the items on the queue

• Operations
– MakeEmpty
– Boolean IsEmpty
– Boolean IsFull
– Enqueue (ItemType newItem)
– Dequeue (ItemType& item)

Department of Computer Science


Enqueue

 Signature: Enqueue (ItemType newItem)


 Function: Adds newItem to the rear of the
queue.
 Preconditions: Queue has been initialized and is
not full.
 Postconditions: newItem is at rear of queue.

Department of Computer Science


Working of enqueue()

 In the above figure, queue implementation using


array is shown.
 As the array size is 8, therefore, the index of the
array will be from 0 to 7.
 The number of elements inside array are 1, 7, 5
and 2, placed at start of the array.
 The front and rear in this implementation are not
pointers but just indexes of arrays.
 front contains the starting index i.e. 0 while rear
comprises 3.
Department of Computer Science 9
Working of enqueue()

Department of Computer Science 10


Working of enqueue()

When an element is removed from the queue. It is removed from the front
index.

After another call of dequeue() function:

Department of Computer Science 11


Working of enqueue()

 With the removal of element from the queue, we are


not shifting the array elements. The shifting of
elements might be an expensive exercise to perform
and the cost is increased with the increase in number
of elements in the array. Therefore, we will leave
them as it is.

Department of Computer Science 12


Insertion Operation

 After insertion of two elements in the queue, the


array that was used to implement it, has reached
its limit as the last location of the array is in use
now.
 isFull() returns true if the number of elements
(noElements) in the array is equal to the size of
the array. Otherwise, it returns false.
 It is the responsibility of the caller of the queue
structure to call isFull() function to confirm that
there is some space left in the queue to
enqueue() more elements.

Department of Computer Science 13


enqueue()

Department of Computer Science 14


dequeue( )

Department of Computer Science 15


display( )

Department of Computer Science 16


enqueue( )

Department of Computer Science 17


dequeue( )

Department of Computer Science 18


display( )

Department of Computer Science 19


Array Implementation of Queues
 To Enqueue an element X, we increment Size and Rear,
then set Queue[Rear]=X.

 To Dequeue an element, we set the return value to


Queue[Front], decrement Size, and then increment
Front.

 Whenever Front or Rear gets to the end of the array, it


is wrapped around to the beginning. This is known as a
circular array implementation.

Department of Computer Science


Circular Queues
 The simple array implementation of queue has a
problem
 It is wasting space
 Because front is moving towards back and leaving empty space
behind.
 Elements are stored towards end of array and front cells may
be empty due to dequeue
To resolve this problem array can be used as a circular
array.
 When we reached at end of array, start from beginning

 Whenever Front or Rear gets to the end of the array, it


is wrapped around to the beginning. This is known as a
circular array implementation.

Department of Computer Science


Circular Queue

Department of Computer Science


Queue Implementation (2)

Department of Computer Science


Circular Array Q Data Structure
 enqueue(Object x) {
 Q[back] = x ;
Q 0 size - 1
 back = (back + 1) % b c d e f
size }
front back

How test for empty list?


How to find K-th element
in the queue?

dequeue() {
x = Q[front] ;
front = (front + 1) % size;
return x ; }

Department of Computer Science


Algorithms

Department of Computer Science


Helper Functions

Helper Queue Circular Queue


Methods

isEmpty() front, back = -1 same

isFull() back+1= N (back+1)%N= front

Size() back-front + 1 (N-front+back)%N + 1


If front<back
back-front + 1
Else
(N-front) +(back+1)

Department of Computer Science


Example Applications
 When jobs are submitted to a printer, they are
arranged in order of arrival.
 Every real-life line is a queue. For instance, lines at
ticket counters are queues, because service is first-
come first-served.
 A whole branch of mathematics, known as
queueing theory, deals with computing,
probabilistically, how long users expect to wait on
a line, how long the line gets, and other such
questions.

Department of Computer Science


Priority Queue
 Imagine a ticket window at Daewoo Terminal, the
person who arrives first will get the ticket before
the person who arrives later.
 Now imagine process queue for CPU, ideally it
should execute the process whose request was
arrived first, but some time situation arise when a
component/process raise request that is of highest
priority and needs to be executed first regardless
of the order of arriving in process queue, like
computer shut down
 That is the case where we need a queue which
can handle priority.

Department of Computer Science


Priority Queue

 A priority queue is a collection of zero or more items,


associated with each item is a priority.
 In a normal queue the enqueue operation add an item at
the back of the queue, and the dequeue operation
removes an item at the front of the queue.
 In priority queue, enqueue and dequeue operations
consider the priorities of items to insert and remove them.
 Priority queue does not follow "first in first out" order in
general.
 The highest priority can be either the most minimum value
or most maximum
 we will assume the highest priority is the minimum.

Department of Computer Science


Priority Queue as ADT

Operations:
 Enqueue(E)
 Dequeue(): remove the item with the highest
priority
 Find() return the item with the highest priority
Examples
Process scheduling
 Few processes have more priority
Job scheduling
 N Jobs with limited resources to complete Patients
treatment in emergency

Department of Computer Science


Priority Queue as ADT

Department of Computer Science


First Approach
Enqueue(3)Enqueue(1)
Enqueue(5) Enqueue(7)
Enqueue(4) Enqueue(2)
Dequeue()
Find highest priority 2
Back Back=3
=5
3 1 5 7 4 2
3 5 7 4

Dequeue()
Find highest priority 1
 Move to left Back=4 Back=3

3 5 7 4 2 5 7 4

Department of Computer Science


Algorithms

Department of Computer Science


Algorithms

Department of Computer Science


Second Approach

Department of Computer Science


Algorithm

Department of Computer Science


Algorithm

Department of Computer Science

You might also like