队列是一种先进先出,支持队尾插入,在对头删除元素。
如有不会的地方请指出,共同提高,谢谢!!!
如下图:

下面就看下代码吧:
package com.shenqi.dataStructure.queue;
/**
* @Author:shenqi
* @Description: 数组队列
* @Date:2019/1/23 12:14
*/
public class ArrayQueue {
private int size;
private int count;
private Object[] array;
private int next;
public ArrayQueue(int size) {
this.size = size;
array = new Object[size];
}
/**
* 入队
*
* @param obj
* @return
*/
public boolean enqueue(Object obj) {
if (size == count) {
if (next == 0) return false;
// 数据搬动
for (int i = next; i < count; i++) {
array[i - next] = array[i];
}
count -= next;
next = 0;
}
array[count] = obj;
count++;
return true;
}
/**
* 出队
*
* @return
*/
public Object dequeue() {
if (count == 0) return null;
if (next == size) return null;
Object obj = array[next];
next++;
return obj;
}
}
原文:https://www.cnblogs.com/shenqiaqia/p/10470021.html