我的Java数据结构学习-3----队列

队列的应用场景:

 

 队列的特性:(数组或者链表实现)-----先进先出

 

 

 

 判断队列为空:rear == front

判断队列为满:rear == MaxSize-1;

复制代码
  1 package Demo1.Queue;
  2 
  3 import java.util.Scanner;
  4 
  5 public class QueueEntity {
  6     //数组模拟的队列类
  7 
  8     //队列最大存储量
  9     private int MaxSize;
 10 
 11     //队列的头部
 12     private int front;
 13 
 14     //队列尾部
 15     private int rear;
 16 
 17     //队列容器--数组/存放数据空间,模拟队列
 18     private int[] queueArr;
 19 
 20 
 21     //创建队列的构造器创建
 22 //  创建队列的构造器创建  alt +insert:快捷键--get,set,constructor
 23 //    Ctrl+Alt+L  格式化代码
 24 //    alt +enter  补全/提示
 25 //     Ctrl+Alt+O 优化导入的类和包
 26 //    ctrl+alt+t 选中,自动选择try-catch
 27     public QueueEntity(int maxSize) {
 28         MaxSize = maxSize;
 29         //初始化队列空间
 30         queueArr = new int[MaxSize];
 31         //队列头部初始化,指向队列头部的前一个位置
 32         front = -1;
 33         //队列的尾部初始化,指向队列尾部位置数据(包含队列最后一个数据)
 34         rear = -1;
 35     }
 36 
 37 
 38     //判断队列是否满
 39     public boolean queueIsFull() {
 40         return rear == MaxSize - 1;
 41     }
 42 
 43     //判断队列是否为空
 44     public boolean queueIsNull() {
 45         return rear == front;
 46     }
 47 
 48     //添加数据到队列
 49     public void addToQueue(int number) {
 50         //判断队列是否为满
 51         if (queueIsFull()) {
 52             System.out.println("队列已经满了,不能加入数据!!");
 53             //满了不能加入,就推出
 54             for (int i = 0; i < queueArr.length; i++) {
 55                 System.out.printf("arr[%d]=%d", i, queueArr[i]);
 56             }
 57             return;
 58         }
 59         //队列加入数据,front不动,rear下移
 60         rear++;//队尾后移
 61         queueArr[rear] = number;
 62         System.out.println("PUSH---->"+number);
 63     }
 64 
 65     //队列中取数据
 66     public int getQueueHead() {
 67         //判断队列是否为空,没有的话就不能取
 68         if (queueIsNull()) {
 69             //如果是空的就不能取数据
 70             //throw new RuntimeException("队列是空的,不能取数据");
 71             return -1;
 72         }
 73         //队列取数据,rear队尾不动,队头移动
 74         front++;
 75         return queueArr[front];
 76     }
 77 
 78     //显示所有的队列信息
 79     public void showQueue() {
 80         //遍历数组
 81         if (queueIsNull()) {
 82             System.out.println("队列是空的,没有数据");
 83             return;
 84         }
 85         for (int i = 0; i < queueArr.length; i++) {
 86             System.out.printf("arr[%d]=%d", i, queueArr[i]);
 87         }
 88     }
 89 
 90     //显示队列的头部,不是取出数据
 91     public int showQueueHead() {
 92         if (queueIsNull()) {
 93             System.out.println("队列是空的,没有数据!");
 94            // throw new RuntimeException("队列是空的!!");
 95             return -1;
 96         }
 97         return queueArr[front + 1];
 98     }
 99 
100     //清空队列
101     public boolean queueBeginNull() {
102         front = -1;
103         rear = -1;
104         queueArr=null;
105         if (queueArr.length==0){
106             return true;
107         }
108         return false;
109     }
110     //删除队首
111     public  boolean deleteQueueHead(){
112         if (queueIsNull()) {
113             System.out.println("不能删除,队列为空");
114             return true;
115         }
116         int[] array = new int[queueArr.length];
117         for (int i = 0; i < queueArr.length-1; i++) {
118             array[i]=queueArr[i+1];
119         }
120         queueArr=array;
121         for (int i : queueArr) {
122             System.out.printf("%d\t",i);
123         }
124         return true;
125     }
126     //队列大小
127     public int getQueueSize(){
128         int cout =0;
129         for (int i = front+1; i < queueArr.length; i++) {
130             if (queueArr[i]!=0){
131                 cout++;
132             }
133         }
134         return cout;
135     }
136 
137     public int getMaxSize() {
138         return MaxSize;
139     }
140 
141 
142     public int getFront() {
143         return front;
144     }
145 
146     public int getRear() {
147         return rear;
148     }
149 
150     public int[] getQueueArr() {
151         return queueArr;
152     }
153 //删除队首数据
154 
155 
156     }
复制代码

 

 测试代码:

