数组实现队列(循环队列)的思想以及简易代码

/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.algorithm.tiger.queue;

import java.util.Arrays;

/**
 * 用数组实现的队列(循环队列),队列容量保持恒定
 * 先进先出FIFO
 *
 * @description: 队列
 * @author: tiger
 * @create: 2020-07-12 13:38
 */
public class MyQueue {

    //队列的存储容器
    private Integer[] array;
    //队头指针
    private Integer   front;
    //队尾指针
    private Integer   rear;
    //队列实际元素数量
    private Integer   size;
    //队列最大容量
    private Integer   maxSize;

    public MyQueue(Integer capacity) {
        this.array = new Integer[capacity];
        front = 0;
        rear = 0;
        size = 0;
        maxSize = capacity - 1;
    }

    /**
     * 入队,只能加入队尾
     *
     * @param element 入队的元素
     */
    public void enQueue(Integer element) throws Exception {

        //判断队列是否已经满
        if ((rear + 1) % array.length == front) {
            throw new Exception("队列已满!");
        }
        //在原来队尾位置添加元素
        array[rear] = element;
        //重置队尾指针(循环移动)
        rear = (rear + 1) % array.length;
        //队列元素数量加1
        size++;
    }

    /**
     * 出队,只能将队头位置元素出队列
     */
    public Integer deQueue() throws Exception {
        //判断队列是否已空
        if (front == rear) {
            throw new Exception("队列已空!");
        }
        //原先队头元素
        Integer deQueue = array[front];
        //将队头元素指空
        array[front] = null;
        //重置队头指针
        front = (front + 1) % array.length;
        //队列元素数量减1
        size--;
        return deQueue;
    }

    public Integer[] getArray() {
        return array;
    }

    public void setArray(Integer[] array) {
        this.array = array;
    }

    public Integer getFront() {
        return front;
    }

    public void setFront(Integer front) {
        this.front = front;
    }

    public Integer getRear() {
        return rear;
    }

    public void setRear(Integer rear) {
        this.rear = rear;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return "MyQueue{" +
                "array=" + Arrays.toString(array) +
                ", front=" + front +
                ", rear=" + rear +
                ", size=" + size +
                ", maxSize=" + maxSize +
                '}';
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //创建一个容量为6-1的队列
        MyQueue myQueue = new MyQueue(7);
        myQueue.enQueue(12);
        myQueue.enQueue(23);
        myQueue.enQueue(44);

        System.out.println(myQueue);
        myQueue.deQueue();
        System.out.println(myQueue);
        myQueue.deQueue();
        System.out.println(myQueue);
        myQueue.enQueue(66);
        myQueue.enQueue(66);
        myQueue.enQueue(77);
        myQueue.enQueue(88);
        System.out.println(myQueue);
        myQueue.enQueue(3333);
        System.out.println(myQueue);
        myQueue.deQueue();
        System.out.println(myQueue);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljt-tiger

thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值