数据结构-队列

数据结构-队列

  • 有序的数据结构
  • 先进先出原则
队列是数据结构的一种,今天我们就通过数组来实现一个的队列。

实现逻辑

  • 定义两个变量分别指向队列的第一个元素的下标和最后一个元素的后一个位置
  • 约定存在一个位置不存放元素,有rear指向
  • front 第一个元素的下标值 初始值0
  • rear 最后一个元素的后一个位置的下标值 初始值0
  • 判断队列是否为满条件 (rear + 1)%maxSize == front
  • 获取当前队列有效数据个数 (rear - front + maxSize)%maxSize

代码

// 使用数组模拟队列
class ArrayCycleQueue{
    // 数组的最大容量
    private int maxSize;

    // 头部 队列的第一个元素的位置
    private int front;

    // 尾部 最后一个元素的后一个位置
    private int rear;

    // 模拟队列
    private int[] arr ;

    //创建队列的构造起
    public ArrayCycleQueue(int maxSize){
        this.maxSize = maxSize;
        arr =new int[this.maxSize];
        // 指向队列头的位置
        this.front = 0;
        // 指向队列尾的后一个位置
        this.rear = 0;
    }

    /**
     * 判断队列是否满
     * @return
     */
    public boolean isFull(){
        return (rear + 1) % maxSize == front;
    }

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

    /**
     * 添加数据
     * @param n
     */
    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 result = arr[front];

        front = (front + 1)%maxSize;

        return result;
    }

    /**
     * 显示队列所有数据
     */
    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列为空 没有数据");
            return;
        }
        for (int i = front;i< front + effectiveNums() ;i++) {
            System.out.printf("arr[%d]=%d\n",i%maxSize,arr[i%maxSize]);
        }
    }

    public int effectiveNums(){
        return (rear - front + maxSize) % maxSize ;
    }
    /**
     * 显示队列的头数据 是多少
     */
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空 不能取出数据");
        }
        return arr[front];
    }

}

测试结果

s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
s
队列为空 没有数据
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
a
输入一个数字
10
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
a
输入一个数字
20
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
h
当前队列头的数据是10
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
a
输入一个数字
30
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
a
输入一个数字
40
队列满 不能加入数据
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
g
取出的数据是10
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
s
arr[1]=20
arr[2]=30
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
a
输入一个数字
40
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据
s
arr[1]=20
arr[2]=30
arr[3]=40
s(show):显示队列
e(exit):退出
a(add):添加队列数据
g(gain):从队列取出数据
h(header):查看队列头的数据


好了今天的分享就结束啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值