This is what the program needs to do. My code is below, I\'m not really sure wha
ID: 3624841 • Letter: T
Question
This is what the program needs to do. My code is below, I'm not really sure what I dont' have correct. Any help appreciated. Thanks!
1. Implement a Priority Queue for integers
1. Internal variables
1. data: An int array that is used to hold the data in the queue
2. head: An int which represents the next place in the data array that an item can be placed
3. tail: An int which represents the next item to pop off the array
2. Methods
1. isFull: Returns a boolean (If tail == head + 1)
2. isEmpty: Returns a boolean (If tail == head)
3. enqueue: Takes an int, and adds it to the queue
4. dequeue: Returns an int, and removes it from the queue
2. Implement a main method inside your queue class that does the following
3. Instantiates an instance of the queue with a size of 8
4. Prompt the user for an integer
1. If the integer is negative, print each element in the queue, and terminate
2. If the integer is zero, dequeue 2 elements from the queue, and print them.
3. If the integer is greater than zero, enqueue the int.
public class Queue
{
private int head,tail;
private int data[];
public Queue(int size)
{
data = new int[size+1];
head = 0;
tail = 0;
}
public boolean ifFull()
{
if(tail == head+1)
return true;
return false;
}
public boolean isEmpty()
{
if(tail == head)
return true;
return false;
}
public void enqueue(int value)
{
data[head] = value;
head++;
if(head == data.length)
head = 0;
System.out.println(head);
}
public int dequeue()
{
int result = data[tail-1];
tail++;
if(tail == data.length-1)
tail = 0;
System.out.println(tail);
return result;
}
}//end queue
import java.util.Scanner;
public class Lab10
{
public static void main(String[]args)
{
Queue queue = new Queue(8);
Scanner ins = new Scanner(System.in);
boolean flag = false;
int number;
while(!flag)
{
System.out.println("Please input an integer: (Negative prints each element in the queue and terminate | zero, print the last two ints in queue | >0 enqueue the int");
number = ins.nextInt();
if (number == 0)
while(queue.isEmpty());
{
System.out.println(queue.dequeue());
flag = true;
}
if (number == 0)
{
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
}
else
{
queue.enqueue(number);
}
}
}//end main
}//end Lab10
Explanation / Answer
Looks like you're missing a { after the 2nd to last "if (number == 0)"
number = ins.nextInt();
if (number == 0)
{ //This one
while(queue.isEmpty());
{
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.