思维导图:
1. 队列(双向链表实现)
接口设计
int size();
boolean isEmpty();
void clear();
void enQueue(E element);
E deQueue();
E front();
优先使用双向链表实现,因为队列主要是往头尾操作元素
public class Queue<E> {
private final List<E> list=new LinkList<>();
public int size(){
return list.size();
}
public boolean isEmpty(){
return list.isEmpty();
}
public void enQueue(E element){
list.add(element);
}
public E deQueue(){
return list.remove(0);
}
public E front(){
return list.get(0);
}
public void clear(){
list.clear();
}
}
2. 队列(栈实现)
思路:
实现:
public class _232_用栈实现队列 {
private final Stack<Integer> inStack = new Stack<>();
private final Stack<Integer> outStack = new Stack<>();
public void push(int x) {
inStack.push(x);
}
public int pop() {
checkOutStack();
return outStack.pop();
}
public int peek() {
checkOutStack();
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void checkOutStack() {
if (outStack.isEmpty()) {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
}
3. 双端队列(双向链表实现)
双端队列是能在头尾两端添加、删除的队列。英文 deque 是 double ended queue 的简称
接口设计:
int size();
boolean isEmpty();
void clear();
void enQueueRear(E element);
E deQueueFront();
void enQueueFront(E element);
E deQueueRear();
E front();
E rear();
实现:
public class DeQue<E> {;
private final List<E> list = new LinkedList<>();
public int size(){
return list.size();
}
public boolean isEmpty(){
return list.isEmpty();
}
public void enQueueFront(E element){
list.add(0,element);
}
public void enQueueRear(E element){
list.add(element);
}
public E deQueue(){
return list.remove(0);
}
public E front(){
return list.get(0);
}
public E rear(){
return list.get(list.size() - 1);
}
public void clear(){
list.clear();
}
}
4. 循环队列
循环队列,没有固定的头
public class CircleQueue<E> {
private int size;
private E[] elements;
private int front;
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 enQueue(E element) {
ensureCapacity(size + 1);
elements[(front + size) % elements.length] = element;
size++;
}
private void ensureCapacity(int capacity) {
int oldCapacity = elements.length;
if (capacity < oldCapacity) return;
int newCapacity = oldCapacity + (oldCapacity >> 1);
E[] newElements = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newElements[i] = elements[(i + front) % elements.length];
}
elements = newElements;
front = 0;
}
public E deQueue() {
E element = elements[front];
elements[front] = null;
front = (front + 1) % elements.length;
size--;
return element;
}
public E front() {
return elements[front];
}
public void clear() {
for (int i = 0; i < size; i++) {
elements[index(i)] = null;
}
front = 0;
size = 0;
}
private int index(int index) {
index += front;
return index - (index >= elements.length ? elements.length : 0);
}
@Override
public String toString() {
StringBuilder string = new StringBuilder();
string.append("capacity=").append(elements.length)
.append(" size=").append(size)
.append(" front=").append(front)
.append(", [");
for (int i = 0; i < elements.length; i++) {
if (i != 0) {
string.append(", ");
}
string.append(elements[i]);
}
string.append("]");
return string.toString();
}
}
5. 循环双端队列
实现:
public class CircleDeQueue<E> {
private int size;
private E[] elements;
private int front;
private static final int DEFAULT_CAPACITY = 10;
public CircleDeQueue() {
elements = (E[]) new Object[DEFAULT_CAPACITY];
}
public E deQueueRear() {
int rearIndex = index(size - 1);
E element = elements[rearIndex];
elements[rearIndex] = null;
size--;
return element;
}
public void enQueueRear(E element) {
ensureCapacity(size + 1);
elements[index(size)] = element;
size++;
}
public E deQueueFront() {
E element = elements[front];
elements[front] = null;
front = index(1);
size--;
return element;
}
public void enQueueFront(E element) {
ensureCapacity(size + 1);
front = index(-1);
elements[front] = element;
size++;
}
public E front() {
return elements[front];
}
public E rear() {
return elements[index(size - 1)];
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
private int index(int index) {
index += front;
if (index < 0) {
index += elements.length;
}
return index - (index >= elements.length ? elements.length : 0);
}
private void ensureCapacity(int capacity) {
int oldCapacity = elements.length;
if (capacity < oldCapacity) return;
int newCapacity = oldCapacity + (oldCapacity >> 1);
E[] newElements = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newElements[i] = elements[index(i)];
}
elements = newElements;
front = 0;
}
@Override
public String toString() {
StringBuilder string = new StringBuilder();
string.append("capacity=").append(elements.length)
.append(" size=").append(size)
.append(" front=").append(front)
.append(", [");
for (int i = 0; i < elements.length; i++) {
if (i != 0) {
string.append(", ");
}
string.append(elements[i]);
}
string.append("]");
return string.toString();
}
}
|