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



Subscribe to Tutorials

Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. Very nice article.... explained the concept very n
View Tutorial          By: prashant yeole at 2010-12-16 03:15:07

2. The example is awesome i found it most useful amon
View Tutorial          By: sabir pasha at 2009-01-20 05:11:34

3. very useful
View Tutorial          By: Ruchika at 2011-07-10 05:15:05

4. Ok, now I get it. New to Java and it just didnt ge
View Tutorial          By: izdelava strani at 2011-12-16 09:42:51

5. the reason for this is quite simple: java expands
View Tutorial          By: vineeth at 2011-08-25 13:54:37

6. Hello, I have stupid questions, but I need to ask
View Tutorial          By: Johnson Edison at 2012-12-13 17:10:57

7. i tried your code but i got an error on this line<
View Tutorial          By: edward at 2011-12-09 11:32:30

8. please send basic sample programs & explanatio
View Tutorial          By: rose at 2013-01-16 04:43:38

9. Why this exception at run time?

Fri

View Tutorial          By: Sebin Joseph at 2011-04-01 06:03:17

10. if the simple example with syntex than very easy t
View Tutorial          By: Rikunj suthar at 2011-08-31 06:57:17