I have been trying to make my circular queue work for some time now and just thought I'd ask a question and see if I can get any hints. The problem with my circular queue is that the insert method always returns the value False even when the queue is not full. Here is what I have so far.
class CircularQueue:
def __init__(self, size):
"""
-------------------------------------------------------
Initializes an empty queue. Data is stored in a list.
-------------------------------------------------------
Postconditions:
Initializes an empty queue.
-------------------------------------------------------
"""
assert size>=0, "size must be >=0"
self._values = [None] * size
self._front = 0
self._rear = 0
self._size = size
self.size = 0
return
def __len__(self):
"""
-------------------------------------------------------
Returns the size of the queue.
Use: n = len( q )
-------------------------------------------------------
Postconditions:
Returns the number of values in the queue.
-------------------------------------------------------
"""
return len(self._values())
def is_full(self):
value = self._rear - self._size
if self._values[value]==None:
full = False
else:
full = True
return full
def is_empty(self):
"""
-------------------------------------------------------
Determines if the queue is empty.
Use: b = q.is_empty()
-------------------------------------------------------
Postconditions:
Returns True if the queue is empty, False otherwise.
-------------------------------------------------------
"""
if len(self._values) == 0:
empty = True
else:
empty = False
return empty
def insert(self, value):
"""
-------------------------------------------------------
Inserts a copy of value into the queue.
Use: q.insert( value )
-------------------------------------------------------
Preconditions:
value - a data element (?)
Postconditions:
value is added to the rear of the queue.
-------------------------------------------------------
"""
if self.is_full() == True:
inserted = False
else:
inserted = True
self._values[self._rear] = value
self._rear = (self._rear + 1)% self._size
return inserted
def remove( self ):
"""
-------------------------------------------------------
Removes and returns value from the queue.
Use: v = q.remove()
-------------------------------------------------------
Postconditions:
Returns the value at the front of queue - the value is
removed from queue. Returns None if queue is empty.
-------------------------------------------------------
"""
if self.is_empty() == False:
value = copy.deepcopy(self._values[self._front])
self._front = (self._front + 1) % self._size
else:
value = None
return value
def peek(self):
"""
-------------------------------------------------------
Peeks at the front of queue.
Use: v = q.peek()
-------------------------------------------------------
Postconditions:
Returns a copy of the value at the front of queue -
the value is not removed from queue. Returns None
if queue is empty.
-------------------------------------------------------
"""
if self.is_empty() == True:
value = None
else:
value = copy.deepcopy(self._values[self._front])
return value
def print_i(self):
"""
-------------------------------------------------------
Prints the contents of queue from front to rear.
Use: q.print_i()
-------------------------------------------------------
Postconditions:
Prints each value in queue from front to rear.
Each value starts on a new line.
-------------------------------------------------------
"""
for i in range(len(self._values)):
print(self._values[i])
return
I just started to try and test it so this is all I have for my testing module.
from circularqueue import CircularQueue
q = CircularQueue(10)
value = q.insert(1)
print(value)
All I'm trying to do here is see if my insert method will return True because I know for a fact that the queue is not full because that is the very first value that I have tried to enter. Any hints?
insertreturnsNone.