数组队列和循环数组队列

public class ArrayQueueDemo {

    public static void main(String[] args) {

        ArrayQueue queue = new ArrayQueue(3);
        boolean flag=true;
        char key=' ';//接收用户输入
        Scanner scanner = new Scanner(System.in);
        while (flag){
            System.out.println("s:显示队列");
            System.out.println("e:退出程序");
            System.out.println("a:添加数据到队列");
            System.out.println("g:队列取数据");
            System.out.println("h:查看队列头");
       key= scanner.next().charAt(0);
        switch (key){
            case 's':
                queue.selectQueue();
                break;
            case 'e':
               scanner.close();
               flag=false;
               break;
            case 'a':
                System.out.println("请输入一个值");
                int i = scanner.nextInt();
                queue.addQueue(i);
                break;
            case 'g':
                try {
                    int one = queue.getOne();
                    System.out.println("取出得数据是"+one);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 'h':
                try {
                    int head = queue.headQueue();
                    System.out.println("取出得队列头数据是"+head);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
        }
        System.out.println("程序退出");
    }
}
//使用数组模拟ArrayQueue类
class  ArrayQueue{

     private int maxSize;
     private int front;//队列头
     private int rear;//队列尾
     private int[] arr;//数组模拟队列

    //创建队列的构造器
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr=new int [this.maxSize];
        front=-1;//指向队列头的数据的前一个(无数据)
        rear=-1;//指向队列尾的最后一个数据(有数据)
    }

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

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

    //添加数据到队列
    public void addQueue(int n){
        if(isFull()){
            System.out.println("队列已满");
            return;//结束这个空方法
        }
        rear++;
        arr[rear]=n;
    }

    //获取队列的某个数据
    public int getOne(){
        if(isEmpty()){
                throw new RuntimeException("队列为空");
                //throw会导致直接结束,而不用使用return,if判断解决返回参数的方法。
        }
        front++;
        return arr[front];
    }
    //遍历队列的数据
    public void selectQueue(){

            if(isEmpty()){
                System.out.println("队列为空");
               return; //结束这个空方法
            }
            for(int arr1:arr){
                System.out.println(arr1);
            }
    }
    //显示队列的头数据,不是取出数据
    public int headQueue(){
        //判断
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front+1];
    }
}

问题所在:只能使用一次,当数组里的值被取出来后无法在使用:
解决方法:使用循环数组队列:

public class CircleArrayQueue {

    public static void main(String[] args) {
        CircleArray queue = new CircleArray(4);//有效数据是3
        boolean flag=true;
        char key=' ';//接收用户输入
        Scanner scanner = new Scanner(System.in);
        while (flag){
            System.out.println("s:显示队列");
            System.out.println("e:退出程序");
            System.out.println("a:添加数据到队列");
            System.out.println("g:队列取数据");
            System.out.println("h:查看队列头");
            key= scanner.next().charAt(0);
            switch (key){
                case 's':
                    queue.selectQueue();
                    break;
                case 'e':
                    scanner.close();
                    flag=false;
                    break;
                case 'a':
                    System.out.println("请输入一个值");
                    int i = scanner.nextInt();
                    queue.addQueue(i);
                    break;
                case 'g':
                    try {
                        int one = queue.getOne();
                        System.out.println("取出得数据是"+one);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'h':
                    try {
                        int head = queue.headQueue();
                        System.out.println("取出得队列头数据是"+head);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }

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


class  CircleArray{

    private int maxSize;
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;//数组模拟队列

    public CircleArray(int maxSize) {
        this.maxSize = maxSize;
        arr=new int [this.maxSize];
        front=0;//指向队列头的第一个数据(有数据)
        rear=0;//指向队列尾的最后一个数据的后一个位置(无数据)
    }

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

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

    //添加数据到队列
    public void addQueue(int n){
        if(isFull()){
            System.out.println("队列已满");
            return;//结束这个空方法
        }
        arr[rear]=n;
        rear=(rear+1)%maxSize;
    }
    
    //获取队列的某个数据
    public int getOne(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
            //throw会导致直接结束,而不用使用return,if判断解决返回参数的方法。
        }
        int i = arr[front];
         front=(front+1)%maxSize;
        return i;
    }

    //遍历队列的数据
    public void selectQueue(){

        if(isEmpty()){
            System.out.println("队列为空");
            return; //结束这个空方法
        }
        //不能用for循环遍历,因为从头遍历数组与队列的顺序无关
        //所以应该从front开始遍历

        for(int i=front;i<front+count();i++){
            System.out.println(arr[i%maxSize]);
        }
    }
    
    //返回有效个数
    public int count(){
        int i = (rear + maxSize - front) % maxSize;
        return i;
    }
    
    //显示队列的头数据,不是取出数据
    public int headQueue(){
        //判断
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值