使用链表和数组来实现栈和队列是一个很基本的问题。在Algorithm的课上老师提到了这一点,因此我花了一点时间重新实现了一遍。老师在课上没有提到用ResizingArray来实现队列的方法,“Not difficult, but
a definitely tricky programming exercise that people are welcome to try”,因此我也尝试了一下。
Linked List — stack
老师提到早起的java并没有使用泛型,因此如果想使用一个String的栈和Integer的栈,那就得写两次代码,显然很不方便了。在这里我使用了泛型,同时实现了Iterable接口以便client side能遍历.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42class CustomStack<Item> implements Iterable<Item>{
private StackNode first;
private class StackNode{
private Item item;
private StackNode next;
}
public CustomStack() {
first = null;
}
public void push(Item item) {
StackNode n = new StackNode();
n.item = item;
n.next = first;
first = n;
}
public Item pop() {
Item res = first.item;
first = first.next;
return res;
}
public boolean isEmpty() {
return first==null;
}
public Iterator<Item> iterator() {
return new StackIterator();
}
private class StackIterator implements Iterator<Item> {
private StackNode cur = first;
public boolean hasNext() {
return cur != null;
}
public Item next() {
Item item = cur.item;
cur = cur.next;
return item;
}
}
}
测试代码:1
2
3
4
5
6
7
8
9
10
11public static void main(String[] args) {
// TODO Auto-generated method stub
CustomStack<Integer> test = new CustomStack<>();
test.push(2);
test.push(4);
test.push(6);
test.push(9);
for(int a : test) {
System.out.println(a);
}
}
遍历后的结果:1
2
3
49
6
4
2
ResizingArray
Java不允许使用泛型数组,如果要在此使用泛型,那就得涉及到强制转换,而老师又提到这是ugly的事情,因此我就以String为例写了一下。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31class ResizingArrayStackOfStrings{
private String[] array;
private int p;
public ResizingArrayStackOfStrings() {
array = new String[1];
p=0;
}
public void push(String item) {
//当前数组满了,就将数组大小扩大一倍
if(p==array.length -1) resize(array.length * 2);
array[p++] = item;
}
public String pop() {
String res = array[--p];
array[p] = null;
/*
当栈中元素是栈大小的1/4,则将栈的大小减小一半。
这里是1/4而不是1/2的原因是防止thrashing,也就是在1/2处用 户频繁进行push和pop操作
*/
if(p > 0 && p==array.length/4) resize(array.length/2);
return res;
}
public boolean isEmpty() {
return p==0;
}
private void resize(int capacity) {
String[] newArray = new String[capacity];
for(int i=0;i<array.length;i++) newArray[i] = array[i];
array = newArray;
}
}
LinkedList — queue
这里需要两个指针,指向一头一尾。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46class CustomQueue<Item> implements Iterable<Item>{
private StackNode begin;
private StackNode end;
private class StackNode{
private Item item;
private StackNode next;
}
public CustomQueue() {
begin = null;
end = null;
}
public void enqueue(Item item) {
StackNode n = new StackNode();
n.item = item;
if(begin==null) begin = n; //对于空队列处理
else end.next = n;
end = n;
}
public Item dequeue() {
Item item = begin.item;
begin = begin.next;
if(begin == null) end = null; //对于空队列处理
return item;
}
public boolean isEmpty() {
return begin==null;
}
public Iterator<Item> iterator() {
return new StackIterator();
}
private class StackIterator implements Iterator<Item> {
private StackNode cur = begin;
public boolean hasNext() {
return cur != null;
}
public Item next() {
Item item = cur.item;
cur = cur.next;
return item;
}
}
}
ResizingArray — queue
1 | class ResizingArrayQueueOfStrings{ |