Menu

Sunday 4 October 2015

Java Concept (Queue Data structure)

package queuedatastructure;

/**
 *
 * @author Adeniran Adetunji
 */
public class ArrayStaticQueue {
    //Declaring the instance variables of a queue. Initalizing head and tail to zero
    int headRef=0;
    int tailRef=0;
    char [] arrayQueue;
     
    //Method to create queue array
   void initializeQueue(int Size)
    {
     
       arrayQueue = new char[Size];
     
       for( int i=0; i<arrayQueue.length; i++)
       {
           arrayQueue[i] = '\u0000';
       }
    }
 
   //This method checks if queue is empty
  boolean isEmpty()
  {
      boolean test= true;
      for (int i=0; i<arrayQueue.length; i++)
      {
          if(arrayQueue[i]!='\u0000')
           
              test=false;
      }
   
      return test;
  }

  //This method test if queue is full
  boolean isFull()
  {
      boolean test = true;
   
      for (int i=0; i<arrayQueue.length; i++)
      {
          if(arrayQueue[i]=='\u0000')
           
              test = false;
      }
   
      return test;
  }

  //This method adds item to queue
 void addItem( char c)
 {
     // The test determines if the queue is full
     boolean test=isFull();
     // If the queue is full
     if(test)
     {
     //The system should print QUEUE IS FULL
         System.out.println("Queue is Full");
     }
     else
     {
        arrayQueue[tailRef]=c;
     }
   
     setTail();
 }
   
//This method set new tail location after adding item to queue
  void setTail()
  {
//Test if queue is full
 boolean  test=isFull();
     //if is full
     if(test)
     {
         //Then tail index becomes -1
         tailRef=-1;
     }
     //If queue is not full
     else
     {
         //Get the current tail index in nTail variable
     int nTail = tailRef;
   
     //Check for free space to the right-side of the tail
         for(int i=tailRef; tailRef<arrayQueue.length; i++)
         {
             //if found...
             if(arrayQueue[i]=='\u0000')
             {
                 //set new tail at the index of the free space
                 tailRef=i;
                 //break out of the loop
                 break;
             }
         }
         //Test if tailRef is not updated by the loop above
         //If test is true: that is loop is not updated
      if (nTail==tailRef)
      {
          //start searching for the free space from the begining of the
          //queue i.e. from index 0
          for (int k=0; k<arrayQueue.length; k++)
          {
             //Get first free space
             if(arrayQueue[k]=='\u0000')
             {
                 //Update tailRef at the index of the first free space gotten
                 tailRef=k;
                 //break out of the loop
                 break;
             }
          }
      }
     }
  }
  //This method removes item from queue
 void removeItem()
 {
     boolean test = isEmpty();
   
     if(test)
     {
         System.out.print("Queue is empty");
     }
     else
     {
       
         arrayQueue[headRef]='\u0000';
       
         if(tailRef==-1)
         {
             tailRef=headRef;
         }
       
     }
   
     setHead();  
 }

 //This method set new head location after removing item from queue
 void setHead()
 {
    boolean test = isEmpty();
         if(test)
         {
           
         headRef=0;
       
         }
         else
         {
             if(headRef+1<arrayQueue.length)
             {
               
              headRef=headRef+1;
             }
             else
             {
                 for(int i=0; i<arrayQueue.length; i++)
                 {
                     if(arrayQueue[i]!='\u0000')
                       
                         headRef=i;
                     break;
                 }
             }
         }
 }
   
 }

....Class Instance...
package queuedatastructure;

/**
 *
 * @author Adeniran Adetunji
 */
public class QueueDataStructure {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     
        ArrayStaticQueue myQueue = new ArrayStaticQueue();
     
        myQueue.initializeQueue(5); //Initializes queue that can hold five characters
     
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }    

/*2*/    boolean check = myQueue.isEmpty();

            System.out.println(check);

/*3*/ System.out.println("Head =" + myQueue.headRef);
         System.out.println("Tail =" + myQueue.tailRef);

/*4*/  myQueue.addItem('A');
/*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*2*/ check = myQueue.isEmpty();

       System.out.println(check);
     
 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);
  /*5*/ myQueue.addItem('B');
       myQueue.addItem('C');
       myQueue.addItem('D');
       myQueue.addItem('E');

 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*2*/ check = myQueue.isEmpty();
     
       System.out.println(check);
     
 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);

              check=myQueue.isFull();
 /*6*/     System.out.println(check);

 /*7*/   myQueue.addItem('F');
/*8*/  myQueue.removeItem();
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);
     
/*9*/  myQueue.removeItem();
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);

 /*10*/ myQueue.removeItem();
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);

 /*11*/ myQueue.addItem('F');
 /*12*/ myQueue.removeItem();
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);



     
     
}
}


Output
run:
1.





2. true      
3. Head =0
   Tail =0

4.A




1. false
2. Head =0
3. Tail =1
//After adding item at /*5*/
1.A
   B
   C
   D
   E
2.false
3.Head =0
4.Tail =-1
6.True
7.Queue is Full
//After /*8*/
1.
   B
   C
   D
   E
3.Head =1
4.Tail =0
//After  /*9*/

C
D
E
Head =2
Tail =0
//After /*10*/
1.


   D
   E
3.Head =3
4.Tail =0
//After  /*11*/ & /*12*/
F


E
Head =4
Tail =1
BUILD SUCCESSFUL (total time: 9 seconds)