循环队列之静态队列

从生活中,能够抽象出队列的概念,队列就是一个能够实现“先进先出”的存储结构。队列分为链式队列和静态队列;静态队列一般用数组来实现,但此时的队列必须是循环队列,否则会造成巨大的内存浪费;链式队列是用链表来实现队列的。这里讲的是循环队列,首先我们必须明确以下几个问题
一、循环队列的基础知识
1.循环队列须要几个參数来确定
循环队列须要2个參数,front和rear
2.循环队列各个參数的含义
(1)队列初始化时,front和rear值都为零;
(2)当队列不为空时,front指向队列的第一个元素,rear指向队列最后一个元素的下一个位置;
(3)当队列为空时,front与rear的值相等,但不一定为零;

Code:
1: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.rear+1)%self.size ==self.head

2.增加一个变量记录队列的长度
class MyCircularQueue:

def __init__(self, k):
    """
    Initialize your data structure here. Set the size of the queue to be k.
    :type k: int
    """
    self.queue = [0] * k
    self.head = 0
    self.tail = 0
    self.lenth = k
    self.count = 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.count < self.lenth:
        self.queue[self.tail] = value
        self.tail = (self.tail + 1) % self.lenth
        self.count += 1
        return True
    else:
        return False

def deQueue(self):
    """
    Delete an element from the circular queue. Return true if the operation is successful.
    :rtype: bool
    """
    if self.count > 0:
        self.queue[self.head] = 0
        self.head = (self.head + 1) % self.lenth
        self.count -= 1
        return True
    else:
        return False

def Front(self):
    """
    Get the front item from the queue.
    :rtype: int
    """
    if self.count > 0:
        return self.queue[self.head]
    else:
        return -1

def Rear(self):
    """
    Get the last item from the queue.
    :rtype: int
    """
    if self.count > 0:
        return self.queue[(self.tail + self.lenth - 1) % self.lenth]
    else:
        return -1

def isEmpty(self):
    """
    Checks whether the circular queue is empty or not.
    :rtype: bool
    """
    if self.count == 0:
        return True
    else:
        return False

def isFull(self):
    """
    Checks whether the circular queue is full or not.
    :rtype: bool
    """
    if self.count == self.lenth:
        return True
    else:
        return False

猜你喜欢

转载自blog.csdn.net/u012420317/article/details/88685871