-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackWithArrayList.java
More file actions
62 lines (49 loc) · 1.23 KB
/
Copy pathStackWithArrayList.java
File metadata and controls
62 lines (49 loc) · 1.23 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
import java.util.ArrayList;
public class StackWithArrayList {
static class Stack {
ArrayList<Integer> list = new ArrayList<>();
public boolean isEmpty() {
return list.size() == 0;
}
public void push(int data) {
list.add(data);
}
public int pop() {
if (isEmpty()) {
return -1;
}
int data = list.get(list.size() - 1);
list.remove(list.size() - 1);
return data;
}
public int peek() {
if (isEmpty()) {
return -1;
}
return list.get(list.size() - 1);
}
}
public static void main(String[] args) {
Stack s = new Stack();
s.push(4);
s.push(9);
s.push(12);
s.push(54);
s.push(45);
while (!s.isEmpty()) {
System.out.print(s.peek() + " ");
s.pop();
}
System.out.println();
Stack s2 = new Stack();
s2.push(3);
s2.push(5);
s2.push(21);
s2.push(98);
s2.push(43);
while (!s2.isEmpty()) {
System.out.print(s2.peek() + " ");
s2.pop();
}
}
}