数组模拟队列

思路分析:
1、将尾指针往后移:rear+1,当front == rear 【空】
2、若尾指针rear小于队列的最大下标maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据。rear==maxSize-1【队列满】

package com.itguigu.queue;

import java.util.Scanner;

public class ArrayQueueDemo {

    public static void main(String[] args) {
        //创建一个队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' '; //接收用户输入
        Scanner input = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        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): 查看队列头的数据");
        while (loop) {
            key = input.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.show();
                    break;
                case 'e':
                    loop = false;
                    break;
                case 'a':
                    System.out.print("请输入入队列的值:");
                    int num = input.nextInt();
                    try {
                        arrayQueue.addQueue(num);
                        System.out.println("插入成功");
                    } catch (Exception e) {
                        System.out.println("数组下标越界");
                    }
                    break;
                case 'g':
                    try {
                        System.out.println("出队列:" + arrayQueue.getQueue());
                        System.out.println("出队成功");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("查看队列的头元素" + arrayQueue.headQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    System.out.println("输入错误");
            }
            if (!loop) {
                System.out.println("拜拜~");
                break;
            }
        }
    }

}

class ArrayQueue {
    private int[] arr;
    private int maxSize;
    private int front;
    private int rear;

    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        front = -1;
        rear = -1;
        arr = new int[maxSize];
    }

    public boolean isFull() {
        return rear == maxSize - 1;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    //添加数据到队列
    public void addQueue(int num) {
        if (isFull()) {
            System.out.println("队列已经满了~");
        }
        arr[++rear] = num;
    }

    //获取队列的数据,出队列
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空~");
        }
        return arr[++front];
    }

    //显示队列的所有数据
    public void show() {
        if (isEmpty()) {
            System.out.println("队列是空的呀~");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d] = %d,\t", i, arr[i]);
        }
        System.out.println();
    }

    //显示队列的头数据,注意不是取出数据
    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列为空~");
            throw new RuntimeException("队列是空的~");
        }
        return arr[front + 1];
    }
}

问题分析及优化:
1、数组只能使用一次,不能进行复用
2、将这个数组,改进成一个环形数组
思路如下:
1、front变量的含义做一个调整:front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值为0
2、rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间做出约定,rear的初始值为0
3、当队列满时,条件是:(rear+1)%maxSize=front
4、当队列为空时,条件时:rear=front
5、当我们这样分析,队列中有效的数据的个数(rear+maxSize-front)%maxSize


```java
package com.itguigu.queue;

import java.util.Scanner;

public class RoundArrayQueueDemo {

    public static void main(String[] args) {

        RoundQueue arrayQueue = new RoundQueue(3);
        char key = ' '; //接收用户输入
        Scanner input = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        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): 查看队列头的数据");
        while (loop) {
            key = input.next().charAt(0);
            switch (key) {
                case 's':
                    try {
                        arrayQueue.showQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    loop = false;
                    break;
                case 'a':
                    System.out.print("请输入入队列的值:");
                    int num = input.nextInt();
                    try {
                        arrayQueue.addQueue(num);
                        System.out.println("插入成功");
                    } catch (Exception e) {
                        System.out.println("数组下标越界");
                    }
                    break;
                case 'g':
                    try {
                        System.out.println("出队列:" + arrayQueue.getQueue());
                        System.out.println("出队成功");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("查看队列的头元素" + arrayQueue.headQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    System.out.println("输入错误");
            }
            if (!loop) {
                System.out.println("拜拜~");
                break;
            }
        }
    }
}


class RoundQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] array;

    public RoundQueue(int maxSize) {
        this.maxSize = maxSize;
        front = 0;
        rear = 0;
        array = new int[maxSize];
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public void addQueue(int num) {
        if (isFull()) {
            throw new RuntimeException("队列已经满了~");
        }
        array[rear] = num;
        rear = (rear + 1) % maxSize;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空~");
        }
        int out = array[front];
        front = (front + 1) % maxSize;
        return out;
    }

    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空~");
        }
        return array[front];
    }

    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空~");
        }
        int index = front;
        while (rear != index) {
            System.out.printf("array[%d]=%d\t", index, array[index]);
            index = (index + 1) % maxSize;
        }
        System.out.println();
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值