数据结构-队列

一. 队列介绍

队列是一个有序列表, 可以用数组或是链表来实现
遵循先入先出的原则。 即: 先存入队列的数据, 要先取出。 后存入的要后取出

  • maxSize :队列容量(数组的长度)
  • arr :模拟队列的数组
  • front :指向队列头部元素的前一个元素,初始值为 -1
  • rear :指向队列尾部元素,初始值为 -1

二. 数组实现

非循环队列

    // 表示数组的最大容量
    private int maxSize;
    // 队列头
    private int front;
    // 队列尾
    private int rear;
    // 该数据用于存放数据, 模拟队列
    private int[] arr;

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

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

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列空的,没有数据~~");
            return;
        }
        for (int i = front + 1; i <= rear; i++) {
            System.out.printf("arr[%d]=%d
", i, arr[i]);
        }
    }

    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空,不能取数据");
        }
        return arr[front++];
    }

    public static void main(String[] args) {
        // 测试一把
        // 创建一个队列
        ArrayQueue queue = new ArrayQueue(3);
        queue.showQueue();
        queue.addQueue(5);
        queue.addQueue(4);
        queue.showQueue();
        System.out.println(queue.getQueue());
    }
}```
循环队列(浪费一个空间)

public class CircleArray {
// 表示数组的最大容量
private int maxSize;
//front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素
//front 的初始值 = 0
private int front;
//rear 变量的含义做一个调整: rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
//rear 的初始值 = 0
// 队列尾
private int rear;
// 该数据用于存放数据, 模拟队列
private int[] arr;

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

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 + 1) % maxSize;
}

public int getQueue() {
    if (isEmpty()) {
        throw new RuntimeException("队列空, 不能取数据");
    }
    int value = arr[front];
    front = (front + 1) % maxSize;
    return value;
}

public void showQueue() {
    if (isEmpty()) {
        System.out.println("队列空的, 没有数据~~");
        return;
    }
    for (int i = front; i < front + size(); i++) {
        System.out.printf("arr[%d]=%d
", i % maxSize, arr[i % maxSize]);
    }
}

public int size() {
    return (rear + maxSize - front) % maxSize;
}

public static void main(String[] args) {
    CircleArray queue = new CircleArray(3);
    queue.addQueue(1);
    queue.addQueue(2);
    queue.addQueue(3);
    queue.showQueue();
    queue.getQueue();
    queue.addQueue(5);
    queue.showQueue();
}

}

原文地址:https://www.cnblogs.com/gcm688/p/14662891.html