Showing posts with label Stack program using java. Show all posts
Showing posts with label Stack program using java. Show all posts

Tuesday, August 21, 2012

Stack Operations Using Java Programming


import java.util.Iterator;
import java.util.Stack;

public class StackOperations {

public static void main(String args[]){
Stack st=new Stack();

//Push the elments into stack
st.push(12);
st.push(9);
st.push(15);
st.push(76);
st.push(55);

//Elements in stack
System.out.println("Stack"+st);

//Pop the Elements
System.out.println("Top of the Stack:"+st.peek());
System.out.println("Pop the element from stack "+st.pop());

//Display elements using iterator
Iterator it=st.iterator();
while(it.hasNext())
System.out.println(it.next());

//Check stack is Empty or not
System.out.println("Stack is empty ?"+st.isEmpty());

}
}

Output:
Stack[12, 9, 15, 76, 55]
Top of the Stack:55
Pop the element from stack 55
12
9
15
76
Stack is empty ?false