手写循环队列

特点:
1)将普通队列改进为循环,使用%取模(取余)
2)复用性高,但会牺牲一个空间。

注意:
1)front头指针指向队列的第一个元素,front初始值= 0
2)rear尾指针指向队列最后一个元素的后一个位置,rear初始值= 0
3)队列满的条件:(rear+1)% maxSize ==front
4)队列空的条件:front ==rear
5)队列中有效数据个数:(rear+maxSize-front)%maxSize

package queue;

/**
 * @author litianfu
 * @version 1.0
 * @email 1035869369@qq.com
 * @date 2020-03-25 13:28
 */
public class CircleArrayQueue {
    public static void main(String[] args) {
        CircleQueue queue = new CircleQueue(5);
        queue.addQueue(1);
        queue.addQueue(3);
        queue.addQueue(5);
        queue.showQueue();
        System.out.println("queue.peek() = " + queue.peek());
        System.out.println("queue.getQueue() = " + queue.getQueue());
        queue.showQueue();
        queue.addQueue(33);
        queue.addQueue(12);
        queue.showQueue();
        queue.addQueue(13);
        queue.showQueue();

    }
}

class CircleQueue {
    /**
     * 队列最大容量
     */
    private Integer maxSize;
    /**
     * 队列头指针
     */
    private Integer front;
    /**
     * 队列尾指针
     */
    private Integer rear;

    /**
     * 用于存放数据
     */
    private int[] arr;

    /**
     * 初始化队列
     *
     * @param maxSize
     */
    public CircleQueue(Integer maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        //指向队列头,该指针是指向队首
        front = 0;
        //指向队列尾,就是队列最后一个数据
        rear = 0;
    }

    /**
     * 判断队列满,
     * 当满足(rear+1)%maxSize == front时,队列满
     *
     * @return
     */
    public Boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    public Boolean isNull() {
        return rear.equals(front);
    }

    /**
     * 添加数据到队列中
     *
     * @param n
     */
    public void addQueue(Integer n) {
        //判满
        if (isFull()) {
            System.out.println("队列为满");
            return;
        } else {
            //将数据加入队列
            arr[rear] = n;
            //将rear尾指针后移
            rear = (rear + 1) % maxSize;
        }
    }

    /**
     * 获取队首数据,出队
     */
    public Integer getQueue() {
        //判空
        if (isNull()) {
            System.out.println("队列为空");
            return -1;
        } else {
            //1.先把front对应值保留到一个临时变量
            Integer temp = arr[front];
            //2.将front后移,考虑取模
            front = (front + 1) % maxSize;
            //3.将临时变量值返回
            return temp;
        }
    }

    /**
     * 查看头数据,并不是取数据
     *
     * @return
     */
    public Integer peek() {
        if (isNull()) {
            System.out.println("队列为空");
            return -1;
        }
        return arr[front];
    }

    /**
     * 获取当前队列有效值个数
     *
     * @return
     */
    public Integer size() {
        return (rear + maxSize - front) % maxSize;
    }

    /**
     * 遍历队列
     */
    public void showQueue() {
        if (isNull()) {
            System.out.println("队列为空");
            return;
        }
        System.out.println("***队尾***");
        for (int i = size() + front -1; i >= front; i--) {
            System.out.println("\t" + arr[i]);
        }
        System.out.println("***队首***");
    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值