Arithmetic

JAVA-2.队列

PineappleCat · 1月19日 · 2021年 242次已读

1.介绍

2.简单使用

public class QueueDemo {
    public static void main(String[] args) {
        Queue list = new LinkedList();
        //入队
        list.offer("1");
        list.offer("2");
        list.offer("3");
        System.out.println("peek(获取第一个元素):"+list.peek());
        //出队
        int size = list.size();
        for (int i = 0; i < size; i++) {
            System.out.println(list.poll());
        }
    }
}

3.数组模拟队列

package Queue;

import org.junit.Test;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 队列:
 * 有序列表、可以用数组或链表实现
 * 先入后出原则
 *
 * @author GoodTime0313
 * @version 1.0
 * @date 2021/1/19 23:10
 */
public class Queue_1 {
    //(1).用已有实现建立队列
    @Test
    public void test1() {
        //1.LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。
        Queue<Integer> queue = new LinkedList<>();
        //2.添加元素
        //queue.add(i); 如果溢出add回报异常、而offer返回false
        queue.offer(2);
        //3.在队列的头部查询元素
        //Integer element = queue.element();
        // 队列为空时, element() 抛出一个异常,而 peek() 返回 null。
        Integer peek = queue.peek();
        System.out.println(peek);
        //4.删除第一个元素
        //queue.remove(); poll()方法在用空集合调用时不是抛出异常,只是返回 null
        queue.poll();
    }

    //(2).用数组模拟队列ArrayQueue
    @Test
    public void test2() {
        ArrayQueue queue = new ArrayQueue(3);
        queue.addQueue(1);
        queue.addQueue(2);
        queue.addQueue(3);
        System.out.println(queue.getQuene());
        queue.showQueue();
        System.out.println(queue.headQueue());
        //队列已满
        queue.addQueue(2);
    }

    //(3).用数组模拟循环队列CircleArray
    @Test
    public void test3() {
        CircleArray queue = new CircleArray(5);
        queue.addQueue(1);
        queue.addQueue(2);
        queue.addQueue(3);
        //System.out.println(queue.getQuene());
        queue.showQueue();
        System.out.println(queue.headQueue());
    }
}

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];
        this.front = -1;
        this.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;
        }
        //先后移一位 因为是从-1开始的
        rear++;
        arr[rear] = n;
    }

    //获取队列的数据,出队
    public int getQuene() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        front++;
        return arr[front];
    }

    //显示队列的所有数据(不包括空数据)
    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        for (int i = front + 1; i < rear + 1; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    //显示队列头部数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front + 1];
    }
}

//数组实现循环队列
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];
        // font与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;
        }
        //rear从下标为0开始
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    //获取队列的数据,出队
    public int getQuene() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int i = arr[front];
        front = (front + 1) % maxSize;
        return i;
    }

    //显示队列的所有数据(不包括空数据)
    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        //循环队列有效元素的个数front + (rear-front+maxSize)%maxSize
        //rear有可能在font的前面减出来就是负数所有这么表达
        for (int i = front; i < front + (rear - front + maxSize) % maxSize; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    //显示队列头部数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }
}

Click here to view the copyright notice of this site(点击此处查看本站版权声明)
0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!