Question: // Java program to implement a queue using an array public class QueueAsArray { private int front, rear, capacity; private T[] queue; public QueueAsArray(int capacity)
// Java program to implement a queue using an array
public class QueueAsArray
private int front, rear, capacity;
private T[] queue;
public QueueAsArray(int capacity) {
front = rear = -1;
this.capacity = capacity;
queue = (T[]) new Object[capacity];
}
public boolean isEmpty(){
return front == -1;
}
public boolean isFull(){
return rear == capacity - 1;
}
// function to insert an element at the rear of the queue
public void enqueue(T data) {
if (isFull())
throw new UnsupportedOperationException("Queue is full!");
if(isEmpty())
front++;
rear++;
queue[rear] = data;
}
public T dequeue() {
if (isEmpty())
throw new UnsupportedOperationException("Queue is empty!");
T temp = queue[front];
if (rear == 0) {
rear = front = -1;
}
else{
for(int i = 0; i
queue[i] = queue[i + 1];
}
rear--;
}
return temp;
}
public boolean search(T e){
if (isEmpty())
throw new UnsupportedOperationException("Queue is empty!");
for(int i = 0; i
if(e.equals(queue[i]))
return true;
return false;
}
public String toString() {
if (isEmpty())
throw new UnsupportedOperationException("Queue is empty!");
String str = "";
for (int i = 0; i
str = str + queue[i] + " ";
}
return str;
}
public T peek() {
if (isEmpty())
throw new UnsupportedOperationException("Queue is empty!");
return queue[front];
}
}
Given the above code do the following:

1. (a) Convert the given QueueAsArray.java into a min-priority queue in which elements are always inser and removed in non-decreasing (i.e., ascending) sorted order. Your program must not use any sorting. (b) Write a test program to test the modified method(s) of QueueAsArray.java For example if the elements are inserted in a priority-queue of size 8 in the following order: 30,60,40,70,50,35,10,80 Then the program output is: First element to be dequeued: 10 Second element to be dequeued: 30 The modified min priority-queue is: 354050607080 Hint: Do the following changes to QueueAsArray.java: - Change the queue initialization to: queue =(T[]) new Comparable [ capacity ]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
