Search This Blog

Sunday 11 May 2014

Java Program to implement Static and Dynamic Stack using Interface and abstract class.

import java.io.*;
import java.util.*;

interface Iface
{
    static final int size=3;
}

abstract class abClass implements Iface
{
   
    static int top=-1,flag=0;
    abstract void push();
    abstract void pop();
    abstract void display();
}

class VectStack5 extends abClass
{
   
   
    static Vector vect=new Vector();
    static DataInputStream in=new DataInputStream(System.in);
    public static void main(String[] args) throws IOException
    {
        int ch1,ch2;
        VectStack5 ob=new VectStack5();
    label:
        for(;;)
        {
            System.out.println("1.Static Stack   2.Dynamic Stack  3.Exit");
            System.out.println("Enter your choice:");
            ch1=Integer.parseInt(in.readLine());
            if(ch1==1)
                flag=1;

            else if(ch1==2)
                flag=2;
            else
                System.exit(0);
            for(;;)
            {   
                if(flag==1)
                    System.out.println("Static Stack Operation");
                else
                    System.out.println("Dynamic Stack Operation");
                System.out.println("1.Push \n2.Pop \n3.Display \n4.Goto Main Menu. \n5.Exit");
                System.out.println("Enter your choice:");
                ch2=Integer.parseInt(in.readLine());
                switch(ch2)
                {
                    case 1:
                        if(flag==1)
                        {
                            if(top>=size-1)
                            {
                                System.out.println("Overflow");
                                break;
                            }
                        }
                        ob.push();
                        break;

                    case 2:    if(top==-1)
                            {
                                System.out.println("Stack is empty");
                            }
                            else
                            {
                                ob.pop();
                            }
                            break;
                    case 3: if(top==-1)
                                System.out.println("Stack is empty");
                            else
                                ob.display();
                            break;
                   
                    case 4: vect.removeAllElements();
                            top=-1;
                            continue label;
                   
                    default:System.exit(0);
                } //Close switch
            } //Close Inner for loop
        } //Close Outer for loop
    } //Close main function.
   
    void push()
    {
        try
        {
                System.out.println("Enter element to be inserted:");
                vect.addElement(Integer.parseInt(in.readLine()));
                top++;
        }
        catch (Exception e){}
    }

    void pop()
    {
        System.out.println("Deleted item="+vect.elementAt(top));
        vect.removeElementAt(top);
        top--;
    }

    void display()
    {
        System.out.println("Stack Contents:");
        for(int i=0;i<vect.size();i++)
            System.out.println(vect.elementAt(i));
    }

}//Close main class

No comments :

Post a Comment

FREE Hit Counters