-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.py
More file actions
60 lines (51 loc) · 1.9 KB
/
Copy pathArrayQueue.py
File metadata and controls
60 lines (51 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Exception when attempting to access an element from an empty container.
class Empty(Exception):
pass
class ArrayQueue:
#FIFO queue implementation using a Python list as underlying storage.
DEFAULT_CAPACITY = 10 # moderate capacity for all new queues
def __init__(self):
#Create an empty queue.
self._data = [None]*ArrayQueue.DEFAULT_CAPACITY
self._size = 0
self._front = 0
def __len__(self):
#Return the number of elements in the queue.
return self._size
def is_empty(self):
#Return True if the queue is empty.
return self._size==0
def first(self):
#Return (but do not remove) the element at the_front of the queue.
#Raise Empty exception if the queue is empty.
if self.is_empty():
raise Empty("Queue is empty")
return self._data[self._front]
def dequeue(self):
#Remove and return the first element of the queue (i.e., FIFO).
#Raise Empty exception if the queue is empty.
if self.is_empty():
raise Empty("Queue is empty")
answer = self._data[self._front]
self._data[self._front] = None # help garbage collection
self._front = (self._front + 1)%len(self._data)
self._size-=1
if 0<self._size<len(self._data)//4:
self._resize(len(self._data)//2)
return answer
def enqueue(self, e):
#Add an element to the back of queue.
if self._size == len(self._data):
self._resize(2*len(self._data)) # double the array size
avail = (self._front + self._size)%len(self._data)
self._data[avail] = e
self._size += 1
def _resize(self, cap): # we assume cap >= len(self)
#Resize to a new list of capacity >= len(self).
old = self._data # keep track of existing list
self._data = [None]*cap # allocate list with new capacity
walk = self._front
for k in range(self._size): # only consider existing elements
self._data[k] = old[walk] # intentionally shift indices
walk = (1+walk)%len(old) # use old size as modulus
self._front = 0 # front has been realigned