1 Star 0 Fork 0

4.9527/Data-Structures-and-Algorithms-Using-Python

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
arrayqueue.py 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
4.9527 提交于 2019-12-04 10:41 +08:00 . read chapter 9
# Implementation of the Queue ADT using a circular array.
from array import Array
class Queue:
# Creates an empty queue
def __init__(self, maxSize):
self._count = 0
self._front = 0
self._back = maxSize - 1
self._qArray = Array(maxSize)
# Returns True if the queue is empty.
def isEmpty(self):
return self._count == 0
# Retruns True if the queue is full.
def isFull(self):
return self._count == len(self._qArray)
# Returns the number of items in the queue.
def __len__(self):
return self._count
# Adds the given item to the queue.
def enqueue(self, item):
assert not self.isFull(), "Cannot enqueue to a full queue."
maxSize = len(self._qArray)
self._back = (self._back + 1) % maxSize
self._qArray[self._back] = item
self._count += 1
# Removes and returns the first item in the queue.
def dequeue(self):
assert not self.isEmpty(), "Cannot dequeue from an empty queue."
item = self._qArray[self._front]
maxSize = len(self._qArray)
self._front = (self._front + 1) % maxSize
self._count -= 1
return item
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/nicolas4d/Data-Structures-and-Algorithms-Using-Python.git
git@gitee.com:nicolas4d/Data-Structures-and-Algorithms-Using-Python.git
nicolas4d
Data-Structures-and-Algorithms-Using-Python
Data-Structures-and-Algorithms-Using-Python
master

搜索帮助