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

In java, create a driver class that: creates instances of an ArrayQ and a Linked

ID: 3595147 • Letter: I

Question

In java, create a driver class that:

creates instances of an ArrayQ and a LinkedQ,

enquees the following ints onto the queues: {5,7,8,12,9,4}

dequeues each element and displays as it is removed

public class ArrayQ {

       private final int DEFAULT_CAPACITY = 10;
       private int front, rear, count;
       private int[] queue;

     
       public ArrayQ()
       {
          front = rear = count = 0;
          queue = new int[DEFAULT_CAPACITY];
       }

       // Adds the specified element to the rear of the queue.
       public void enqueue (int element) {
          if (count == queue.length)
              expandCapacity();

           queue[rear] = element;
           rear = (rear+1) % queue.length;
           count++;     
       }

       //-----------------------------------------------------------------
       // Creates a new array to store the contents of this queue with
       // twice the capacity of the old one.
       //-----------------------------------------------------------------
       public void expandCapacity()
       {
          int[] larger = new int[queue.length*2];

          for (int index=0; index < count; index++)
             larger[index] = queue[(front+index) % queue.length];

          front = 0;
          rear = count;
          queue = larger;
       }
     
     
       // Removes and returns the element at the front of the queue.
       public int dequeue() {
           if (count==0) return -1;
           int x = queue[front];
           front = (front+1) % queue.length;
           count--;
           return x;
       }

       // Returns a reference to the element at the front of the queue
       // without removing it.
       public int first() {
           return queue[front];
       }

       // Returns true if the queue contains no elements and false
       // otherwise.
       public boolean isEmpty() {
           return count==0;
       }

       // Returns the number of elements in the queue.
       public int size() {
           return count;
       }

       // Returns a string representation of the queue.
       public String toString() {
           return "";
       }

}

// This class implements a queue using a linked list
// It requires the LinkedNode class to be defined
public class LinkedQueue {

   LinkedNode front;
   LinkedNode rear;
   int count;
  
   LinkedQueue() {
       front = rear = null;
       count = 0;
   }
  
   void enqueue(int x) {
       LinkedNode newNode = new LinkedNode(x);
       if (rear!=null)          
           rear.next = newNode;
       else
           front = newNode;
       rear = newNode;
       count++;
   }
  
   int dequeue() {
       if (front==null) {
           return -1;
       }
      
       if (front!=rear) {
           int x = front.x;
           front = front.next;      
           count--;
           return x;
       } else {
           int x = front.x;
           front = null;
           rear = null;
           count --;
           return x;
       }
   }
  
   int first() {
       return front.x;
   }
  
   boolean isEmpty() {
       return count==0;
   }
  
   int size() {
       return count;
   }
}

Explanation / Answer

//In java, create a driver class that:
//creates instances of an ArrayQ and a LinkedQ,
//enquees the following ints onto the queues: {5,7,8,12,9,4}
//dequeues each element and displays as it is removed

public class Main
{
public static void main (String[] args){
ArrayQ l = new ArrayQ(); //create a instacne of ArrayQ

// enqueue {5,7,8,12,9,4}
l.enqueue(5);
l.enqueue(7);
l.enqueue(8);
l.enqueue(12);
l.enqueue(9);
l.enqueue(4);
System.out.println(l.toString());
// dequeues {5,7,8,12,9,4}
System.out.println(l.dequeue());
System.out.println(l.dequeue());
System.out.println(l.dequeue());
System.out.println(l.dequeue());
System.out.println(l.dequeue());
System.out.println(l.dequeue());


LinkedQ q = new LinkedQ(); //create a instacne of LinkedQ

// enqueue {5,7,8,12,9,4}
q.enqueue(5);
q.enqueue(7);
q.enqueue(8);
q.enqueue(12);
q.enqueue(9);
q.enqueue(4);
System.out.println(l.toString());
// dequeues {5,7,8,12,9,4}
System.out.println(q.dequeue());
System.out.println(q.dequeue());
System.out.println(q.dequeue());
System.out.println(q.dequeue());
System.out.println(q.dequeue());
System.out.println(q.dequeue());

}}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote