Skip to content

Commit

Permalink
basics of stack (push, peek, size, pop)
Browse files Browse the repository at this point in the history
  • Loading branch information
suhanigupta23 authored Feb 7, 2024
1 parent 5c46871 commit e76828c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions stack_basic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
}

0 comments on commit e76828c

Please sign in to comment.