# implementation of the Queue ADT using a linked list.class Queue:# Creates an empty queue.def __init__(self):self._qhead = Noneself._qtail = Noneself._count = 0# Returns True if the queue is empty.def isEmpty(self):return self._qhead is None# Returns the number of items in queue.def __len__(self):return self._count# Adds the given item to the queue.def enqueue(self, item):node = _QueueNode(item)if self.isEmpty():self._qhead = nodeelse:self._qtail.next = nodeself._qtail = nodeself._count += 1# Removes and returns the first item in the queue.def dequeue(self):assert not self.isEmpty(), "Cannot dequeue from an empty queue."node = self._qheadif self._qhead is self._qtail:self._qtail = Noneself._qhead = self._qhead.nextself._count -= 1return node.item# Private storage class for creating the linked list nodes.class _QueueNode(object):def __init__(self, item):self.item = itemself.next = None
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。