-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingAL.java
More file actions
35 lines (35 loc) · 910 Bytes
/
StackUsingAL.java
File metadata and controls
35 lines (35 loc) · 910 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
import java.util.*;
public class StackUsingAL{
public static class Stack{
static ArrayList<Integer> s = new ArrayList<>();
public static boolean isEmpty(){
return s.size()==0;
}
public static void push(int data){
s.add(data);
}
public static int pop(){
if (isEmpty()){
return -1;
}
int top=s.get(s.size()-1);
s.remove(s.size()-1);
return top;
}
public static int peek(){
if (isEmpty()){
return -1;
}
return s.get(s.size()-1);
}
}
public static void main(String[] args) {
Stack.push(30);
Stack.push(20);
Stack.push(30);
while(!Stack.isEmpty()){
System.out.println("Top: " + Stack.peek());
Stack.pop();
}
}
}