-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMyLinkedStack.java
More file actions
67 lines (55 loc) · 1.3 KB
/
Copy pathMyLinkedStack.java
File metadata and controls
67 lines (55 loc) · 1.3 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ch03;
import java.util.EmptyStackException;
/**
* Created by cookfront on 2017/3/2.
*/
public class MyLinkedStack<AnyType> {
/**
* 栈节点类
* @param <AnyType>
*/
private static class Node<AnyType> {
public AnyType data;
public Node<AnyType> next;
public Node(AnyType val, Node<AnyType> nextNode) {
data = val;
next = nextNode;
}
public boolean end() {
return data == null && next == null;
}
}
private Node<AnyType> topOfStack;
private int theSize;
public MyLinkedStack() {
doClear();
}
private void doClear() {
topOfStack = new Node<AnyType>(null, null);
theSize = 0;
}
public void clear() {
doClear();
}
public int size() {
return theSize;
}
public boolean isEmpty() {
return theSize == 0;
}
public void push(AnyType val) {
theSize++;
topOfStack = new Node<AnyType>(val, topOfStack);
}
public AnyType pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
AnyType result = topOfStack.data;
if (!topOfStack.end()) {
topOfStack = topOfStack.next;
}
theSize--;
return result;
}
}