-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
42 lines (34 loc) · 822 Bytes
/
Stack.java
File metadata and controls
42 lines (34 loc) · 822 Bytes
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
package datastructure.lineartable;
import java.util.Iterator;
import java.util.LinkedList;
public class Stack<T> implements Iterable<T> {
private LinkedList<T> list = new LinkedList<T>();
public int size() {
return list.size();
}
public boolean isEmpty() { return list.size() == 0; }
/**
* 获取栈顶元素
*/
public T peek() {
if (isEmpty()) throw new RuntimeException("Stack empty");
return list.peekLast();
}
/**
* 入栈
*/
public void push(T data) {
list.add(data);
}
/**
* 出栈
*/
public T pop() {
if (isEmpty()) throw new RuntimeException("Stack empty");
return list.removeLast();
}
@Override
public Iterator<T> iterator() {
return list.iterator();
}
}