编程语言
首页 > 编程语言> > Java数组实现队列+环形队列

Java数组实现队列+环形队列

作者:互联网

一.数组实现队列

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("a:添加数据  ");
            System.out.println("b:取出数据  ");
            System.out.println("c:打印数据  ");
            System.out.println("d:返回头数据 ");
            System.out.println("e:终止程序  ");
            System.out.println("请输入一个字符:");
            String next = scanner.next();
            switch (next){
                case "a" :
                    System.out.println("请输入一个整数:");
                    int num = scanner.nextInt();
                    queue.add(num);
                    break;
                case "b" :
                    int result = queue.remove();
                    System.out.println("取出的数为:"+result);
                    break;
                case "c" :
                    queue.print();
                    break;
                case "d" :
                    int head = queue.head();
                    System.out.println("头部的数据为:"+head);
                    break;
                case "e" :
                    System.out.println("程序终止");
                    scanner.close();
                    System.exit(1);
                default:
                    System.out.println("无法识别该指令");
            }
        }
    }
}

class ArrayQueue {
    private int front;  //指向数组头部
    private int rear;   //指向数组尾部
    private int maxSize;  //数组容量
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        this.front = 0;
        this.rear = 0;
    }

    public boolean isFull() {
        return rear == maxSize;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    //插数据
    public void add(int num){
        if (isFull()){
            System.out.println("队列已满.....");
            return;
        }
        arr[rear] = num;
        rear++;
    }

    //取数据
    public int remove(){
        if(isEmpty()){
            throw new RuntimeException("队列为空....");
        }
        int num = arr[front];
        front++;
        return num;
    }

    //打印
    public void print(){
        if(isEmpty()){
            System.out.println("队列为空....");
            return;
        }
        for(int i=0;i<arr.length;i++){
            System.out.println("arr["+i+"]="+arr[i]);
        }
        System.out.println("----------");
    }

    //返回头数据
    public int head(){
        if (isEmpty()){
            throw new RuntimeException("队列为空....");
        }
        return arr[front];
    }
}

以上代码存在一个缺陷,添加3个元素之后,移除了头部元素还是不能继续添加,所以将队列改成环形队列

二.数组实现环形队列

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        {
            CicleArrayQueue queue = new CicleArrayQueue(4);
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("a:添加数据  ");
                System.out.println("b:取出数据  ");
                System.out.println("c:打印数据  ");
                System.out.println("d:返回头数据 ");
                System.out.println("e:终止程序  ");
                System.out.println("请输入一个字符:");
                String next = scanner.next();
                switch (next){
                    case "a" :
                        System.out.println("请输入一个整数:");
                        int num = scanner.nextInt();
                        queue.add(num);
                        break;
                    case "b" :
                        int result = queue.remove();
                        System.out.println("取出的数为:"+result);
                        break;
                    case "c" :
                        queue.print();
                        break;
                    case "d" :
                        int head = queue.head();
                        System.out.println("头部的数据为:"+head);
                        break;
                    case "e" :
                        System.out.println("程序终止");
                        scanner.close();
                        System.exit(1);
                    default:
                        System.out.println("无法识别该指令");
                }
            }
        }
    }
}

class CicleArrayQueue {
    private int front;  //指向数组头部
    private int rear;   //指向数组尾部
    private int maxSize;  //数组容量
    private int[] arr;

    public CicleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        this.front = 0;
        this.rear = 0;
    }

    //约定好留一个空位,否则实现不了
    public boolean isFull() {
        return (rear+1) % maxSize == front;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    //插数据
    public void add(int num) {
        if (isFull()) {
            System.out.println("队列已满.....");
            return;
        }
        arr[rear] = num;
        rear = (rear + 1) % maxSize;
    }

    //取数据
    public int remove() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空....");
        }
        int num = arr[front];
        front = (front + 1) % maxSize;
        return num;
    }

    //从front头部开始打印
    public void print() {
        if (isEmpty()) {
            System.out.println("队列为空....");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.println("arr[" + i % maxSize + "]=" + arr[i % maxSize]);
        }
        System.out.println("----------");
    }

    //返回元素的个数
    public int size() {
        return (rear + maxSize - front) % maxSize;
    }

    //返回头数据
    public int head() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空....");
        }
        return arr[front];
    }
}

标签:Java,队列,环形,System,int,maxSize,println,public,out
来源: https://blog.csdn.net/bully9/article/details/122463293