我一直试图使我的循环队列工作一段时间,只是想我问一个问题,看看我是否能得到任何提示。我的循环队列的问题是即使队列未满,insert方法也始终返回值False。这是我到目前为止所拥有的。
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
我刚开始尝试测试它,所以这就是我的测试模块。
from circularqueue import CircularQueue
q = CircularQueue(10)
value = q.insert(1)
print(value)
我试图在这里做的就是查看我的insert方法是否会返回True,因为我知道队列未满,因为这是我尝试输入的第一个值。任何提示?
答案 0 :(得分:1)
您将_front
和_rear
初始化为0,因此您的is_full
检查从一开始就返回True。