Java数组模拟简单队列实现(顺序队列,环形队列)

  1. 顺序队列实现
//使用数组模拟队列
class ArrayQue{
    //表示数组最大容量
    private int maxSize;
    //队列头
    private int front;
    //队列尾
    private int rear;
    //该数组用于存放数据
    private int[] arr;

    //创建队列的构造器
    public ArrayQue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        //指向队列头部,分析出front指向队列头的前一个位置
        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 ++;//让rear 后移
        arr[rear] = n;
    }

    //数据出队列
    public int getQueue(){
        //判断队列是否为空
        if (isEmpty()){
            System.out.println("队列为空,没有数据");
            throw new RuntimeException("队列空,没有数据");
        }
        front ++;//让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. 环形队列
//使用数组模拟环形队列
// 1. rear默认值为0, 指向最后一个元素的后一个位置
// 2. front默认值为0,指向队列的第一个元素
class CircleArray{
    //表示数组最大容量
    private int maxSize;
    //队列头
    private int front;
    //队列尾
    private int rear;
    //该数组用于存放数据
    private int[] arr;

    //创建队列的构造器
    public CircleArray(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        //指向队列头部,分析出front指向队列头的前一个位置
        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 = (rear + 1) % maxSize;
    }

    //数据出队列
    public int getQueue(){
        //判断队列是否为空
        if (isEmpty()){
            System.out.println("队列为空,没有数据");
            throw new RuntimeException("队列空,没有数据");
        }
        //front 是指向队列的第一个元素
        // 1. 先把front对应的值保存到临时变量
        // 2. 将front 后移
        int res = arr[front];
        front = (front + 1) % maxSize;
        return res;
    }

    //显示队列所有数据
    public void showQueue(){
        //遍历
        if (isEmpty()){
            System.out.println("队列空,没有数据");
            return;
        }
        //从front 开始遍历,遍历多少个元素 也就是判断数组中到底有多少个元素
        int size = size();
        for (int i = front; i < front + size; i++) {

            System.out.printf("arr[%d]=%d\n",i % maxSize,arr[(i % maxSize)]);

        }

    }

    //求出当前队列有效数据个数
    public int size(){
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列的头数据,不是取出数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列空,没有数据");
        }

        return arr[front];
    }
}

测试:

public class ArrayQueueCircle {
    public static void main(String[] args) {
    	//创建一个队列
        ArrayQue arrayQue = new ArrayQue(3);
		//创建一个环形队列
        CircleArray arrayCir = new CircleArray(4);
        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':
                    arrayQue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    arrayQue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = arrayQue.getQueue();
                        System.out.println("取出的数据是:"+res);
                    } catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQue.headQueue();
                        System.out.println("队列头的数据是:"+res);
                    } catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    loop = false;
                    scanner.close();
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

参考:尚硅谷Java数据结构

posted @ 2020-04-09 22:40  胖子坠  阅读(26)  评论(0)    收藏  举报