韩顺平老师——数组模拟队列和环形队列代码实现

先用数组表示队列代码如下:

public class ArrayQueueDemp {
    public static void main(String[] args) {
        ArrayQueue queue = 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':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个要插入的数据:");
                    int value = scanner.nextInt();
                    try{
                        queue.add(value);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int i = queue.getQueue();
                        System.out.println("取出的数据为:" + i);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int k = queue.headQueue();
                        System.out.println("队列当前的队头是:" + k);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + key);
            }
        }
    }
}

//使用数组模拟队列,编写一个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[maxSize];
        front = -1; //指向队列头部,(队头的前一个位置)
        rear = -1;  //指向队尾,即队列的最后一个数据
    }

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

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

    //添加数据
    public void add(int value) {
        //先判断队列是否满
        if (isFull()) {
            System.out.println("队列满,无法添加数据");
            return;
        }
        rear++; //尾指针后移
        arr[rear] = value;
    }

    //出队
    public int getQueue() {
        if (isEmpty()) {
            //为了避免返回整型引起歧义,用异常的方式处理
            throw new RuntimeException("队列为空,取出失败!");
        }

        front++;  //头指针后移
        return arr[front];
    }

    //显示队列所有数据
    public void showQueue() {
        //先判断是否为空
        if (isEmpty()) {
            System.out.println("队列为空,无数据");
            return;
        }
        for (int i : arr) {
            System.out.print(i + ",");
        }
    }

    //显示队列头数据,注意不是取出数据
    public int headQueue() {
        //判断是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无数据");
        }
        return arr[front + 1];
    }
}

以上代码存在复用问题,数组只能使用一次,优化此问题的思路如下:

 

public class CircleArrayQueueDemo {
    public static void main(String[] args) {

    }
}

class CircleArray {
    private int maxSize; //队列最大容量
    private int front;   //队列头
    private int rear;  //队列尾
    private int[] arr;  //存储数据,模拟队列

    public CircleArray(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

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

    //添加数据到队列
    public void addQueue(int value) {
        //先判断是否满
        if (isFull()) {
            System.out.println("队列已满,不能添加数据");
            return;
        }
        //添加数据
        arr[rear] = value;
        //尾指针后移,这里必须考虑取模
        rear = (rear + 1) % maxSize;
    }

    //出队
    public int getQueue() {
        if (isEmpty()) {
            //为了避免返回整型引起歧义,用异常的方式处理
            throw new RuntimeException("队列为空,取出失败!");
        }
        //这里的front是直接指向队列第一个元素的。
        //1.先把front指向的元素存到一个临时变量中
        int temp = arr[front];
        //2.指针后移
        front = (front + 1) % maxSize;
        return temp;
    }

    //显示队列所有数据
    public void showQueue() {
        //先判断是否为空
        if (isEmpty()) {
            System.out.println("队列为空,无数据");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.print(arr[i % maxSize]);
        }
    }

    //当前队列有几个元素
    private int size() {
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列头数据,注意不是取出数据
    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、付费专栏及课程。

余额充值