queue in arrays

import java.util.*;
class queue_in_arrays
{
    
    public static void main(String[] args)
    {
      queue_in_arrays object = new queue_in_arrays();
      Scanner vinnu = new Scanner(System.in);

        while(true)
        {
            System.out.println("Please enter the optin \n 1.enqueue \n 2.dequeue \n 3.top \n 4.isempty  \n 5.isfull \n 6.Show queue \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();     
                        object.enqueue(value);
                        break;
              case 2 : object.dequeue();
                        break;
              case 3 : object.top();
                       break;
              case 4 : object.isempty();
                       break;
              case 5 : object.isfull();
                        break;
              case 6 : object.fullqueue();
                        break;
              case 7  : System.exit(1);

            }
             System.out.println("--------------------------------------------------");
        }        
    }


int capacity=3;
int top=-1;
int rear=0;
int array[]=new int[capacity];

        public void enqueue(int value)
        {
           if(top<capacity-1)
           {
             top++;
             array[top]=value;
             System.out.println(value+" is enqueued into the queue .");

           }
           else
           {
              System.out.println(" overflow ..");
           }
        }
        
        public void dequeue()
        {
          if(rear<=top)
          {
            System.out.println(array[rear]+" is dequeued into the queue .");
           rear++;
            
          }
          else
          {
              System.out.println(" underflow ..");
          }
        }
        
        public void top()
        {
             
            if(top>=rear)
            {
                System.out.println(array[top]+" is dequeued into the queue .");
            }
            
        }
        
        public void isempty()
        {
           if(rear>top)
           {
               System.out.println(" empty ..");
           }
           else
           {
              System.out.println(" not empty ..");
           }
        }
        
        public void isfull()
        {
           if(top==capacity-1)
           {
               System.out.println(" full ..");
           }
           else
           {
              System.out.println(" not full ..");
           }
        }
        
        
         public void fullqueue()
        {
           if(rear<=top)
           {
             for(int i=rear;i<=top;i++)
               System.out.println(array[i]+"");
           }
           else
           {
              System.out.println(" not full ..");
           }
        }
        

}
queue in arrays queue in arrays Reviewed by Unknown on 10:26 Rating: 5

No comments:

Powered by Blogger.