数据结构-队列java实现

博文主要是自己学习的笔记,供自己以后复习使用,
参考的主要教程是B站的
尚硅谷数据结构和算法

队列(queue)

1.队列的特点

1)队列是一个有序列表,可以用数组或者链表来实现
2)遵循先进先出的原则:先存入队列的数据,要先取出。
用数组模拟队列的示意图:
在这里插入图片描述
初始化:rear=front=-1,都指向队列的前一个元素
入队:rear++
出队:front++
判空:rear==front
判满:rear = maxSize - 1

2.数组模拟队列JAVA代码

class ArrayQueue {
    private int maxSize;//最大容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;//存放数组

    //创建队列的构造器
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;
        rear = -1;
    }

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

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

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

    //获取队列的数据,出队列
    public int getQueue() {
        if (isEmpty()) {
            //通过抛出异常
            throw new RuntimeException("队列为空");
        } else {
            front++;
            return arr[front];
        }
    }

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

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

测试代码

public class ArrayQueueDemo {
    public static void main(String[] args) {
        //测试
        ArrayQueue arrayQueue = 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); //接受一个字符
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入数据");
                    int data = scanner.nextInt();
                    arrayQueue.addQueue(data);
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':

                    try {
                        int res = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
    }
}

3.上述过程优化

上述代码存在问题:
队列不管是存还是取都是++操作,因此上述队列只能用一次
**优化手段:**改为环形的队列,利用取模的操作实现。

初始化:rear=front=0,front指向当前元素,rear指向当前元素的后一个位置,空出一个空间作为约定
入队:rear = (rear+1)%maxSize
出队:front= (front+1)%maxSize
判空:rear==front
判满:(rear+1)%maxSize = front
有效数据个数(rear - front + maxSize) % maxSize
JAVA代码实现

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

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

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

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

    //添加数据到队列
    public void addQueue(int data) {
        if (isFull()) {
            System.out.println("队列已满");
        } else {
            arr[rear] = data;
            rear = (rear + 1) % maxSize;
        }
    }

    //获取队列的数据,出队列
    public int getQueue() {
        if (isEmpty()) {
            //通过抛出异常
            throw new RuntimeException("队列为空");
        } else {
            int temp = arr[front];
            front = (front + 1) % maxSize;
            return temp;
        }
    }

    //显示队列的所有数据
    public void showQueue() {
        //遍历
        if (isEmpty()) {
            System.out.println("队列空,没有数据~~~");
            return;
        }
        for (int i = front; i < front +size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

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

    //显示队列的头数据,注意不是取数据
    public int size() {
        return (rear - front + maxSize) % maxSize;
    }
}

测试代码

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        //测试
        CircleArray arrayQueue = 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); //接受一个字符
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入数据");
                    int data = scanner.nextInt();
                    arrayQueue.addQueue(data);
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':

                    try {
                        int res = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
    }
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值