Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For the array implementation of the queue, the data members are: two indexes cal

ID: 3557342 • Letter: F

Question

For the array implementation of the queue, the data members are: two indexes called front and rear, an array called info and an integer called length. For the linked implementation of the queue the data members are two pointers: one called front, one called rear. For the array implementation of the stack, the data members are: an index called top and an array called info. For the linked implementation of the stack the only data member is a pointer called top. a. Write a method, to be included in a queue class - circular array implementation - that will search for an item passed as a parameter and will return true, if found, talse otherwise.

Explanation / Answer

public boolean search(T item)
{
   int count = 0;      
   for(int i = front; count < length; i = (i + 1) % info.length)
   {
       if(((Comparable<T>)info[i]).compareTo(item) == 0)
           return true;          
      
       count++;
   }
      
   return false;
}

-----------------------------------------

Sample Output:

The elements in the queue are: 25 15 35 30 10
35 is found in the queue.(T/F): true
45 is found in the queue.(T/F): false

-----------------------------------------------------------------------------

Complete Program:

public class CircularArrayQueue<T>
{
   private final int DEFAULT_CAPACITY = 5;
   private int front, rear, length;
   private T[] info;

   public CircularArrayQueue()
   {
       front = rear = length = 0;
       info = (T[]) (new Object[DEFAULT_CAPACITY]);
   }

   public CircularArrayQueue(int initialCapacity)
   {
       front = rear = length = 0;
       info = ((T[]) (new Object[initialCapacity]));
   }

   public void enqueue(T element)
   {
       if(size() == info.length)
           expandCapacity();

       info[rear] = element;
       rear = (rear + 1) % info.length;

       length++;
   }

   public void expandCapacity()
   {
       T[] larger = (T[]) (new Object[info.length * 2]);

       for(int scan = 0; scan < length; scan++)
       {
           larger[scan] = info[front];
           front = (front + 1) % info.length;
       }

       front = 0;
       rear = length;
       info = larger;
   }

   public T dequeue()
   {
       if(isEmpty())
       {
           System.out.println("Queue is empty!");
           return null;
       }

       T result = info[front];
       info[front] = null;
       front = (front + 1) % info.length;
       length--;

       return result;
   }
  
   public boolean search(T item)
   {
       int count = 0;
      
       for(int i = front; count < length; i = (i + 1) % info.length)
       {
           if(((Comparable<T>)info[i]).compareTo(item) == 0)
               return true;          
      
           count++;
       }
      
       return false;
   }

   public int size()
   {
       return length;
   }

   public T first()
   {
       if(isEmpty())
       {
           System.out.println("Queue is empty!");
           return null;
       }

       T result = info[front];
       return result;
   }

   public boolean isEmpty()
   {
       return (length == 0);
   }

   public String toString()
   {
       String queueElements = "";
       int count = 0;
      
       for(int i = front; count < length; i = (i + 1) % info.length)
       {
           queueElements = queueElements + info[i] + " ";
      
           count++;
       }

       return queueElements;
   }

   public static void main(String[] args)
   {
       CircularArrayQueue<Integer> caq = new CircularArrayQueue<Integer>();

       caq.enqueue(25);
       caq.enqueue(15);
       caq.enqueue(35);
       caq.enqueue(30);
       caq.enqueue(10);
      
       System.out.println("The elements in the queue are: " + caq);      
       System.out.println("35 is found in the queue.(T/F): " + caq.search(35));
       System.out.println("45 is found in the queue.(T/F): " + caq.search(45));
   }
}