Question
Revise the code and provide menu options such as enqueue, dequeue, display queue, and many others. package queueconsole; import java.util.Arrays; public class QueueConsole { private
Revise the code and provide menu options such as enqueue, dequeue, display queue, and many others.
package queueconsole;
import java.util.Arrays;
public class QueueConsole {
private String[] queueArray;
private int queueSize;
private int front, rear,numberOfItems = 0;
QueueConsole(int size){
queueSize = size;
queueArray = new String[size];
Arrays.fill(queueArray,"-1");
}
public void insert(String input){
if(numberOfItems <= queueSize)
{
queueArray[rear] = input;
rear++;
numberOfItems++;
System.out.println("PUSH " + input + " was inserted into the queue.");
}
else{
System.out.println("Sorry but the queue is full.");
}
}
//insert elements using priority options (priority queues)
//optional whether use simple insert or priority insert function
public void priorityInsert(String input) {
int i;
if(numberOfItems == 0) {
insert(input);
}
else {
for(i=numberOfItems-1; i>=0; i--){
if(Integer.parseInt(input) > Integer.parseInt(queueArray[i])){
queueArray[i+1] = queueArray[i];
} else break;
}
queueArray[i+1] = input;
rear++;
numberOfItems++;
}
}
public void remove(){
if(numberOfItems > 0) {
System.out.println("REMOVE " + queueArray[front] + " was removed from the queue.");
queueArray[front] = "-1";
front++;
numberOfItems--;
} else
{
System.out.println("Sorry but the queue is empty.");
}
}
public void peek(){
System.out.println("The first element is " +queueArray[front]);
}
public void displayTheQueue(){
for(int i=0; i<45;i++) { System.out.print("=");} System.out.print(" ");
for(int i = 0; i < 10; i++){
System.out.print("| " + queueArray[i]);
}
System.out.print("| ");
for(int i=0; i<45;i++) { System.out.print("=");} System.out.print(" ");
}
public static void main(String[] args) {
QueueConsole thequeue = new QueueConsole(10);
//priority insert - priority queues
//thequeue.priorityInsert("10");
//thequeue.priorityInsert("19");
//thequeue.priorityInsert("15");
//thequeue.priorityInsert("11");
thequeue.insert("1");
thequeue.insert("10");
thequeue.insert("20");
thequeue.insert("30");
thequeue.insert("40");
thequeue.priorityInsert("500");
thequeue.priorityInsert("1000");
thequeue.priorityInsert("1500");
thequeue.priorityInsert("2500");
thequeue.displayTheQueue();
thequeue.peek();
thequeue.remove();
thequeue.displayTheQueue();
thequeue.peek();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started