亲宝软件园·资讯

展开

java数组循环队列

小辉小辉- 人气:0

 从顶部进去数据,从底部出来数据,用数组实现队列,但是下面这个队列,只能进行一次存数值,取数值,不够完善。

import java.util.Scanner;
 
public class ArrayQueueDemo {
    public static  void main(String[]args){
        //定义队列大小maxsize
        ArrayQueue arrayQueue=new ArrayQueue(3);
        Scanner scanner=new Scanner(System.in);
        char key=' ';
        boolean  leap=true;
        while(leap){
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("h(head):显示头条数据");
            System.out.println("a(add):添加数据");
            System.out.println("g(get):从程序中取出队列");
            System.out.println("请输入一个数");
 
            key= scanner.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
 
                case 'h':
                    System.out.printf("头数据为%d",arrayQueue.showHead());
                    break;
                case 'a':
                    System.out.println("输入添加数据");
                   int data= scanner.nextInt();
                    arrayQueue.addQueue(data);
                    break;
                case 'g':
                   arrayQueue.getQueue();
                    break;
                case 'e':
                    leap=false;
                     break;
                default:
                    break;
 
 
            }
        }
 
    }
 
}
class ArrayQueue{
    private  int maxsize;//队列大小
    private int rear;
    private int front;
    private int [] arry;
    public ArrayQueue(int maxsize1){
        maxsize=maxsize1;
        arry=new int[maxsize];
        front =-1;
        rear=-1;
    }
    //判断队列是否满
    public boolean isFull(){
        return rear==maxsize-1;
    }
    //增加数据
    public  void addQueue(int data){
        if (isFull()){
            System.out.println("队列满了");
        }
        else{
            arry[rear+1]=data;
            rear++;
 
 
        }
    }
    //显示数据
    public  void showQueue(){
        for (int i = 0; i < arry.length; i++) {
            System.out.println();
            System.out.printf("%d",arry[i]);
 
        }
    }
    //是否空
    public boolean isEmpety(){
        return rear== front;
    }
    //显示头数据
    public int showHead() {
        if (isEmpety()) {
 
            throw new RuntimeException("队列是空的");
        }
        return arry[front +1];
 
    }
    public int getQueue(){
        if (isEmpety()){
            throw new RuntimeException("队列是空");
 
        }
        System.out.printf("去除一个队列为%d",arry[++front]);
        arry[front]=0;
 
        return 0;
 
    }
 
 
 
 
}

 循环队列,可以进行数据插入和取出,是上个普通队列的增强版。上个队列只能使用一次的情况被解决了

import java.util.Scanner;
 
public class CircleArrayQueueDemo {
    public  static void main(String[] args){
        //定义队列大小maxsize
        cirCleArray cirCleArray=new cirCleArray(3);//队列大小是三,但是有效数据是两个
        Scanner scanner=new Scanner(System.in);
        char key=' ';
        boolean  leap=true;
        while(leap){
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("h(head):显示头条数据");
            System.out.println("a(add):添加数据");
            System.out.println("g(get):从程序中取出队列");
            System.out.println("请输入一个数");
 
            key= scanner.next().charAt(0);
            switch (key){
                case 's':
                    cirCleArray.showCircleArray();
                    break;
 
                case 'h':
                    System.out.printf("头数据为%d",cirCleArray.showHead());
                    break;
                case 'a':
 
                   cirCleArray.addQueue();
                    break;
                case 'g':
                    cirCleArray.getQueue();
                    break;
                case 'e':
                    leap=false;
                    break;
                default:
                    break;
 
 
            }
 
 
    }
 
    }
}
    class cirCleArray{
        private int maxsize;
        private int front;
        private int rear;
        private int [] arry;
        Scanner scanner=new Scanner(System.in);
        public cirCleArray(int maxsize1){
            this.maxsize=maxsize1;
          arry=new int[maxsize];
            front=0;
            rear=0;
        }
        //显示队列
        public void showCircleArray(){
            if (isEmpty()){
                System.out.println("队列为空");
            }
            else{
                for (int i =front; i < front+size(); i++) {
                    System.out.printf("arry[%d]=%d\n",i % maxsize,arry[i % maxsize]);
                }
            }
 
        }
        public  int size(){
            return (rear+maxsize-front)%maxsize;
        }
        //队列为空
        public boolean isEmpty(){
            return front==rear;
        }
        //队列为满
        public boolean isFull(){
            return (rear+1)%maxsize==front;
        }
        //添加数据
        public void addQueue(){
 
           if (isFull()){
               System.out.printf("队列满了");
           }
           else{
               System.out.printf("输入添加的数据");
               int  data=scanner.nextInt();
               arry[rear]=data;
             if (rear<maxsize-1){
                 rear++;
             }
             else {
                 rear=(rear+1)%maxsize;
             }
 
           }
 
 
        }
        //取出数据
        public void getQueue(){
            if (isEmpty()){
                System.out.println("队列是空的");
            }
            else{
                System.out.printf("取出数据%d",arry[front]);
//               arry[front]=0;
//                front++;
                front=(front+1)%maxsize;
 
            }
        }
        //显示头数据
        public int showHead() {
            if (isEmpty()) {
 
                throw new RuntimeException("队列是空的");
            }
            return arry[front];
 
        }
    }

加载全部内容

相关教程
猜你喜欢
用户评论