【Java数据结构】使用数组模拟队列

1.队列
  • 队列是一个有序列表,可以用数组或是链表来实现。
  • 遵循先进先出的原则。
  • 示意图:
    在这里插入图片描述
2.使用数组模拟队列

Demo1:

/**
 * 数组模拟队列
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/23
 */
public class ArrayQueueDemo {


    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key;
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;

        while (loop) {

            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);
            int res;
            switch (key) {
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'g':
                    try {
                        res = queue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception var8) {
                        System.out.println(var8.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception var7) {
                        System.out.println(var7.getMessage());
                    }
                    break;
                case 's':
                    queue.showQueue();
            }
        }

        System.out.println("程序退出~~");
    }
}

class ArrayQueue {


    /**
     * 数组最大容量
     */
    private int maxSize;
    /**
     * 队列头
     */
    private int head;
    /**
     * 队列尾
     */
    private int tail;
    /**
     * 存放数据的数组
     */
    private int[] arr;

    public ArrayQueue(int arrMaxSize) {
        this.maxSize = arrMaxSize;
        this.arr = new int[this.maxSize];
        this.head = -1;
        this.tail = -1;
    }

    public boolean isFull() {
        return this.head == this.maxSize - 1;
    }

    public boolean isEmpty() {
        return this.head == this.tail;
    }

    public void addQueue(int n) {
        if (this.isFull()) {
            System.out.println("队列满,不能加入数据~");
        } else {
            ++this.tail;
            this.arr[this.tail] = n;
        }
    }

    public int getQueue() {
        if (this.isEmpty()) {
            throw new RuntimeException("队列空,不能取数据");
        } else {
            ++this.head;
            return this.arr[this.head];
        }
    }

    public void showQueue() {
        if (this.isEmpty()) {
            System.out.println("队列空的,没有数据~~");
        } else {
            for(int i = 0; i < this.arr.length; ++i) {
                System.out.printf("arr[%d]=%d\n", i, this.arr[i]);
            }

        }
    }

    public int headQueue() {
        if (this.isEmpty()) {
            throw new RuntimeException("队列空的,没有数据~~");
        } else {
            return this.arr[this.head + 1];
        }
    }
}


-运行结果:

s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
队列空的,没有数据~~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输出一个数
12
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
arr[0]=12
arr[1]=0
arr[2]=0
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是12
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
队列空的,没有数据~~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输出一个数
1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
arr[0]=12
arr[1]=1
arr[2]=0
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e
程序退出~~

Process finished with exit code 0

  • 目前该Demo模拟的队列存在不能复用的问题,故选择使用算法,改进成一个环形的队列
    Demo2
/**
 * 环形数模拟队列
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/23
 */
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        System.out.println("测试数组模拟环形队列的案例~~~");
        CircleArray queue = new CircleArray(4);
        char key ;
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;

        while(loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);
            int res;
            switch(key) {
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'g':
                    try {
                        res = queue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception var8) {
                        System.out.println(var8.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception var7) {
                        System.out.println(var7.getMessage());
                    }
                    break;
                case 's':
                    queue.showQueue();
            }
        }

        System.out.println("程序退出~~");
    }
}

class CircleArray {
    /**
     * 数组最大容量
     */
    private int maxSize;
    /**
     * 队列头,指向队列第一个元素
     */
    private int head;
    /**
     * 队列尾,指向队列最后一个元素的后一个完位置
     */
    private int tail;
    /**
     * 存放数据的数组
     */
    private int[] arr;

    public CircleArray(int arrMaxSize) {
        this.maxSize = arrMaxSize;
        this.arr = new int[this.maxSize];
    }

    public boolean isFull() {
        return (this.tail + 1) % this.maxSize == this.head;
    }

    public boolean isEmpty() {
        return this.tail == this.head;
    }

    public void addQueue(int n) {
        if (this.isFull()) {
            System.out.println("队列满,不能加入数据~");
        } else {
            this.arr[this.tail] = n;
            this.tail = (this.tail + 1) % this.maxSize;
        }
    }

    public int getQueue() {
        if (this.isEmpty()) {
            throw new RuntimeException("队列空,不能取数据");
        } else {
            int value = this.arr[this.head];
            this.head = (this.head + 1) % this.maxSize;
            return value;
        }
    }

    public void showQueue() {
        if (this.isEmpty()) {
            System.out.println("队列空的,没有数据~~");
        } else {
            for(int i = this.head; i < this.head + this.size(); ++i) {
                System.out.printf("arr[%d]=%d\n", i % this.maxSize, this.arr[i % this.maxSize]);
            }

        }
    }

    public int size() {
        return (this.tail + this.maxSize - this.head) % this.maxSize;
    }

    public int headQueue() {
        if (this.isEmpty()) {
            throw new RuntimeException("队列空的,没有数据~~");
        } else {
            return this.arr[this.head];
        }
    }
}

  • 运行结果:
测试数组模拟环形队列的案例~~~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输出一个数
1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输出一个数
2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输出一个数
3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输出一个数
4
队列满,不能加入数据~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输出一个数
4
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
arr[1]=2
arr[2]=3
arr[3]=4
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e
程序退出~~

Process finished with exit code 0

【完】
:文章内所有测试用例源码:https://gitee.com/wjie2018/java-data-structure-and-algorithm.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值