基于数组实现队列结构

一次性队列的实现

package com.hand.deque;

/**
 *
 * 基于数组实现的一次性队列
 */
public class ArrayQueue {


    /**
     * 数组的最大容量
     */
    private int maxSize;


    /**
     * 队列头
     */
    private int front;


    /**
     * 队列尾
     */
    private int rear;


    /**
     * 队列的数据
     */
    private int[] data;


    /**
     * 构造器
     *
     * @param arrayMaxSize 数组最大值
     */
    public ArrayQueue(int arrayMaxSize) {
        maxSize = arrayMaxSize;
        data = new int[arrayMaxSize];
        front = -1;
        rear = -1;
    }

    /**
     * 队列是否已满
     *
     * @return
     */
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 队列是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }


    /**
     * 向队列中添加元素
     *
     * @param n
     */
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已经满.");
            throw new RuntimeException("队列已经满.");
        }
        rear++;
        data[rear] = n;
    }

    /**
     * 取出队列的头元素
     *
     * @return
     */
    public int getQueue() {
        if (isEmpty()) {
            System.out.println("队列为空.");
            throw new RuntimeException("队列为空.");
        }
        front++;
        return data[front];
    }


    /**
     * 查看队列中的全部的数据
     */
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空.");
            throw new RuntimeException("队列为空.");
        }
        for (int i = 0; i < data.length; i++) {
            System.out.printf("array[%d]=%d\n", i, data[i]);
        }
    }

    /**
     * 查看队列的第一个元素
     *
     * @return
     */
    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列为空.");
            throw new RuntimeException("队列为空.");
        }
        return data[front + 1];
    }

}

环形队列的实现

package com.hand.deque;

/**
 * 基于数组的环形队列
 *
 *
 * 约定如下:
 * front 指向队列的第一个元素 array[front]为队列的第一个元素 front的初始值为0
 * rear 指向队列的最后一个元素的后一个位置 rear的初始值为0
 *
 *
 * 队列满的条件: ( rear + 1 ) % maxSize == front
 * 队列空的条件: front == rear
 * 队列中有效数据个数: ( rear + maxSize - front ) % maxSize
 *
 * 尾索引的下一个为头索引时表示队列满 即将队列容量空出一个作为约定。
 */
public class CircleQueue {
    /**
     * 最大容量
     */
    private int maxSize;


    /**
     * 队列尾 指向最后一个元素的后一个位置
     */
    private int rear;


    /**
     * 指向队列头
     */
    private int front;


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


    /**
     * 构造函数初始化队列
     *
     * @param arrayMaxSize
     */
    public CircleQueue(int arrayMaxSize) {
        maxSize = arrayMaxSize;
        data = new int[arrayMaxSize];
        front = 0;
        rear = 0;
    }


    /**
     * 判断队列是否已满
     *
     * @return
     */
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }


    /**
     * 队列是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }


    /**
     * 向队列中添加元素
     *
     * @param n
     */
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已经满.");
            throw new RuntimeException("队列已经满.");
        }
        //直接将数据加入
        data[rear] = n;
        //将数据后移  后移动时不可越界 需要加取模
        rear = (rear + 1) % maxSize;
    }


    /**
     * 取出队列的头元素
     *
     * @return
     */
    public int getQueue() {
        if (isEmpty()) {
            System.out.println("队列为空.");
            throw new RuntimeException("队列为空.");
        }
        //保存值到一个临时变量
        int value = data[front];
        //front后移 后移动时不可越界 需要加取模
        front = (front + 1) % maxSize;
        return value;
    }


    /**
     * 查看队列中的全部的数据
     */
    public void showQueue() {
        //有效数据的个数
        int size = (rear + maxSize - front) % maxSize;

        if (isEmpty()) {
            System.out.println("队列为空.");
            throw new RuntimeException("队列为空.");
        }

        //从front开始遍历 遍历指定个数的元素
        for (int i = front; i < front + size; i++) {
            System.out.printf("array[%d]=%d\n", i % maxSize, data[i % maxSize]);
        }
    }

    /**
     * 查看队列的第一个元素
     *
     * @return
     */
    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列为空.");
            throw new RuntimeException("队列为空.");
        }
        //直接返回
        return data[front];
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值