循环队列之静态队列

博客主要介绍了队列的概念,它是能实现“先进先出”的存储结构,分为链式队列和静态队列,静态队列一般用数组实现且需为循环队列。着重讲解了循环队列,包括确定其所需的参数front和rear,以及各参数在不同队列状态下的含义,还给出了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从生活中,能够抽象出队列的概念,队列就是一个能够实现“先进先出”的存储结构。队列分为链式队列和静态队列;静态队列一般用数组来实现,但此时的队列必须是循环队列,否则会造成巨大的内存浪费;链式队列是用链表来实现队列的。这里讲的是循环队列,首先我们必须明确以下几个问题
一、循环队列的基础知识
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值