Question
existing code class CircularQueue: def __init__(self, capacity = 8): self.capacity = capacity self.front = 0 self.count = 0 self.back = capacity - 1 self.items =
existing code
class CircularQueue:
def __init__(self, capacity = 8):
self.capacity = capacity
self.front = 0
self.count = 0
self.back = capacity - 1
self.items = [None] * capacity
def __str__(self):
a_list = []
for i in range(self.count):
j = (self.back - i) % len(self.items)
item = self.items[j]
a_list.append(item)
return '-> |{}| ->'.format(', '.join([repr(item) for item in a_list]))
def enqueue(self, item):
if not self.is_full():
self.back = (self.back + 1) % self.capacity
self.items[self.back] = item
self.count += 1
else:
raise IndexError('ERROR: The queue is full.')
def dequeue(self):
if not self.is_empty():
item = self.items[self.front]
self.front = (self.front + 1) % self.capacity
self.count -= 1
return item
else:
raise IndexError('ERROR: The queue is empty.')
def is_empty(self):
return self.count == 0
def is_full(self):
return self.count == self.capacity
def create_new_circular_queue(self, number):
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started