198k views
5 votes
Question 1 (20 points): a. Consider the "circular" array implementation of a queue, similar to ArrayQueue that we studied in class, where the only difference is that the initial capacity is set to 4 (INITIAL_CAPACITY=4): class ArrayQueue: INITIAL_CAPACITY = 4 def __init__(self): self.data_arr = make_array(ArrayQueue.INITIAL_CAPACITY) self.num_of_elems = 0 self.front_ind = None def __len__(self): … def is_empty(self): … def enqueue(self, elem): … def dequeue(self): … def first(self): … def resize(self, new_cap): … Show the values of the data members: front_ind, num_of_elems, and the contents of each data_arr[i] after each of the following operations. If you need to increase the capacity of data_arr, add extra slots as described in class.

User FnCzar
by
4.0k points

1 Answer

2 votes

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

Question 1 (20 points): a. Consider the "circular" array implementation-example-1
Question 1 (20 points): a. Consider the "circular" array implementation-example-2
Question 1 (20 points): a. Consider the "circular" array implementation-example-3
Question 1 (20 points): a. Consider the "circular" array implementation-example-4
Question 1 (20 points): a. Consider the "circular" array implementation-example-5
User Golvok
by
4.1k points