-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathStackTest.java
72 lines (50 loc) · 1.17 KB
/
StackTest.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.List;
import java.util.ArrayList;
public class StackTest {
public static void main(String[] args) {
IStack<Integer> intStack = new Stack<>();
intStack.push(4);
intStack.push(5);
intStack.push(9);
System.out.println(intStack.pop());
System.out.println(intStack.size());
System.out.println(intStack.top());
}
}
interface IStack<T> {
/*
* 'pop' removed the last element from the stack and returns it
*/
T pop();
/*
* 'push' adds an element to at the end of the stack and returns the new size
*/
int push(T element);
/*
* 'size' returns the length of the stack
*/
int size();
/*
* 'top' returns the first element of the stack
*/
T top();
}
class Stack<T> implements IStack<T> {
private List<T> list;
public Stack() {
this.list = new ArrayList<>();
}
public T pop() {
return this.list.remove(this.size() - 1);
}
public int push(T element) {
this.list.add(element);
return this.size();
}
public int size() {
return this.list.size();
}
public T top() {
return this.list.get(this.size() - 1);
}
}