循环队列笔记

本文详细介绍了循环队列的概念,包括其作为顺序队列的一种避免假溢出的改进方式,以及如何判断队列为空和队满的条件。文章还提供了循环队列的代码实现,帮助读者深入理解这一数据结构。

循环队列(Circular Queue

顺序队列

顺序队列是一种只能在表的一端进行插入运算,在表的另一端进行删除运算的线性表(头删尾插)会出现假溢出。

Rear = N - 1 时队满。

循环队列

Front == Rear 时队列为空
Front == (Rear +1 ) % queueSize 时队满

人为浪费一个空间。
侵删

代码实现

class MyCircularQueue {
    
    private int[] queue;
    private int head = 0;
    private int tail = 0;
    private int size;

    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        this.queue = new int[k + 1]; /*需要一位空出区分队满和队空*/
        this.size = k + 1;/*容量为k 但大小是k+1*/
    }
    
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        queue[tail] = value;
        tail = (tail + 1) % size;
        return true;
    }
    
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if(isEmpty()) return false;
        head = (head + 1) % size;
        return true;
    }
    
    /** Get the front item from the queue. */
    public int Front() {
        return isEmpty() ? -1 : queue[head];
    }
    
    /** Get the last item from the queue. */
    public int Rear() {
        return isEmpty() ? -1 : queue[(tail + size - 1) % size];
    }
    
    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        return head == tail;
    }
    
    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        return (tail + 1) % size == head;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */
循环队列的入队和出队操作如下: 入队操作:当队列不满时,将元素插入队尾,并将队尾指针rear向后移动一位。如果队列已满则无法插入元素。 出队操作:当队列不为空时,将队头元素删除,并将队头指针front向后移动一位。如果队列为空则无法进行出队操作。 循环队列的入队和出队操作是通过维护队头指针front和队尾指针rear来实现的。在循环队列中,队头指针front始终指向队列中的第一个元素,而队尾指针rear则指向下一个可插入元素的位置。当队尾指针rear达到队列的最后一个位置时,如果队头指针front不在队列的第一个位置上,说明队列还有空余位置,可以将rear指针循环到队列的第一个位置上,实现循环队列的特性。 同时,为了正确判断循环队列是否为空或者已满,需要设计判空和判满的条件。当队头指针front等于队尾指针rear时,表示队列为空。当 (rear+1) % MAXQSIZE == front时,表示队列已满。其中MAXQSIZE是队列的最大容量。 总结起来,循环队列的入队操作是将元素插入队尾并移动rear指针,出队操作是删除队头元素并移动front指针,同时需要根据指针位置来判断队列是否为空或已满。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [循环队列之进队出队](https://blog.youkuaiyun.com/weixin_43671437/article/details/120854633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [循环队列的入队出队-----数据结构与算法笔记](https://blog.youkuaiyun.com/weixin_44847002/article/details/121881554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [keras的API编写LeNet5网络来做mnist的分类,将谷歌训好的incep-V3迁移到花朵分类等.zip](https://download.youkuaiyun.com/download/qq_35831906/88225545)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值