首页 > 其他分享 >队列

队列

时间:2023-07-26 17:47:31浏览次数:23  
标签:队列 System int front println out

实现代码:

import java.util.Scanner;

public class Test1 {
    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 value = scanner.nextInt();
                    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'://退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
    }

}
class ArrayQueue{
    private  int maxSiza;//表示数组的最大容量
    private int front;//队列头
    private int rear;//该数据用于存放数据,模拟队列
    private int[] arr;//该数据用于存放数据,模拟队列
    //创建队列的构造器
    public ArrayQueue(int arrMaxSize){
        maxSiza = arrMaxSize;
        arr = new int[maxSiza];
        front = -1;//指向队列头部,分析出front是指向队列头的前一个位置
        rear = -1;//指向队列尾,指向队列尾的数据(即就是队列的最有一个数据)

    }
    //判断队列是否满
    public boolean isFull(){
        return rear == maxSiza - 1;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }
    //添加数据到队列
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列满,不能加入数据");
            return;
        }
        rear++;
        arr[rear] =n;
    }
    //获取队列数据 出队列
    public int getQueue(){
        //判断队列是否为空
        if (isEmpty()){
           throw new RuntimeException("队列为空,不能取数据");

        }
        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];
    }
}

问题:数组只能使用一次,不能复用

改进:将这个数组使用算法,改进称环形队列

环形队列思路分析

1.front变量的含义做一个调整:front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素

2.rear变量含义做一个调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定

3.当队列满时:条件是:(rear+1)% maxSize=front

4.队列为空:rear==front 空

5.当我们这样分析,队列中有效数据的个数:(rear+maxSize-front)%maxSize

代码实现:

import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        //测试
        CircleArray arrayQueue = new CircleArray(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 value = scanner.nextInt();
                    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'://退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
    }
}
class CircleArray{
    private  int maxSiza;//表示数组的最大容量
    private int front;//队列头 初始值为0
    private int rear;//队尾 初始值为0
    private int[] arr;//该数据用于存放数据,模拟队列
    public CircleArray(int arrmaxSiza){
        maxSiza = arrmaxSiza;
        arr = new int[maxSiza];
    }
    //判断队列是否满
    public boolean isFull(){
        return (rear + 1)%maxSiza ==front;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return front==rear;
    }
    //添加数据
    public void addQueue(int n ){
        //判断队列是否满
        if (isFull()){
            System.out.println("队列满,不能添加数据");
            return;
        }
        //直接加入数据
        arr[rear] = n;
        //讲rear后移,考虑取模
        rear= (rear+1)%maxSiza;

    }
    public int getQueue(){
        //判断队列是否为空
        if (isEmpty()){
            throw new RuntimeException("队列空,不能取数据");
        }
        //这里需要分析出front是指向队列的第一个元素
        //1.先把front对应的值保存到一个临时变量
        //2.讲front后移
        //3.将临时保存的变量返回
        int value= arr[front];
        front=(front+1)%maxSiza;
        return value;
    }
    //显示队列的所有数据
    public void showQueue(){
        //遍历
        if (isEmpty()){
            System.out.println("队列空,没有数据");
            return;
        }
        //思路:从front开始遍历,遍历多少个元素
        for (int i = front; i < front+(rear-front+maxSiza)%maxSiza ; i++) {
            System.out.printf("arr[%d]=%d\n",i%maxSiza,arr[i%maxSiza]);
        }
    }
    //显示队列的头数据
    public int headQueue(){
        //判断
        if (isEmpty()){
            throw new RuntimeException("队列空的,没有数据");
        }
        return arr[front];
    }
}

标签:队列,System,int,front,println,out
From: https://www.cnblogs.com/lin513/p/17583116.html

相关文章

  • 数据结构练习笔记——循环队列的基本操作
    循环队列的基本操作【问题描述】根据循环队列的类型定义,完成循环队列的基本操作。主函数中测试队列。【输入形式】一个整数m,表示入队的元素个数【输出形式】第一行:输出队头元素第二行:队列中元素依次出队以空格间隔【样例输入】5【样例输出】113579【样例输入】0【样......
  • Redis实现消息队列
    Redis基于内存,高性能并且提供多种数据结构供使用,那么对于Redis能不能作为消息队列?以及与专业的消息队列,如RocketMQ,Kafka等差距又在哪里?Redis提供多种方式实现消息队列,基于List,基于Pub/Sub等,如今基本广泛使用的是Redis5.0之后推出的Stream流格式,其具有支持持久化,支持消......
  • 阻塞的队列
    BLockingQueue是一个阻塞的队列,最典型的应用场景就是生产者和消费者模式。生产者和消费者模式是通过一个容器来解决生产者和消费者的强耦合问题。生产者和消费者彼此并不直接通信,而是通过阻塞队列进行通信,所以生产者生产完数据后不用等待消费者进行处理,而是直接扔给阻塞队列,消费......
  • 【学习笔记】单调队列和单调栈
    单调栈以这道题为例:P5788。我们考虑维护一个单调栈,里面存的是下标,使里面的下标对应的元素从栈顶到栈底是单调上升的。我们从\(n\rightarrow1\)枚举\(a_i\)对于每个\(i\),如果栈非空,令栈顶的下标为\(j\),若\(a_j\)不比\(a_i\)大,那么这个栈顶元素由于值又小,位置又靠后,如......
  • php redis消息队列
    1、php如何把key存储在不同的redis分片上2、php怎么查看redis的key3、用phpredis操作redis集群支持publish和subscribe吗4、php2018怎么安装redis5、redis使用php怎么进行更新php如何把key存储在不同的redis分片上php如何把key存储在不同的redis分片上redis集群部署方式......
  • LeetCode 406. 根据身高重建队列
    classSolution{public:structnode{intval;intpre;node*next;node(inta,intb,node*c){val=a;pre=b;next=c;}};voidinsert(node*&head,int......
  • 6 栈与队列
    栈与队列1栈与队列基础栈:先进后出栈是以底层容器完成其所有的工作,对外提供统一的接口,底层容器是可插拔的(也就是说我们可以控制使用哪种容器来实现栈的功能)。我们常用的SGISTL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。2用栈实现队列题目:......
  • .net消息队列
    .NET消息队列消息队列是一种常用的软件架构模式,可以实现异步通信和解耦合。在分布式系统中使用消息队列可以提高系统的可伸缩性和可靠性。.NET框架提供了一个称为.NET消息队列(.NETMessageQueue,简称MSMQ)的组件,用于在应用程序之间发送消息。什么是.NET消息队列?.NET消息队列是一......
  • 【优先队列】【堆排序实现优先队列】[1054. 距离相等的条形码](https://leetcode.cn/p
    【优先队列】【堆排序实现优先队列】1054.距离相等的条形码在一个仓库里,有一排条形码,其中第i个条形码为barcodes[i]。请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。你可以返回任何满足该要求的答案,此题保证存在答案。示例1:输入:barcodes=[1,1,1,2,2,2]......
  • day10 栈与队列
    232.用栈实现队列题解:这一题在大学的时候学过,用两个栈来实现队列,队列是先进先出,而栈是先进后出,所以需要两个栈一个用来存队列入队的数据,出队列的时候,需要将顺序调转,这时候就需要用到另一个队列,注意好边界条件就行225.用队列实现栈题解:队列实现栈的功能也不难,主要是想到栈......