-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingArrayList.java
More file actions
44 lines (36 loc) · 945 Bytes
/
StackUsingArrayList.java
File metadata and controls
44 lines (36 loc) · 945 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
43
import java.util.ArrayList;
public class StackUsingArrayList {
static class Stack {
ArrayList<Integer> list = new ArrayList<>();
public void push(int data) {
list.add(data);
}
public boolean isEmpty() {
return list.size() == 0;
}
public int pop() {
if (isEmpty()) {
return -1;
}
int top = list.remove(list.size() - 1);
return top;
}
public int peek() {
if (isEmpty()) {
return -1;
}
return list.get(list.size() - 1);
}
}
public static void main(String args[]) {
Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
while (!stack.isEmpty()) {
System.out.println(stack.peek());
stack.pop();
}
}
}