python - 循环队列Python实现

标签 python python-3.x

我一直在努力让我的循环队列工作一段时间,我只是想问一个问题,看看我是否能得到任何提示。我的循环队列的问题是插入方法总是返回值 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)

我在这里要做的就是查看我的插入方法是否会返回 True,因为我知道队列未满,因为这是我尝试输入的第一个值。有什么提示吗?

最佳答案

您将 _front_rear 初始化为 0,因此您的 is_full 检查从一开始就返回 True。

关于python - 循环队列Python实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635015/

相关文章:

python - 使用 pytest 设置一个简单的 python 应用程序,出现错误 NameError 和 AttributeError

python - 打开文件并在一行中对该文件执行操作是否会关闭该文件?

python - 字符串到 int 的转换错误?

python - 第一次出现大于 numpy 数组中给定值的值

python - Reddit 的一行代码(排名功能)让我感到困惑

python - matplotlib:更改轴

Python 合并 .xls 文件

python - 返回给定矩阵中出现次数最多的元素

python - PIP 升级权限被拒绝 Windows 10

python-3.x - 从另一个数据帧搜索(行值)数据