datastructure:环形数组实现队列

本文详细介绍了如何使用环形数组来实现队列的数据结构,重点讨论了Java中的实现细节,包括队列的基本操作如入队、出队以及相关的效率分析。
摘要由CSDN通过智能技术生成
front指向队列的第一个元素, arr[front] 就是队列的第一个元素。
front的初始值 = 0

rear指向队列的最后一个元素的后一个位置。空出最后一个空间做为约定。
rear的初始值 = 0

front = 0, rear = 1 表示此时只有1个数据。
front指的这个数据的位置,rear指的这个数据的后一个位置。

当队列满时,条件是 (rear + 1) % maxSize == front 【满】
队列为空的条件,rear == front
当我们这样分析, 队列中有效的数据的个数 (rear - front + maxSize) % maxSize
我们就可以在原来的队列上修改得到,一个环形队列


如:有一个环形队列,有8个元素空间(下标为1-7),此时front = 0, rear = 0;
现在依次入队A、B、C、D、E、F、G、H。
A在下标为0处、此时front = 0, rear = 1;
B在下标为1处、此时front = 0, rear = 2;
C在下标为2处、此时front = 0, rear = 3;
A出队,此时,front = 1, rear = 3;
D在下标为3处、此时front = 1, rear = 4;
E在下标为4处、此时front = 1, rear = 5;
F在下标为5处、此时front = 1, rear = 6;
G在下标为6处、此时front = 1, rear = 7;
H现在要入队:首先,先把H放在rear的位置,即H放在element[rear],然后rear=rear+1=8,
由于下标最大为7,没有下标为8的位置,所以要8%8=0,此时rear=0,才能实现首尾相连。
因此入队调整语句为:rear = (rear + 1) % maxSize。

如果I入队,rear = 1,此时队满,front == rear == 1。
队空的判断条件:rear == front,程序无法判断是队空还是队满。

解决方案:1.另外设置一个标识符或count统计队列总元素的个数。
        2.不允许I入队,在rear和front之间保留一个空间。
          即不允许出现rear + 1 == front计算,改为(rear + 1) % maxSize == front(队满的判断)。

结论:
    队列初始化:rear = front = 0;
    队空判断:rear == front;
    队满判断:(rear + 1) % maxSize == front;
import java.util.Scanner;

public class CircleArrayQueue {
    public static void main(String[] args) {
        // 创建一个环形队列,队列的有效数据个数最大为3
        // 在rear和front之间保留一个空间
        // 预留空间从arr[3]开始动态变化
        CircleArray queue = new CircleArray(4);
        // 接受用户输入
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("d(display):显示队列");
            System.out.println("a(add):添加数据");
            System.out.println("g(get):取出头数据");
            System.out.println("h(head):查看头数据");
            System.out.println("e(exit):退出程序");
            System.out.println("请输入对应的指令:");
            key = scanner.next().charAt(0);
            switch (key) {
                case 'd':
                    queue.displayQueue();
                    break;
                case 'a':
                    System.out.println("输入一个数:");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.println("取出的头数据是" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.println("头数据是" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    System.out.println("输入的指令有误!");
                    break;
            }
        }
        System.out.println("程序已退出。");
    }
}

// 使用环形数组实现队列
class CircleArray {
    // 数组的最大容量
    private int maxSize;
    // 指向队列的第一个元素
    private int front;
    // 指向队列的最后一个元素的最后一位
    private int rear;
    // 数组用来存放数据,模拟队列
    private int[] arr;

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

    // 判断队列是否满
    public boolean isFull() {
        // 在rear和front之间保留一个空间,预留空间从arr[3]开始动态变化
        return (this.rear + 1) % maxSize == this.front;
    }

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

    // 添加数据到队列
    public void addQueue(int n) {
        // 判断队列是否满
        if (isFull()) {
            System.out.println("队列满,不能添加数据。");
            return;
        }
        this.arr[this.rear] = n;
        // rear后移,必须考虑取模,防止下标越界
        this.rear = (this.rear + 1) % maxSize;
    }

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

    // 显示队列的所有数据
    public void displayQueue() {
        if (isEmpty()) {
            System.out.println("队列空,没有数据");
        }
        for (int i = front; i < front + size(); i++) {
            // i可能会越界
            System.out.println("arr[" + i % maxSize + "] = " + this.arr[i % maxSize]);
        }
    }

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

    // 显示队列的头数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空,没有数据");
        }
        return this.arr[this.front];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值