本文最后更新于:1 个月前

代码如下:

class MyQueue:
    def __init__(self, capcity):
        self.list = [None] * capcity
        self.front = 0
        self.rear = 0

    def enqueue(self, element):
        if (self.rear+1) % len(self.list) == self.front:
            raise Exception('队列已满!')
        self.list[self.rear] = element
        self.rear = (self.rear+1) % len(self.list)

    def dequeue(self):
        if self.rear == self.front:
            raise Exception('队列已满!')
        dequeue_element = self.list[self.front]
        self.front = (self.front+1) % len(self.list)
        return dequeue_element
    
    def output(self):
        i = self.front
        while i != self.rear:
            print(self.list[i])
            i = (i+1)% len(self.list)

MyQueue = MyQueue(6)
MyQueue.enqueue(3)
MyQueue.enqueue(5)
MyQueue.enqueue(6)
MyQueue.enqueue(2)
MyQueue.enqueue(4)
MyQueue.output()

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

Python中二叉树的遍历 上一篇
Python中的链表 下一篇