队列是一种特殊的线性表,只能在头尾两端进行操作
队尾(rear):只能从队尾添加元素,一般叫做 enQueue,入队
队头(front):只能从队头移除元素,一般叫做 deQueue,出队
先进先出的原则,First In First Out,FIFO
普通队列 - java实现
public class Queue<E> {
private LinkedList<E> list = new LinkedList<>();
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public void clear() {
list.clear();
}
// 入队
public void enQueue(E element) {
list.add(list.size() - 1, element);
}
// 出队
public E deQueue() {
return list.remove(0);
}
// 获取队列的头元素
public E front() {
return list.get(0);
}
}
双端队列 - java实现
public class Deque <E> {
private LinkedList<E> list = new LinkedList<>();
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public void clear() {
list.clear();
}
// 队尾入队
public void enQueueRear(E element) {
list.add(list.size()- 1, element);
}
// 从队头出队
public E deQueueFront() {
return list.remove(0);
}
// 从队头入队
public void enQueueFront(E element) {
list.add(0, element);
}
// 从队尾出队
public E deQueueRear() {
return list.remove(list.size()-1);
}
// 获得队头元素
public E front() {
return list.get(0);
}
// 获得队尾元素
public E rear() {
return list.peekLast();
}
}
循环队列 - java实现
public class CircleQueue <E>{
private int front;
private int size;
private E[] elements;
private static final int DEFAULT_CAPACITY = 10;
public CircleQueue() {
elements = (E[]) new Object[DEFAULT_CAPACITY];
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void clear() {
for (int i = 0; i < size; i++) {
elements[index(i)] = null;
}
front = 0;
size = 0;
}
// 入队
public void enQueue(E element) {
ensureCapacity(size + 1);
elements[index(size)] = element;
size++ ;
}
// 出队
public E deQueue() {
E frontElement = elements[front];
elements[front] = null;
front = index(1);
size-- ;
return frontElement;
}
// 队列头元素
public E front() {
return elements[front];
}
/***
* 索引 0 1 2 3 4 5 6
* 元素 11 22 33 44 55
* 此时如果往 [11, 22, 33, 44, 55] 后面加元素 应该加到 索引为 0 的位置
* 需要进行索引转换
* @return
*/
private int index(int index) {
return (front + index) % elements.length;
}
/**
* 保证要有capacity的容量
* @param capacity
*/
private void ensureCapacity(int capacity) {
int oldCapacity = elements.length;
if (oldCapacity >= capacity) return;
// 扩容
int newCapacity = oldCapacity + (oldCapacity >> 1);
E[] newElements = (E[]) new Object();
for (int i = 0; i < elements.length; i++) {
newElements[i] = elements[i];
}
elements = newElements;
// 重置头节点
front = 0;
}
}
循环双端队列 - java实现
/**
* 循环双端队列
*/
public class CircleDeque<E> {
private int front;
private int size;
private E[] elements;
private static final int DEFAULT_CAPACITY = 10;
public CircleDeque() {
elements = (E[]) new Object[DEFAULT_CAPACITY];
}
public int size() {
return size;
}
public void clear() {
for (int i = 0; i < size; i++) {
elements[i] = null;
}
size = 0;
front = 0;
}
/**
* 从尾部入队
* @param element
*/
public void enQueueRear(E element) {
ensureCapacity(size + 1);
elements[index(size)] = element;
size++ ;
}
/**
* 从头部出队
*/
public E deQueueFront() {
E first = elements[front];
elements[front] = null;
front = index(1);
size-- ;
return first;
}
/**
* 从头部入队
* @param element
*/
public void enQueueFront(E element) {
ensureCapacity(size + 1);
front = index(-1);
elements[front] = element;
size++;
}
/**
* 从尾部出队
*/
public E deQueueRear() {
E lastElement = elements[index(size - 1)];
elements[index(size - 1)] = null;
size-- ;
return lastElement;
}
public E front() {
return elements[front];
}
public E rear() {
return elements[index(size - 1)];
}
private int index(int index) {
index += front;
//
/***
* 考虑这种情况
* 索引 0 1 2 3 4 5 6
* 元素 11 22 33 44 55 66
* 往头部入队的时候 front = 0 ; index(-1) + front = -1 < 0
*/
if (index < 0) return index + elements.length;
return index % elements.length;
}
/**
* 保证要有capacity的容量
* @param capacity
*/
private void ensureCapacity(int capacity) {
int oldCapacity = elements.length;
if (oldCapacity >= capacity) return;
int newCapacity = oldCapacity + (oldCapacity >> 1);
E[] newElements = (E[]) new Object[newCapacity];
for (int i = 0; i < oldCapacity; i++) {
newElements[i] = elements[i];
}
elements = newElements;
// 重置头节点位置
front = 0;
}
}
网友评论