Question: Python 3 defining a Class - Lots of screenshots uploaded for reference/background info Q2.1: Implement the Queue You will need the following instance attributes: front

Python 3 defining a Class - Lots of screenshots uploaded for reference/background info

Python 3 defining a Class - Lots of screenshots uploaded for reference/backgroundinfo Q2.1: Implement the Queue You will need the following instance attributes:front - index of the front of the queue rear - indexof the first empty slot (or index of the last element, itis up to you) num_elems integer that tracks the number of elementspresent in the queue capacity - capacity of the queue, initially 3,initialize your array with elements set to None data - an array

Q2.1: Implement the Queue You will need the following instance attributes: front - index of the front of the queue rear - index of the first empty slot (or index of the last element, it is up to you) num_elems integer that tracks the number of elements present in the queue capacity - capacity of the queue, initially 3, initialize your array with elements set to None data - an array of length capacity (represents your queue) To implement the behavior of the ADT you need to provide implementations for the following methods . dequeue - removes and returns the front element. Notice the asserts in the doctests! Do create an assert statement in this method. enqueue - adds element, doubling the capacity if is full .is_ful1 - checks whether a queue is full. Returns True if full, False otherwise. is_empty - checks whether a queue is empty. Returns True if empty, False otherwise. print_queue-prints queue. o Format: Assume that elements 4, 5, 6, 7, 8 were added to the queue q. Then q. print-queue() will print: [ I 4 I 5 I 6 I 7 I 8 I NOTE: There are spaces in between the | and the numbers in q.print_queue ). If you ignore these spaces, you will lose some points o If a queue is empty, print: [ ] Define class Queue that implements the ADT (Abstract Data Type) Queue using a circular array. . You must use a np.array for this question: o Somewhere in your constructor you must have a line that looks like self.data - o If you use a Python list, then your solution will get a 0 You must observe the FIFO convention. When implementing a queue, we have to worry about running time for its basic operations: dequeue and enqueue. Both of them must run in Theta(1) Unfortunately, a regular array does not give you desired complexity. Let me explain why: We will assume that front (index) points to the front of a queue, and rear (index) points to the end of a queue, to the next available location. Front always stays at 0, and we move rear when we add elements. AssertionError: attempt to dequeue from an empty queue >b.enqueue (1) >b.enqueue (max) >>b.print_queue() [ 1 | ] b.dequeue() 1 >b.dequeue() >b.front >>>b. rear >b.dequeue() Traceback (most recent call last): AssertionError: attempt to dequeue from an empty queue def init_(self,capacity 3): :param capacity: a positive integer qQueue) # Your Code Here def dequeue(self) dequeues from the front of the queue >q.dequeue() Traceback (most recent call last): AssertionError: attempt to dequeue from an empty queue def enqueue (self,elem): enqueue at the rear of the queue :param elem: a value that is not None >q.enqueue ("a") # Your Code Here def expand(self) expand the capacity of the circular array when needed >qQueue () >q.capacity >>>q.expand() >>q.capacity # Your Code Here def is full(self): checks if circu lar array is fut >Queue () >>>for i in range(4): q.enqueue(i) >>q.data array ([0, 1, 2, 3, None, None], dtype-object) >>> q.is_full() False # Your Code Here def is_empty(self) checkS 1T circular array 1s futl >>> q = Queue() True # Your Code Here q.is_empty() def print_queue(self) prints out queue in a human-friend ly format >qQueue() >for i in range (5): q.enqueue(i) >>> q.print_queue() [|01 |2|341 >pQueue() >>> p.print_queue() # Your Code Here

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 Databases Questions!