I'm working on a small project where I'm implementing a queue as a circular array. As a challenge, I was assigned to not use any list functions like append when implementing this ADT. It's assumed that I only need to resize the queue when it gets full. When I ran my code through a debugger and step through it, I find that the issue revolves around my self.read value (read pointer) which is the global variable in question. This is kind of perplexing to me since neither of my functions that would be affected increments my pointer. Could anyone shed some light on this problem for me?
This class is coded as:
class CircularQueue(object):
def __init__(self, capacity=2):
"""
Initialize the queue to be empty with a fixed capacity
:param capacity: Initial size of the queue
"""
self.capacity = capacity
self.size = 0
self.list = [0] * self.capacity
self.sum = 0
self.read = 0
self.write = 0
def __eq__(self, other):
return self.capacity == other.capacity
and self.size == other.size
and self.read == other.read
and self.write == other.write
def __str__(self):
if self.size == 0:
return "Queue is empty"
content = ""
while ((self.read + 1) % self.size) != self.write:
content = content + str(self.list[self.read]) + " -> "
self.read = (self.read + 1) % self.size
content = content[:-3]
return f"Contents: {content}"
__repr__ = __str__
The portion that I'm interested in looking at is my enqueue and resize functions:
def resize(self):
bigger = [None] * (self.capacity * 2) #create bigger queue
b_ind = 0
read_ptr = self.read
while read_ptr != (self.write + 1): #iterate through old queue to copy into new queue
bigger[b_ind] = self.list[read_ptr]
b_ind += 1
read_ptr += 1
self.capacity *= 2 #setting capacity
self.list = bigger #setting new list as queue
self.read = 0 #normalize queue
self.write = b_ind
def enqueue(self, number):
if self.size == 0: #if queue was originally empty
self.list[self.read] = number
self.write += 1
else:
self.list[self.write] = number #add onto end of queue
if ((self.write + 1) % self.capacity == self.read): #resize if queue loops back and the write pointer is the same as the read pointer
self.resize()
else:
self.write = (self.write + 1) % self.capacity #set write pointer
self.sum += number #add to sum
self.size += 1 # increment size
This was the test case I ran for my code:
queue = CircularQueue()
queue.enqueue(23)
queue.enqueue(42)
queue.enqueue(2)
queue.enqueue(195)
print(queue)