数据结构与算法--数组模拟循环队列(Circular Queue)

此文章仅作为自己学习过程中的记录和总结,同时会有意地去用英文来做笔记,一些术语的英译不太准确,内容如有错漏也请多指教,谢谢!


一、概述

-循环队列的基本组成结构为:

  • (int) maxSIze:队列的最大容量。
  • (int) front:指向队列头的“指针”。(实际上存储的是指向队列第一个元素下标
  • (int) rear:指向队列尾的“指针”。(实际上存储的是队列最后一个元素的下一个位置的下标
  • (E[ ]) queueArr:模拟队列的数组。(E的类型取决于实际情况)

在这里插入图片描述
-相较于普通队列的区别:

  1. 队列容量:模拟普通队列的数组每个元素都被利用起来;而模拟循环队列的数组的实际有效元素个数为(maxSize - 1),有一个位置被用来对队列空/满做判断。
  2. front和rear的含义: 循环队列中,front存储的是队列第一个元素的下标,rear存储的是队列最后一个元素的下一位置的下标;而普通队列中,front存储的是队列第一个元素的下一位置的下标,rear存储的是队列最后一个元素的下标。
  3. front和rear的默认值: 循环队列中,front和rear默认值为0;而普通队列中,front和rear的默认值为-1。

(关于普通队列,具体可见:数据结构与算法–数组模拟队列(Queue)

(关于C语言描述的普通队列和循环队列,具体可见:队列的定义、顺序、循环、链队列及其部分操作具体实现(C语言描述)

在这里插入图片描述
-内容

  • 构造方法创建数组循环队列。
  • isEmpty(), isFull()【判断队列是否为空/满】
  • getSIze()【获取队列当前有效元素个数】
  • addQueue()【向队列中添加元素】
  • getQueue()【获取队列头元素并将front向后移一个,实现“假出队列”的效果】
  • peekQueue()【获取队列头元素但不影响front的值】
  • showQueue()【展示队列所有元素】

-特别说明:

  • 判断循环队列是否为空的条件:rear == front
  • 判断循环队列是否为满的条件:(rear + 1) % maxSize == front
  • 循环队列当前有效元素个数:(rear + maxSize - front) % maxSize【可看作对rear-front取绝对值】

二、代码实现

  • Attributes and constructor
/*
 CircularArrayQueue

 Zzay

 2021/01/17
 */
package com.zzay.queue;

/**
 * DIFFERENCE:
 * (1) Capacity:【容量】
 * The capacity of a circular array queue is actually "maxSize - 1",
 * because it need to spare a space for the judgements of "isFull()" and "isEmpty()".
 * (若不留一个空位,则无论是满还是空,都是"front == rear")
 * 
 * (2) Default value of "front" and "rear":【front和rear的默认值】
 * Front: Default value is now 0 in place of -1 as before.
 * Rear: Default value is now 0 in place of -1 as before.
 * 
 * (3) The meaning of "front" and "rear":【front和rear的含义】
 * Front: Points to the first element of the queue, instead of the previous place of the first element.
 * Rear: Points to the next place of the last element, instead of the last element.
 *
 * @author Zzay
 * @version 2021/01/17
 */
public class CircularArrayQueue {
   

    // The maximum capacity of the array.
    private int maxSize;

    // The indicator of the first element of the queue.
    // The "front" indicator points right to the first element, different from the linear queue.
    // Default value is 0.
    private int front;

    // The indicator of the rear element of the queue.
    // The "rear" indicator points to the next place of the last element, different from the linear queue.
    // Default value is 0.
    private int rear;

    // The array that modifies a queue.
    private int[] queueArr;

    /**
     * Receive a capacity and instantiate a queue array with the max size of the same value.
     * Also, do initializations.
     *
     * @param capacity The expected max size of the queue array
     */
    public CircularArrayQueue(int capacity) {
   
        if (capacity <= 0
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值