-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathStackUsingArrayList.java
More file actions
46 lines (40 loc) · 1.09 KB
/
StackUsingArrayList.java
File metadata and controls
46 lines (40 loc) · 1.09 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
import java.util.ArrayList;
public class Stack {
ArrayList<Integer> al = new ArrayList<>();
public boolean isEmpty() {
return al.size() == 0;
}
public void push(int item) {
al.add(item);
}
public int pop() {
if (isEmpty()) {
System.out.print("Overflow Error");
System.exit(0);
}
int top = al.remove(al.size() - 1);
return top;
}
public int peek() {
if (isEmpty()) {
System.out.print("Overflow Error");
System.exit(0);
}
return al.get(al.size() - 1);
}
public static void main(String[] args) {
Stack st = new Stack();
st.push(1);
st.push(2);
st.push(3);
System.out.println(st.peek());
System.out.println(st.peek());
System.out.println(st.peek());
System.out.println(st.peek());
System.out.println(st.peek());
// System.out.println(st.pop());
// System.out.println(st.pop());
// System.out.println(st.pop());
// System.out.println(st.pop());
}
}