Question: Circular, array - backed queue ( 1 2 points ) A queue can be build using fixed - size array to store its elements, and

Circular, array-backed queue (12 points)
A queue can be build using fixed-size array to store its elements, and whose `head` and `tail` indices "wrap around" when they reach the end of the array. For this exercise you will complete this implementation so that it properly detects when it is full (in which case it should raise a `RuntimeError` when `enqueue` is called), and so that it can be resized when necessary (using the `resize` method). You will also implement an `__iter__` method that allows the queue to be iterated over from front (head) to back (tail).
The starter code can be found in class `Queue`, found in the file `circ_queue.py`. You should not modify the `__init__` method, and your implementation should ensure that when dequeuing an item, its slot in the array is reset to `None`, and that when the queue is emptied its `head` and `tail` attributes are reset to `-1`.
Assume that `resize` will only be called with a value greater than the current length of the underlying array. Note that the underlying data container is a `numpy` array --- when you resize the queue, you should create a new `numpy` array of the appropriate size and copy the existing elements over to it.
circ_queue.py
import numpy as np
from typing import Any
class Queue:
def __init__(self, limit=10):
self.data = np.empty(limit, dtype=object)
self.head =-1
self.tail =-1
self.count =0
def empty(self):
return self.count ==0
def __bool__(self):
return not self.empty()
def __str__(self):
if not(self):
return ''
return ','.join(str(x) for x in self)
def __repr__(self):
return str(self)
## Don't change the above code!
def enqueue(self, val: Any):
raise NotImplementedError
def dequeue(self)-> Any:
raise NotImplementedError
def resize(self, newsize: int):
assert len(self.data)< newsize
raise NotImplementedError
def __iter__(self):
raise NotImplementedError

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!