-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleStack.java
45 lines (35 loc) · 866 Bytes
/
SimpleStack.java
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
import java.util.*;
public class SimpleStack {
private List<Integer> array;
public SimpleStack() {
this.array = new ArrayList<>();
}
public void push(int i) {
this.array.add(i);
}
public Integer pop() {
int lastIndex = size() - 1;
if (lastIndex >= 0) {
int i = this.array.get(lastIndex);
this.array.remove(lastIndex);
return i;
} else {
return null;
}
}
public Integer peek () {
int lastIndex = size() - 1;
if (lastIndex >= 0) {
int i = this.array.get(lastIndex);
return i;
} else {
return null;
}
}
public int size() {
return this.array.size();
}
public String toString() {
return this.array.toString();
}
}