Professional Documents
Culture Documents
Queue,
Bounded Queue, Circular Queue
S S
0 5 4 0 11 9
Stack 1 2 3 Queue 1 3 6
Search 6 7 8 Search 2 5 8
10 11 9 4 7 10
F F
S
0 11 9 P10
P0
P1
P2
P3
P4
P5
P6
P7
P8
P9
1 3 6
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
Front Rear
0 n-1
Data 1 Data 2 Data 3 Data 4 …
class BoundedQueue:
# Constructor, which creates a new empty queue:
def __init__(self, capacity):
assert isinstance(capacity, int), ('Error: Type error: %s' % (type(capacity)))
assert capacity > 0, ('Error: Illegal capacity: %d' % (capacity))
self.__items = []
self.__capacity = capacity
isEmpty() size()
items capacity
public private
dequeue()
enqueue()
# Adds a new item to the back of the queue, and returns nothing:
def enqueue(self, item):
if len(self.__items) >= self.__capacity:
raise Exception('Error: Queue is full')
self.__items.append(item)
0
Data 1 Data 2 Data 3 Data 4 …
items capacity
# 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]
1 2 3 4 5 Max=100
February 24, 2021 © Osmar R. Zaïane : University of Alberta 24
February 24, 2021 © Osmar R. Zaïane : University of Alberta 25
Circular Queues
A circular queue is a bounded queue, but
instead of pinning the front (head) at position
0, both rear and front have indexes that slide
Front Rear
0 n
… Data 1 Data 2 Data 3 Data 4 …
0 1 … n-2 n-1
…
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
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
head
tail
0 1 2 3 4 0 1 2 3 4
D2 D3 D4 D5 dequeue D3 D4 D5
D2
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 24, 2021 © Osmar R. Zaïane : University of Alberta 31
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 24, 2021 © Osmar R. Zaïane : University of Alberta 32
Circular Queue: ADT
CircularQueue(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 trying
capacity() to enqueue at capacity,
Return the capacity of the queue
clear()
and dequeue when
Empty the queue. queue is empty
February 24, 2021 © Osmar R. Zaïane : University of Alberta 33
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
# Adds a new item to the back of the queue, and returns nothing:
def enqueue(self, item):
if self.__count == self.__capacity: We could have avoided
raise Exception('Error: Queue is full') if the __init__() had:
for i in range(self.__capacity):
if len(self.__items) < self.__capacity:
self.__items.append(None)
self.__items.append(item)
else:
self.__items[self.__tail]=item
self.__count +=1 items capacity
self.__tail=(self.__tail +1) % self.__capacity
count
head tail
0 Data 1 Data 2 Data 3 Data 4
# 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]