复制代码
package Demo1.Queue;

import java.util.Scanner;

public class ArrayQueue {
    //测试队列对象
    QueueEntity queueEntity = new QueueEntity(10);
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue();
        //接受用户输入的数据
        Scanner scanner = new Scanner(System.in);
        //中断输入条件
       // boolean tagScanner = true;
        System.out.println("请输入测试组操作次数:");
        int count = scanner.nextInt();
        while (count!=0){
            //输入测试用例的测试个数
            int number = scanner.nextInt();
            //遍历每次输出
            for (int i = 0; i < number; i++) {
                String Begin = scanner.next();
                if (Begin.isEmpty()){
                }else{
                    boolean tag = arrayQueue.FunctionWhile(Begin);
                    if (tag){
                        count--;
                    }

                }
            }
        }

    }
    //检测输入的值,判断调用哪一个方法
    //判断输入的指标是调用哪一个方法:while
    public boolean FunctionWhile(String temp){

        switch (temp) {
            //添加
            case "PUSH":
                Scanner scanner1 = new Scanner(System.in);
                int x1 = scanner1.nextInt();//添加数据
                queueEntity.addToQueue(x1);
                queueEntity.showQueue();
                return true;
            case "TOP":
                int head = queueEntity.showQueueHead();
                System.out.println("查看队首:TOP---》"+head);
                queueEntity.showQueue();
                return true;
            case "POP":
                int b = queueEntity.getQueueHead();
               // queueEntity.deleteQueueHead();
                System.out.println("删除队首:POP---》"+b);
                queueEntity.showQueue();
                return true;
            case "SIZE":
                int maxSize = queueEntity.getQueueSize();
                System.out.println("队列大小:"+maxSize);
                queueEntity.showQueue();
                return true;
            case "CLEAR":
                queueEntity.queueBeginNull();
                System.out.println("队列已清空!!");
                queueEntity.showQueue();
                return true;
            default:
                System.out.println("请从新输入!!指令");
                return false;
        }

    }


}
//使用数组模拟一个队列----编写一个QueueEntity
复制代码

结果:

请输入测试组操作次数:
2
7
PUSH
1
PUSH---->1
arr[0]=1arr[1]=0arr[2]=0arr[3]=0arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0PUSH
2
PUSH---->2
arr[0]=1arr[1]=2arr[2]=0arr[3]=0arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0TOP
查看队首:TOP---》1
arr[0]=1arr[1]=2arr[2]=0arr[3]=0arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0POP
删除队首:POP---》1
arr[0]=1arr[1]=2arr[2]=0arr[3]=0arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0TOP
查看队首:TOP---》2
arr[0]=1arr[1]=2arr[2]=0arr[3]=0arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0POP
删除队首:POP---》2
队列是空的,没有数据
POOP
请从新输入!!指令
5
PUSH
1
PUSH---->1
arr[0]=1arr[1]=2arr[2]=1arr[3]=0arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0PUSH
2
PUSH---->2
arr[0]=1arr[1]=2arr[2]=1arr[3]=2arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0SIZE
队列大小:2
arr[0]=1arr[1]=2arr[2]=1arr[3]=2arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0POP
删除队首:POP---》1
arr[0]=1arr[1]=2arr[2]=1arr[3]=2arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0SIZE
队列大小:1
arr[0]=1arr[1]=2arr[2]=1arr[3]=2arr[4]=0arr[5]=0arr[6]=0arr[7]=0arr[8]=0arr[9]=0
Process finished with exit code -1

posted on   白嫖老郭  阅读(95)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示