# Implementation of the Queue ADT using a circular array.from array import Arrayclass Queue :# Creates an empty queue.def __init__( self, maxSize ) :self._count = 0self._front = 0self._back = maxSize - 1self._qArray = Array( maxSize )# Returns True if the queue is empty.def isEmpty( self ) :return self._count == 0# Returns 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) % maxSizeself._qArray[self._back] = itemself._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) % maxSizeself._count -= 1return item
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。