# 链表

``````class Node {
int value;
Node next;
}``````

# 队列

``````public class LinkedQueue {
Node tail;

void offer(Integer value) {
if (value == null) {
throw new NullPointerException();
}
Node node = new Node(value);
if (head == null) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}
}

Integer poll() {
Node first = head;
if (first != null) {
first.next = null;
return first.value;
} else {
return null;
}
}

static class Node {
int value;
Node next;

public Node(int value) {
this.value = value;
}
}
}``````

``````public class ArrayQueue {
int[] array;
int offerIndex;
int pollIndex;
int size;

public ArrayQueue(int capacity) {
this.array = new int[capacity];
this.offerIndex = this.pollIndex = 0;
this.size = 0;
}

boolean offer(Integer value) {
if (value == null) {
throw new NullPointerException();
}

if (size == array.length) {
return false;
}

array[offerIndex] = value;
offerIndex = (offerIndex + 1) % array.length;

size++;

return true;
}

Integer poll() {
if (size == 0) {
return null;
}

int value = array[pollIndex];
pollIndex = (pollIndex + 1) % array.length;

size--;

return value;
}
}``````

OK，以上就是使用数组实现的队列，可以看到，与链表实现的队列相比，它需要指定容量，这叫做`有界队列`，如果需要使用数组实现***队列，则需要加入扩容的机制，有兴趣的同学可以自己实现看看。

# 栈

`3 + 2 * 4 -1`为例：

# 后记

(0)
(0)

© 2014 mamicode.com 版权所有 京ICP备13008772号-2