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

Code: public class HomeworkW13Driver { public static void main(String[] args) {

ID: 3714823 • Letter: C

Question

Code:

public class HomeworkW13Driver {

public static void main(String[] args) {

// Q5 splice client

QueueInterface<Integer> q1 = new LinkedQueue<Integer>();

q1.enqueue(4);

q1.enqueue(8);

q1.enqueue(9);

q1.enqueue(2);

QueueInterface<Integer> q2 = new LinkedQueue<Integer>();

q2.enqueue(1);

q2.enqueue(3);

q2.enqueue(7);

q2.enqueue(6);

splice(q1, q2);

System.out.println("q1 contains: 4 8 9 2 1 3 7 6");

((LinkedQueue)q1).display();

System.out.println("q2 still contains: 1 3 7 6");

((LinkedQueue)q2).display();

// Q6 splice ArrayQueue

ArrayQueue<Integer> aq1 = new ArrayQueue<Integer>();

aq1.enqueue(4);

aq1.enqueue(8);

aq1.enqueue(9);

aq1.enqueue(2);

ArrayQueue<Integer> aq2 = new ArrayQueue<Integer>();

aq2.enqueue(1);

aq2.enqueue(3);

aq2.enqueue(7);

aq2.enqueue(6);

aq1.splice(aq2);

System.out.println("q1 contains: 4 8 9 2 1 3 7 6");

aq1.display();

System.out.println("q2 still contains: 1 3 7 6");

aq2.display();

// Q7 splice LinkedQueue

LinkedQueue<Integer> lq1 = new LinkedQueue<Integer>();

lq1.enqueue(4);

lq1.enqueue(8);

lq1.enqueue(9);

lq1.enqueue(2);

LinkedQueue<Integer> lq2 = new LinkedQueue<Integer>();

lq2.enqueue(1);

lq2.enqueue(3);

lq2.enqueue(7);

lq2.enqueue(6);

lq1.splice(lq2);

System.out.println("q1 contains: 4 8 9 2 1 3 7 6");

lq1.display();

System.out.println("q2 still contains: 1 3 7 6");

lq2.display();

}

public static void splice(QueueInterface firstQueue, QueueInterface secondQueue) {

}

}

Question: Show the contents of an initially empty priority queue after each of the following statements execute.

Assume that a, b, c, d, and e are objects and that the number next to the object specifies its priority.

For example, object 1a has priority 1 and object 2b has priority 2.

Lower numbers have higher priorities.

Example: 2b has a higher priority than 3d

Example: 2b has the same priority as 2e.

Your answer should have 13 lines that show the contents of pq1.

Put the front of the priority queue on the left and the back on the right.

pq1.add(1d)

pq1.add(2e)

pq1.add(1a)

pq1.add(2a)

pq1.add(1c)

pq1.add(1e);

pq1.remove()

pq1.add(pq1.remove())

pq1.add(pq1.getFront())

pq1.remove()   

pq1.remove()

pq1.remove();

pq1.remove();

Explanation / Answer

Solution:

hey heres the code:

class PriorityQueue
{

   int[] heap;
   char [] l;

    private int heapSize,capacity;

    /** Constructor **/

    public PriorityQueue(int capacity)

    {   

       
        this.capacity=capacity;
        heap = new int[this.capacity];
        l = new char[this.capacity];
        heapSize = -1;
       

    }

    /** function to clear **/

    public void clear()

    {

        heap = new int[capacity];
        l = new char[capacity];
        heapSize = -1;

    }

    /** function to check if empty **/

    public boolean isEmpty()

    {

        return heapSize == -1;

    }

    /** function to check if full **/

    public boolean isFull()

    {

        return heapSize == capacity;

    }

    /** function to get Size **/

    public int size()

    {

        return heapSize;

    }

    /** function to insert task **/

    public void add(String priority)

    {

   if(heapSize==-1)
{
heapSize++;
heap[heapSize]=(int)priority.charAt(0)-48;
l[heapSize]=priority.charAt(1);
}
else
{
heapSize++;
int n=heapSize;
heap[heapSize]=(int)priority.charAt(0)-48;
l[heapSize]=priority.charAt(1);
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(heap[i]<heap[j])
{
int temp=heap[i];
char t=l[i];
heap[i]=heap[j];
l[i]=l[j];
heap[j]=temp;
l[j]=t;
}
}
}
}
}

    /** function to remove task **/

    public String remove()

    {

      return Integer.toString(heap[heapSize])+l[heapSize--];
}
public String getFront()
{
return Integer.toString(heap[heapSize])+l[heapSize];
}
}

/** Class PriorityQueueTest **/

public class PriorityQueueTest

{

    public static void main(String[] args)

    {


        System.out.println("Priority Queue Test ");  


        PriorityQueue pq1 = new PriorityQueue(10 );

pq1.add("1d");

pq1.add("2e");

pq1.add("1a");

pq1.add("2a");

pq1.add("1c");

pq1.add("1e");

System.out.println(pq1.remove());

pq1.add(pq1.remove());
System.out.println(pq1.getFront());

pq1.add(pq1.getFront());

pq1.remove();  

pq1.remove();

pq1.remove();

pq1.remove();


    }

}