zoukankan      html  css  js  c++  java
  • 循环队列的实现

    class MyCircularQueue:
    
        def __init__(self, k: int):
            """
            Initialize your data structure here. Set the size of the queue to be k.
            """
            self.k=k
            self.front=-1
            self.rear=-1
            self.queue=[None]*k
            
            
    
        def enQueue(self, value: int) -> bool:
            """
            Insert an element into the circular queue. Return true if the operation is successful.
            """
            if self.isFull():     
                return False
            if self.isEmpty():
                self.front = 0
            self.rear = (self.rear + 1) % self.k
            self.queue[self.rear] = value
            return True
            
    
        def deQueue(self) -> bool:
            """
            Delete an element from the circular queue. Return true if the operation is successful.
            """
            if self.isEmpty():
                return False
            if self.front == self.rear:      # 出栈不需要真的删除元素,只需要改变头尾指针
                self.front = -1              # 首尾指针指到相同元素,且不为-1,此时必然仅有一个元素
                self.rear = -1
                return True
            self.front = (self.front + 1) % self.k
            return True
            
    
        def Front(self) -> int:
            """
            Get the front item from the queue.
            """
            return -1 if self.isEmpty() else self.queue[self.front]
    
        def Rear(self) -> int:
            """
            Get the last item from the queue.
            """
            return -1 if self.isEmpty() else self.queue[self.rear]
            
    
        def isEmpty(self) -> bool:
            """
            Checks whether the circular queue is empty or not.
            """
            return self.front == -1 and self.rear==-1
            
    
        def isFull(self) -> bool:
            """
            Checks whether the circular queue is full or not.
            """
            return (self.rear+1)%self.k==self.front
    
        # 输出队列中的元素
        def ShowQueue(self):
            for i in range(self.k):
                print(self.queue[i],end=' ')
            print(' ')
  • 相关阅读:
    弗尤博客(二)
    弗尤博客(一)
    第一系列完
    C# 关闭子窗体释放子窗体对象问题
    C#设置IE代理
    C# 计算位置居中
    C# 绘图
    From传值
    pictureBox绑定Base64字符串
    C# 绘制圆角矩形
  • 原文地址:https://www.cnblogs.com/jzxs/p/11374659.html
Copyright © 2011-2022 走看看