-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
45 lines (38 loc) · 933 Bytes
/
Stack.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.NoSuchElementException;
import java.util.ArrayList;
public class Stack<T> implements StackADT<T> {
private ArrayList<T> data;
public Stack() {
data = new ArrayList<T>();
}
public void push(T item) {
data.add(item);
}
public T pop() throws NoSuchElementException {
T ret = data.get(data.size()-1);
data.remove(data.size()-1);
return ret;
}
public T top() throws NoSuchElementException {
return data.get(data.size()-1);
}
public Stack<T> clone() {
Stack<T> ret = new Stack<T>();
for(T i:data) {
ret.push(i);
}
return ret;
}
public int size() {
return data.size();
}
public boolean isEmpty() {
if(data.size() > 0) {
return false;
}
return true;
}
public void clear() {
data = new ArrayList<T>();
}
}