Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

IT2153/IT2352/IT2553/IT2653/IT2852

Data Structures & Algorithms


Topic 09: Queues
version 1.0

(Content adapted from – Data Structures & Algorithms using Python.


Rance D. Necaise, Wiley, 1st Edition, 2011.)

Topic 09: Queues AY2022/23 S1


Queues
 What is a Queue?
 Queue ADT
 Queue Implementation
 with a Python List (Efficiency?)
 with a Circular Array

Topic 09: Queues AY2022/23 S1


What is a Queue?
 Queue is a linear data structure that has
restrictions on how items are added to
and removed from it.
 Items can only be added from one end (back)
and removed from the other end (front)
 Known as first-in, first-out (FIFO)
 Enqueue – adds an item to the back of the
queue
 Dequeue – removes and returns the front
item from the queue

Topic 09: Queues AY2022/23 S1


What is a Queue?
Stackis a data structure of ordered items such that
A queue

items can be added at one end (called the back) and
 Items are inserted and removed only at one
removed
end from thethe
(called other end (called the front).
top).
 Queue
 Items are inserted at one end and can only
be removed from the other end.

Top front back


D A
B
C
D
C
B
A
Queue
Topic 09: Queues Stack AY2022/23 S1
The Queue ADT
 ADT – Abstract Data Types
 A type (or class) of objects whose behaviour
is defined by a set of values and a set of
operations.

 E.g. Stack ADT, Queue ADT, Tree ADT etc.

5
Topic 09: Queues AY2022/23 S1
The Queue ADT

6
Topic 09: Queues AY2022/23 S1
Abstract View of the Queue

Topic 09: Queues AY2022/23 S1


Queue Implementation (with a Python List)

# Implementation of the Queue ADT using a Python list.


class Queue:
# Creates an empty queue.
def __init__( self ):

# Returns True if the queue is empty.


def isEmpty( self ):

# Returns the number of items in the queue.


def __len__( self ):

Topic 09: Queues AY2022/23 S1


Queue Implementation (with a Python List)

# Adds the given item to the queue.


def enqueue( self, item ):

# Removes and returns the first item in the queue.


def dequeue( self ):
assert not self.isEmpty(), \
"Cannot dequeue from an empty queue."

Topic 09: Queues AY2022/23 S1


Efficiency of Queue using Python
List
 What happened
when an item is
inserted to a
Python List
which is
already “full”?

Topic 09: Queues AY2022/23 S1


Efficiency of Queue using Python
List
 enqueue operation
 Requires O(n) time in the worst case since the
list may need to expand to accommodate the
new item.
 dequeue operation
 Requires O(n) time since the underlying array
used to implement the Python list may need to
shrink when an item is removed, and
 When an item is removed from the front of the
list, the following items have to be shifted
forward.
 Can this be improved? Circular
Array?
Topic 09: Queues AY2022/23 S1
Circular Array
 A circular array is simply an array
viewed as a circle instead of a line.

 
Abstract Physical
View View

14
Topic 09: Queues AY2022/23 S1
Circular Array
 Advantage:
 Allows us to add new items to a queue and
remove existing ones without having to
shift items in the process.
 Disadvantage:
 Concept of a maximum-capacity queue
that can become full.

15
Topic 09: Queues AY2022/23 S1
Queue Implementation (with a
Circular Array)
 Implementation Details:
 count  number of items in the queue
 front  array element containing the first item
 back  array element containing the last item

Topic 09: Queues AY2022/23 S1


Queue Implementation (with a
Circular Array)
 enqueue operation:
 Item is inserted in the position immediately
following the back marker
 back marker advanced one position
 count incremented by 1

Topic 09: Queues AY2022/23 S1


Queue Implementation (with a
Circular Array)
 dequeue operation:
 value in the element marked by front will be
returned
 front marker advanced one position
 count decremented by 1

Topic 09: Queues AY2022/23 S1


Queue Implementation (with a
Circular Array)
 Add 8 and 23:

 What happens if we add 39 next?

Topic 09: Queues AY2022/23 S1


Queue Implementation (with a
Circular Array)
 Add 39:

 Queue wraps around the circular array


when items are added and removed.
 Eliminates the need to shift items.
Topic 09: Queues AY2022/23 S1

You might also like