美文网首页
数组模拟队列

数组模拟队列

作者: lp209 | 来源:发表于2020-05-17 12:19 被阅读0次

public class ArralQuene {
public int arr[];
private int front;
private int rear;

public ArralQuene(int size) {
    if (size <= 0) {
        throw new RuntimeException("长度不能为空");
    }
    arr = new int[size];
}

public boolean isEmpty() {

    return front == rear;

}

public boolean isFull() {
    return rear == arr.length;
}

public void add(int a) {
    if (isFull()) {
        System.out.println("满了");
        return;
    }
    arr[rear] = a;
    rear++;
}

public int get() {
    if (isEmpty()) {
        System.out.println("没了");
        throw new RuntimeException("异常");
    }
    return arr[front++];
  
}

public int length() {
    return rear - front;
}

}

相关文章

  • HDUOJ-1026 Ignatius and the Prin

    解题思路 广搜 使用队列来模拟广搜 数组模拟队列 使用1维数组来模拟队列,head为当前队列头,tail-1为当前...

  • 数据结构之队列

    什么是队列 队列是一个有序列表, 可以用数组或链表实现 先入先出 使用数组模拟队列和环形队列 用数组模拟队列 思路...

  • 队列ADT

    /***************利用数组模拟循环队列***************/ #include "stdi...

  • 数组模拟队列

    public class ArralQuene {public int arr[];private int fro...

  • 数组模拟队列

  • javascript第七章

    栈和队列: js中没有专门的栈和队列类型,都是用普通该数组模拟的。 何时: 只要希望按照顺序使用数组元...

  • 数组模拟环形队列

    思路 代码 package com.atguigu.queue; import java.util.Scanner...

  • 数组模拟环形队列

  • 数组模拟队列和环形队列

    方式均根据先进先出的思想,按照双指针的方式实现;首先指针一 rear 代表了队列尾端,每新增一个数据则放置在队列尾...

  • JavaScript⑦数组队列

    栈和队列: js中没有专门的栈和队列类型,都是用普通该数组模拟的。 何时: 只要希望按照顺序使用数组元素时 栈: ...

网友评论

      本文标题:数组模拟队列

      本文链接:https://www.haomeiwen.com/subject/wznjohtx.html