2019-04-21 10:36:08 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-03-27 20:57:37 +08:00
|
|
|
|
* [栈](#栈)
|
|
|
|
|
* [1. 数组实现](#1-数组实现)
|
|
|
|
|
* [2. 链表实现](#2-链表实现)
|
|
|
|
|
* [队列](#队列)
|
2019-04-21 10:36:08 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 栈
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-09 23:42:50 +08:00
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public interface MyStack<Item> extends Iterable<Item> {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
MyStack<Item> push(Item item);
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Item pop() throws Exception;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
boolean isEmpty();
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
int size();
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
## 1. 数组实现
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public class ArrayStack<Item> implements MyStack<Item> {
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
// 栈元素数组,只能通过转型来创建泛型数组
|
|
|
|
|
private Item[] a = (Item[]) new Object[1];
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
// 元素数量
|
|
|
|
|
private int N = 0;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public MyStack<Item> push(Item item) {
|
|
|
|
|
check();
|
|
|
|
|
a[N++] = item;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Item pop() throws Exception {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
if (isEmpty()) {
|
|
|
|
|
throw new Exception("stack is empty");
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Item item = a[--N];
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
check();
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
// 避免对象游离
|
|
|
|
|
a[N] = null;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
return item;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private void check() {
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
if (N >= a.length) {
|
|
|
|
|
resize(2 * a.length);
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
} else if (N > 0 && N <= a.length / 4) {
|
|
|
|
|
resize(a.length / 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* 调整数组大小,使得栈具有伸缩性
|
|
|
|
|
*/
|
|
|
|
|
private void resize(int size) {
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Item[] tmp = (Item[]) new Object[size];
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
tmp[i] = a[i];
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
a = tmp;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return N == 0;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public int size() {
|
|
|
|
|
return N;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Iterator<Item> iterator() {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
// 返回逆序遍历的迭代器
|
|
|
|
|
return new Iterator<Item>() {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private int i = N;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return i > 0;
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Item next() {
|
|
|
|
|
return a[--i];
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
## 2. 链表实现
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
需要使用链表的头插法来实现,因为头插法中最后压入栈的元素在链表的开头,它的 next 指针指向前一个压入栈的元素,在弹出元素时就可以通过 next 指针遍历到前一个压入栈的元素从而让这个元素成为新的栈顶元素。
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-09 23:42:50 +08:00
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public class ListStack<Item> implements MyStack<Item> {
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private Node top = null;
|
|
|
|
|
private int N = 0;
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private class Node {
|
|
|
|
|
Item item;
|
|
|
|
|
Node next;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public MyStack<Item> push(Item item) {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Node newTop = new Node();
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
newTop.item = item;
|
|
|
|
|
newTop.next = top;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
top = newTop;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
N++;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Item pop() throws Exception {
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
if (isEmpty()) {
|
|
|
|
|
throw new Exception("stack is empty");
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Item item = top.item;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
top = top.next;
|
|
|
|
|
N--;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
return item;
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return N == 0;
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public int size() {
|
|
|
|
|
return N;
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Iterator<Item> iterator() {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
return new Iterator<Item>() {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private Node cur = top;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return cur != null;
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Item next() {
|
|
|
|
|
Item item = cur.item;
|
|
|
|
|
cur = cur.next;
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
# 队列
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
下面是队列的链表实现,需要维护 first 和 last 节点指针,分别指向队首和队尾。
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
这里需要考虑 first 和 last 指针哪个作为链表的开头。因为出队列操作需要让队首元素的下一个元素成为队首,所以需要容易获取下一个元素,而链表的头部节点的 next 指针指向下一个元素,因此可以让 first 指针链表的开头。
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-09 23:42:50 +08:00
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public interface MyQueue<Item> extends Iterable<Item> {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
int size();
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
boolean isEmpty();
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
MyQueue<Item> add(Item item);
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Item remove() throws Exception;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public class ListQueue<Item> implements MyQueue<Item> {
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private Node first;
|
|
|
|
|
private Node last;
|
|
|
|
|
int N = 0;
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private class Node {
|
|
|
|
|
Item item;
|
|
|
|
|
Node next;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return N == 0;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public int size() {
|
|
|
|
|
return N;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public MyQueue<Item> add(Item item) {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Node newNode = new Node();
|
|
|
|
|
newNode.item = item;
|
|
|
|
|
newNode.next = null;
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
if (isEmpty()) {
|
|
|
|
|
last = newNode;
|
|
|
|
|
first = newNode;
|
|
|
|
|
} else {
|
|
|
|
|
last.next = newNode;
|
|
|
|
|
last = newNode;
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
N++;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Item remove() throws Exception {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
if (isEmpty()) {
|
|
|
|
|
throw new Exception("queue is empty");
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Node node = first;
|
|
|
|
|
first = first.next;
|
|
|
|
|
N--;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
if (isEmpty()) {
|
|
|
|
|
last = null;
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
return node.item;
|
|
|
|
|
}
|
2019-03-08 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Iterator<Item> iterator() {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
return new Iterator<Item>() {
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Node cur = first;
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return cur != null;
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
public Item next() {
|
|
|
|
|
Item item = cur.item;
|
|
|
|
|
cur = cur.next;
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-03-09 23:42:50 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-09 22:32:10 +08:00
|
|
|
|
<img width="580px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/other/公众号海报1.png"></img>
|