Answer:
Check the explanation
Step-by-step explanation:
main. py
class ArrayQueue:
INITIAL_CAPACITY = 4
def __init__(self):
#self. data_arr = make_array(ArrayQueue.INITIAL_CAPACITY)
self. data_arr = [None] * ArrayQueue.INITIAL_CAPACITY
self. num_of_elements = 0
self. front_ind = None
def __len__(self):
return self. num_of_elements
def is_empty(self):
return self. num_of_elements == 0
def first(self):
""" Peak the front of the queue without removing any item. """
if self. front_ind == None:
return None
else:
return self. data_arr[self.front_ind]
def enqueue(self, elem):
""" Add the element to the rear of queue. Extend the queue if necessary. """
# If queue is empty
if self. front_ind == None:
self. front_ind = 0
self. data_arr[0] = elem
self. num_of_elements += 1
else:
# If the queue is not full
if self. num_of_elements != len(self. data_arr):
self. data_arr[(self. front_ind + self. num_of_elements) % len(self. data_arr)] = elem
self. num_of_elements += 1
else:
# Add new cell at the end of data array
self. data_arr. append(None)
# Index of new empty cell
new_cell_index = len(self. data_arr) - 1
# If the last element of queue was the last element of array
if self. front_ind == 0:
self. data_arr[new_cell_index] = elem