-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_basic.java
34 lines (34 loc) · 1.17 KB
/
stack_basic.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
import java.util.*;
public class Program
{
public static void main(String[] args)
{
Stack<Integer> st=new Stack<>();
st.push(10);
System.out.println(st+" "+st.peek()+" "+st.size());
st.push(20);
System.out.println(st+" "+st.peek()+" "+st.size());
st.push(30);
System.out.println(st+" "+st.peek()+" "+st.size());
st.push(40);
System.out.println(st+" "+st.peek()+" "+st.size());
st.push(50);
System.out.println(st+" "+st.peek()+" "+st.size());
st.pop();
System.out.println(st+" "+st.peek()+" "+st.size());
st.pop();
System.out.println(st+" "+st.peek()+" "+st.size());
st.pop();
System.out.println(st+" "+st.peek()+" "+st.size());
st.pop();
System.out.println(st+" "+st.peek()+" "+st.size());
st.pop();
System.out.println(st+" "+st.peek()+" "+st.size());
st.pop();
//till here all elements are gone
//so when we again print st.peek it will not print it will give run time error
//so we do not write st.peek function
System.out.println(st+" "+st.size());
//size comes as zero
}
}