Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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):

image text in transcribed

(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions