Question: Solve using Java programming language. // Java program to implement a queue using an array public class QueueAsArray { private int front, rear, capacity; private

Solve using Java programming language.

// 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];

}

}

For the code above solve the following (please show the answer clearly):

Solve using Java programming language. // Java program to implement a queue

(a) Comment the iterative public T dequeue() method of the given class QueueAsArray T then implement it as a recursive method. Use an appropriate helper method in your solution. (b) Write a test program to test the recursive dequeue method. Sample program run: The queue is: 6020403070 First dequeued element is: 60 Second dequeued element is: 20 After two node deletion the queue is: 403070 Element at queue front is: 40

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!