Java数据结构:队列
导读:
数据结构示意图↓
public class queue<T> {
private int maxSize; //队列初始化大小
private Object[] arr; //定义数组
private int front = 0; //队头指针
private int rear = 0; //队尾指针
/**
* 初始化指定大小队
*/
public queue(int size) {
this.maxSize = size;
this.arr = new Object[maxSize];
}
/**
* 入队
*/
public void offer(T t) {
this.arr[rear++] = t;
}
/**
* 出队
*/
public void poll() {
Object tmp = arr[front];
arr[front++] = null;
}
public int getMaxSize() {
return maxSize;
}
public int getFront() {
return front;
}
public int getRear() {
return rear;
}
}
public class queueT {
public static void main(String[] args) {
queue<Integer> qe = new queue(5);
qe.offer(101);
qe.offer(101);
qe.offer(101);
System.out.println(qe.getRear() - qe.getFront());
qe.poll();
System.out.println(qe.getRear() - qe.getFront());
}
}
运行结果如下:
参考文章: