Programming Tutorials

Comment on Tutorial - Stack example in Java - push(), pop(), empty(), search() By Henry



Comment Added by : srikanth

Comment Added at : 2014-05-30 06:44:58

Comment on Tutorial : Stack example in Java - push(), pop(), empty(), search() By Henry
import java.io.*;
import java.util.*;
class Stackdemo
{
public static void main(String []args)throws IOException
{
Stack<Integer> st=new Stack<>();
Buffer Reader br =new Buffer Reader(new InputStreamReader(System.in));
int element,pos,choice=0;
while(choice<4)
{
System.out.println("stack operation");
System.out.println("1 push element");
System.out.println("2 pop element");
System.out.println("3 search for element");
System.out.println("4 exit");
System.out.println("your choice");
choice=Integer.parseInt(br.readline());
switch(choice)
{
case1:
System.out.print("enter element:");
element =Integer.parseInt(br.readline());
st.push(element);
break;
case2:
Integer i=st.pop();
System.out.print("poped="+i);
break;
case3:
System.out.print("enter element");
element=Integer.parseInt(br.readline());
pos=st.search(element);
if(pos==-1)
System.out.println("element not found");
else
System.out.println("element found position:"+pos);
break;
default;
return;
}
System.out.println("Stack="+st);
}
}
}




}


View Tutorial