Tuesday, November 16, 2010

stack program using java array implement in java

import java.io.*;
class stack
    {
        public static void main()throws IOException
            {
                BufferedReader br=new BufferedReader(
                    new InputStreamReader(System.in));
                int ar[]=new int[5];
                int top=-1,ch;
                do
                    {
                        System.out.println("1.Push element");
                        System.out.println("2.Pop element");
                        System.out.println("3.Display element");
                        System.out.println("4.Exit");
                        System.out.print("Enter your choice :-");
                        ch=Integer.parseInt(br.readLine());
                        switch(ch)
                            {
                                case 1:
                                    if(top<5)
                                        {
                                            System.out.print("Enter element :-");
                                            ar[++top]=Integer.parseInt(br.readLine());            
                                        }
                                    else
                                        System.out.println("Stack overflow...");
                                    break;
                                case 2:
                                    if(top == -1)
                                        System.out.println("Stack underflow....");
                                    else
                                        System.out.println("Poped element :-"+ar[top--]);
                                    break;
                                case 3:
                                    if(top == -1)
                                        System.out.println("Stack underflow....");
                                    else
                                        {
                                            for(int i=0;i<=top;i++)
                                                {
                                                    System.out.print(ar[i]+"\t");
                                                }
                                            System.out.println();
                                        }
                                    break;
                                case 4:
                                    System.out.println("Program end....");
                                    break;
                                default:
                                    System.out.println("Invalid choice enter....");
                            }
                    }while(ch != 4);
            }
    }

3 comments:

Unknown said...

“Everyone isn’t logical. Everything doesn’t make sense in the end. Sometimes you have to forget about explanations or excuses and leave people and places behind, because otherwise they will drag you straight down.” See the link below for more info. #breakable


#breakable
www.matreyastudios.com

Unknown said...

Awesome article. Please do more articles like this in the future. Very informational and knowledgeable. I will expect more from you in the future. For now i will just bookmark your page and surely I'm gonna come back later to read more. Thank you to the writer!


www.imarksweb.org

Unknown said...

Awesome article. Please do more articles like this in the future. Very informational and knowledgeable. I will expect more from you in the future. For now i will just bookmark your page and surely I'm gonna come back later to read more. Thank you to the writer!


www.imarksweb.org