Professional Documents
Culture Documents
Introduction to Foundations
of Computing
Queue, bounded Queue, Circular Queue
Post-test
Stack 1 2 3 Queue 1 3 6
Search 6 7 8 Search 2 5 8
10 11 9 4 7 10
F F
February 9, 2022 © Osmar R. Zaïane : University of Alberta 7
Queue Example - Maze
Trace
S
0 11 9 P10
P0
P1
P2
P3
P4
P5
P6
P7
P8
P9
1 3 6
Front
2 5 8 P7
P3
P4
P5
P6 Success!
P10
P2
P4
P5
P6
P7
P8
P9
4 7 10
F P10
P11
P1
P3
P5
P6
P7
P8
P9
Rear
q=Queue() q= Queue()
q.enqueue("bob") q.enqueue('blue')
q.enqueue("eva") q.enqueue('red')
q.enqueue("paul") while not(q.dequeue() == 'red'):
if (q.dequeue() == 'eva'): q.enqueue('green')
q.dequeue() q.show()
q.show()
self._items = []
self._capacity = capacity Error: Illegal capacity: -2
isEmpty() size()
items capacity
public private
dequeue()
enqueue()
0
Data 1 Data 2 Data 3 Data 4 …
items capacity
February 9, 2022 © Osmar R. Zaïane : University of Alberta 22
Bounded Queue dequeue()
and peek()
try:
# Removes and returns the front-most item in the queue.
# Returns nothing if the queue is empty. item = q.dequeue()
def dequeue(self): except Exception as e:
if len(self._items) <= 0: print(e.args)
raise Exception('Error: Queue is empty')
return self._items.pop(0)
('Error: Queue is empty',)
# Returns the front-most item in the queue, and DOES NOT change the queue.
def peek(self):
if len(self._items) < = 0:
raise Exception('Error: Queue is empty')
return self._items[0]
return self._capacity
February 9, 2022 © Osmar R. Zaïane : University of Alberta 24
Bounded Queue clear()
and str()
# Removes all items from the queue, and sets the size to 0
# clear() should not change the capacity
def clear(self):
self._items = []
0 1 … n-2 n-1
…
February 9, 2022 © Osmar R. Zaïane : University of Alberta 28
Visualize Modulus
0
Clock is hour modulo 12
0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11,0,1,2…
33 % 6 = ? 33 % 6 = 3
head
tail
tail
0 1 2 3 4 0 1 2 3 4
D1 D2 D3 enqueue D1 D2 D3 D4
D4
Add at tail then increment tail
ue
que
de D1
head
head
tail
tail
0 1 2 3 4 0 1 2 3 4
D2 D3 D4 enqueue D2 D3 D4 D5
D5
Remove from head then increment head
February 9, 2022 © Osmar R. Zaïane : University of Alberta 30
head
Sliding Indexes (slide 1)
tail
head
tail
0 1 2 3 4 0 1 2 3 4
D2 D3 D4 D5 dequeue D3 D4 D5
D2
ue
q ue
en D6
head
head
tail
tail
0 1 2 3 4 0 1 2 3 4
D6 D3 D4 D5 dequeue D6 D4 D5
D3
tail
head
0 1 2 3 4
0 1 2 3 4
D6 D3 D4 D5
February 9, 2022 © Osmar R. Zaïane : University of Alberta 32
Circular Queues - Empty and Full
We can avoid leaving one empty entry in the Queue
by caching the current size of the queue
The condition for an empty Queue is: the size is 0.
The condition for a full Queue is: the size is equal to
the queue maximum capacity.
When enqueueing the cached size is incremented
When dequeueing the cached size is decremented
head
tail
Dequeue: Enqueue: count 4
remove from head add to tail
0 1 2 3 4
increment head increment tail
D6 D3 D4 D5
February 9, 2022 © Osmar R. Zaïane : University of Alberta 33
Circular Queue ADT
CQueue(capacity)
Create a new queue of size capacity that is empty.
enqueue(item)
Adds a new item item to the rear of the queue (if there is room).
dequeue()
Remove the front item from the queue and return it.
peek()
Return the front item from the queue without removing it
isEmpty()
Test to see whether the queue is empty and return a Boolean value
isFull()
Test to see whether the queue reached full capacity and return a
Boolean value
size() We need to raise
Return the number of items on the queue exceptions when
capacity() enqueue and reaching
Return the capacity of the queue
clear()
capacity or dequeue
Empty the queue. when queue is empty
February 9, 2022 © Osmar R. Zaïane : University of Alberta 34
Circular Queue
Constructor
Different way to
class CircularQueue:
test input
# Constructor, which creates a new empty queue:
parameter
def __init__(self, capacity):
if type(capacity) != int or capacity<=0:
raise Exception (‘Capacity Error')
self._items = []
self._capacity = capacity
self._count=0
self._head=0
self._tail=0 isEmpty() size()
items capacity
public private
# Returns the front-most item in the queue, and DOES NOT change the queue.
def peek(self):
if self._count == 0:
raise Exception('Error: Queue is empty')
return self._items[self._head]
February 9, 2022 © Osmar R. Zaïane : University of Alberta 38
Circular Queue isEmpty(),
IsFull(), size() and capacity()
# Returns True if the queue is empty, and False otherwise:
def isEmpty(self):
return self._count == 0