Thursday, 15 October 2015

Stack example in java

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<String> st = new Stack<String>();
st.add("India");
st.add("Nepal");
st.add("Sri Lanka");
st.add("Country");
for (String s : st){
System.out.println("the value of st is " +s);
}
boolean i = st.empty();
System.out.println("The value of i is " +i);
String s1 = st.peek();
System.out.println("the value of top most element is " +s1);
String s2 = st.pop();
System.out.println("the value of top most element is " +s2);
int j = st.search("Country");
System.out.println("the value of j is " +j);
String s3 = st.peek();
System.out.println("the value of top most element is " +s3);
}
}


output:-
the value of st is India
the value of st is Nepal
the value of st is Sri Lanka
the value of st is Country
The value of i is false
the value of top most element is Country
the value of top most element is Country
the value of j is -1
the value of top most element is Sri Lanka


No comments:

Post a Comment