Java队列(数组方式实现)

队列

队列是一个有序列表,可以用数组和链表实现\

数组实现队列

package dataStructure.Queue;

import java.util.Scanner;

/**
 * @author lhn
 * @date 2024-04-10 11:52
 */

public class ArrayQueue {
    private int maxSize; // 数组最大容量
    private int front;  //队列头
    private int rear;   //队列尾
    private int[] arr;  //该数据的存入位置

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        front = -1; //指向队列头部,指向队列头的前一个位置
        rear = -1;  //指向队列尾部,指向队列尾的数据
    }
    public boolean isFull() {
        return rear == this.maxSize - 1;
    }
    public boolean isEmpty() {
        return this.front == this.rear;
    }
    public void addQueue(int n) {
        if (isFull()){
            System.out.println("队列已满不能加入数据");
            return;
        }
        rear++;
        this.arr[rear] = n;
    }
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能取数据");
        }
        front++;
        return this.arr[front];
    }
    public void showQueue() {
        if (isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i = this.front + 1; i < this.front + this.maxSize; i++) {
            System.out.print(this.arr[i] + " ");
        }
        System.out.println(); // 换行
    }
    public int headQueue() {
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        } else {
            return this.arr[++front];
        }
    }
    public static void main(String[] args){
        //创建一个队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char Key = ' ';//接口用户输入
        Scanner s= new Scanner(System.in);
        boolean loop = true;
        //输出菜单
        while(loop){
            System.out.println("s:显示队列");
            System.out.println("e:退出程序");
            System.out.println("a:添加数据到队列");
            System.out.println("g:从队列取出数据");
            System.out.println("h:查看队列头部数据");
            Key = s.next().charAt(0);//接收一个字符
            switch (Key){
                case  's':
                    arrayQueue.showQueue();
                    System.out.println("show funish");
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value= s.nextInt();
//                    System.out.println("value is " + value);
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try{
                        int res = arrayQueue.getQueue();
                        System.out.println("去除的数据为:"+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());//捕捉异常信息
                    }
                    break;
                case 'h':
                    try{
                        int res = arrayQueue.headQueue();
                        System.out.println("去除的数据为:"+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());//捕捉异常信息
                    }
                    break;
                case 'e':
                    s.close();
                    loop = false;
                    break;
                default:
                    break;
            }

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

}

使用数组模拟环形队列

在这里插入图片描述

环形队列代码实现
package dataStructure.Queue;

import java.util.Scanner;

/**
 * @author lhn
 * @date 2024-04-12 10:32
 */

public class CircleArrayQueue {
    private int maxSize; // 数组最大容量
    private int front = 0;  //  队列头
    private int rear = 0;   //  指向队列尾部,指向队列尾的后一个位置
    private int[] arr;  //  该数据的存入位置

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
    }
    public boolean isFull() {
        return (this.rear + 1) % this.maxSize == this.front;
    }
    public boolean isEmpty() {
        return this.front == this.rear;
    }
    public void addQueue(int n) {
        if (isFull()){
            System.out.println("队列已满不能加入数据");
            return;
        }
        this.arr[rear] = n;
        rear = (rear + 1) % maxSize;    //  循环队列
    }
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能取数据");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }
    public int size() {
//        System.out.println("size() is used size is " + (rear + maxSize - front) % maxSize);
        return (rear + maxSize - front) % maxSize;
    }
    public void showQueue() {
        if (isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i = this.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("队列为空");
        } else {
            return this.arr[front];
        }
    }

    public static void main(String[] args) {
        //创建一个队列
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
        char Key = ' ';//接口用户输入
        Scanner s= new Scanner(System.in);
        boolean loop = true;
        //输出菜单
        while(loop){
            System.out.println("s:显示队列");
            System.out.println("e:退出程序");
            System.out.println("a:添加数据到队列");
            System.out.println("g:从队列取出数据");
            System.out.println("h:查看队列头部数据");
            Key = s.next().charAt(0);//接收一个字符
            switch (Key){
                case  's':
                    circleArrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value= s.nextInt();
//                    System.out.println("value is " + value);
                    circleArrayQueue.addQueue(value);
                    break;
                case 'g':
                    try{
                        int res = circleArrayQueue.getQueue();
                        System.out.println("去除的数据为:"+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());//捕捉异常信息
                    }
                    break;
                case 'h':
                    try{
                        int res = circleArrayQueue.headQueue();
                        System.out.println("头部队列的数据为:"+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());//捕捉异常信息
                    }
                    break;
                case 'e':
                    s.close();
                    loop = false;
                    break;
                default:
                    break;
            }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值