Question: Have 2 classes in the program. Please help with how to fix the code so the class need to be generic: Class #1 public class
Have 2 classes in the program. Please help with how to fix the code so the class need to be generic:
Class #1
public class QueueNode {
int data;
QueueNode next;
public QueueNode(int data) {
this.data = data;
}
void Next(QueueNode next) {
this.next = next;
}
}
Class #2
import java.util.Random;
public class Queue
static QueueNode head;
static QueueNode tail;
public static void main(String args[]) {
Random value = new Random();
int upperBounded = 27;
head = new QueueNode(value.nextInt(upperBounded));
tail = head;
tail.Next(null);
int t = 0;
while (t < 1000) {
double m = Math.random();
int int_value = (int)(m * 100) % 2;
if (int_value == 0) {
int random_value = value.nextInt(upperBounded);
Enqueue(random_value);
System.out.println("Enqueue(" + random_value + ")");
}
else {
if (!empty()) {
System.out.println("Dequeue() = " + Dequeue());
}
else {
System.out.println("Dequeue - failed (queue empty).");
}
}
t ++;
}
}
public static void Enqueue(int a) {
QueueNode node = new QueueNode(a);
node.Next(null);
if (head == null) {
head = node;
tail = node;
}
else if (tail != null) {
tail.next = node;
tail = node;
}
}
public static int Dequeue() {
int a = head.data;
head = head.next;
return a;
}
static boolean empty() {
if (head == null) {
return true;
}
else {
return false;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
