two_stacks_in_a_array

import java.util.*;
class two_stacks_in_a_array
{
  public static void main(String[] args)
  {
    Scanner vinnu = new Scanner(System.in);
    while(true)
    {
        System.out.println("Please enter the optin \n 1.push1 \n 2.push2 \n 3.pop1 \n 4.pop2  \n 5.isfull \n 7.exit");
         System.out.println("--------------------------------------------------");
        int option=vinnu.nextInt();     
        switch(option)
        {
          case 1 : System.out.print("enter the elememt to push = ");
                   int value=vinnu.nextInt();     
                    push1(value);
                    break;
          case 2 : System.out.print("enter the elememt to push = ");
                    value=vinnu.nextInt();     
                    push2(value);
                    break;
          case 3 : pop1();
                   break;
          case 4 : pop2();
                   break;
          case 5 : isfull();
                    break;
          case 7  : System.exit(1);
                      
        }
         System.out.println("--------------------------------------------------");
  }
  }
    private static final int capacity=6;
    static int stackarray[]=new int[capacity];
    static int top1=-1;
     static int top2=capacity;
      
      /// push1 method
    public static void push1(int value)
    {
       if(top1<top2-1)
       {
          top1++;
          stackarray[top1]=value;   
           System.out.println(value+" is pushed into the stack .");
       }
        else
        {
            System.out.println("Stack is overflowed .");
        }                
    }
    
      
      //////push2 method
       public static void push2(int value)
    {
       if(top1<top2-1)
       {
          top2--;
          stackarray[top2]=value;   
           System.out.println(value+" is pushed into the stack .");
       }
        else
        {
            System.out.println("Stack is overflowed .");
        }                
    }
      
      
   //////////////////pop1   
      
    public static void pop1()
    {
      if(top1>=0)
      {
          System.out.println(stackarray[top1]+" is popped  out from the stack .");
          top1--;
      }
        else
        {
            System.out.println("Stack  underflow .");
        }
    }

      //////////////////pop2        
        public static void pop2()
    {
      if(top2<capacity)
      {
          System.out.println(stackarray[top2]+" is popped  out from the stack .");
          top2++;
      }
        else
        {
            System.out.println("Stack  underflow .");
        }
    }
        

    public static void isempty()
    {
      if(top1==-1 && top2==capacity)
      {
          System.out.println("Stack is empty .");
      }
        else
        {
            System.out.println("Stack is not empty .");
        }
    }
    
    public static void isfull()
    {
         if(top1==top2-1)
      {
          System.out.println("Stack is full .");
      }
        else
        {
            System.out.println("Stack is not full .");
        }
    }
   
    
}
two_stacks_in_a_array two_stacks_in_a_array Reviewed by Unknown on 04:02 Rating: 5

No comments:

Powered by Blogger.