https://blog.csdn.net/lilililililiki/article/details/104317286
设置初始标记: flag=false
当入队列时,让rear往后移动,让flag=true
当出队列时,让front往后移动,让flag=false
当队列为空时: rear == front && flag==false
当队列为满时: rear == front && flag == true
rear==front
,就肯定是队列满了呀当队列为空时条件:rear == front
当队列满时条件为:(rear+1)% maxsize == front
队列为空时,count == 0
当有元素入队时,count++,当count和队列的maxsize相等时,代表队列已满
进队:front = (front + 1) % maxSize
出队:rear = (rear + 1) % maxSize
队列中元素的个数 m = (rear - front + maxSize) % maxSize
class MyCircularQueue:
def __init__(self, k):
"""
Initialize your data structure here. Set the size of the queue to be k.
:type k: int
"""
self.size = k+1
self.data = [0]*self.size
self.head = self.rear = 0
def enQueue(self, value):
"""
Insert an element into the circular queue. Return true if the operation is successful.
:type value: int
:rtype: bool
"""
if self.isFull():
return False
self.data[self.rear] = value
self.rear = (self.rear+1)%self.size
return True
def deQueue(self):
"""
Delete an element from the circular queue. Return true if the operation is successful.
:rtype: bool
"""
if self.isEmpty():
return False
self.head = (self.head+1)%self.size
return True
def Front(self):
"""
Get the front item from the queue.
:rtype: int
"""
if self.isEmpty():
return -1
return self.data[self.head]
def Rear(self):
"""
Get the last item from the queue.
:rtype: int
"""
if self.isEmpty():
return -1
return self.data[(self.rear-1)%self.size]
def isEmpty(self):
"""
Checks whether the circular queue is empty or not.
:rtype: bool
"""
return self.head ==self.rear
def isFull(self):
"""
Checks whether the circular queue is full or not.
:rtype: bool
"""
return (self.head - self.rear)%self.size ==1