我正在尝试修复我的程序以使其运行得更好并且我遇到了一些代码问题。在空队列中应用时,选项3和4遇到运行时异常错误,我该如何检查?对于非空队列,选项4显示“队列为空,抛出异常”,然后是正确答案(“队列中的最后一项是:”)。选项5(大小)在空队列中返回1。应为0.实际上,它返回的大小超过1。另外,如何重复菜单,每次操作后输入'c'都很烦人。贝娄是我的java代码:
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;
public class QueueMenu {
public static void main(String[] args) throws IOException {
int b;
int c;
String cont = "c";
DataInputStream in = new DataInputStream(System.in);
QueueLL newq = new QueueLL();
while(cont.equals("c"))
{
System.out.println("I Insert an item to the queue (at rear)");
System.out.println("II Delete an item from the queue (from front)");
System.out.println("III Display the item at front");
System.out.println("IV Display the item at rear");
System.out.println("V Display total number of items currently present in the queue");
System.out.println("VI Print the items currently present in the queue");
System.out.println("VII Quit");
b = Integer.parseInt(in.readLine());
switch(b)
{
case 1:
System.out.print("Enter an integer number: ");
c = Integer.parseInt(in.readLine());
newq.insert(c);
break;
case 2:
c = newq.remove();
System.out.print("Enter an integer number: " + c);
break;
case 3:
c = newq.displayQF();
System.out.print("First item in Queue is: \n" + c);
break;
case 4:
c = newq.displayQR();
if(c != 0)
System.out.print("Last item in Queue is: \n" + c);
else
System.out.print("The Queue is empty");
break;
case 5:
c = newq.count();
System.out.println("Total number of elements currently present in the queue are: \n" + c);
break;
case 6:
System.out.println("Elements currently present in the queue are: \n" );
newq.displayQ();
break;
case 7:
System.exit(0);
System.out.println("Quit\n");
break;
default:
System.out.println("What you have entered is not accepted by the system.Please retry");
break;
}
System.out.println(" ");
System.out.println("Enter c to continue or q to end program");
cont = in.readLine();
}
}
}
public class QueueLL {
int count = 1;
SinglyLinkedListQueue sllq = new SinglyLinkedListQueue();
public int count(){
return count;
}
public boolean isQEmp()
{
return sllq.isEmpty();
}
public void insert(int val){
sllq.insertF(val);
count++;
}
public int remove() {
count--;
return sllq.removeL();
}
public int displayQF() {
return sllq.displayFQ();
}
public int displayQR(){
return sllq.displayRQ();
}
public void displayQ() {
sllq.displayLinkedListQ();
}
class Node{
public int qdata;
public Node next;
public Node(int n){
qdata = n;
}
public void displayN()
{
System.out.print(qdata + "");
}
}
public class SinglyLinkedListQueue{
Node front;
Node rear;
public SinglyLinkedListQueue(){
front = null;
rear = null;
}
public void insertF(int a){
Node newNode = new Node(a);
newNode.next=null;
if(isEmpty())
front = newNode;
else
rear.next = newNode;
rear = newNode;
}
public int removeL(){
int b = front.qdata;
if(front.next == null)
rear = null;
front = front.next;
return b;
}
public int displayFQ() {
if(isEmpty()== true)
System.out.print("The Queue is empty, excpetion thrown");
return front.qdata;
}
public int displayRQ() {
Node current = front;
while(current != rear && isEmpty() != true){
current = current.next;
}
System.out.println("Queue is empty, exception thrown\n");
return current.qdata;
}
public void displayLinkedListQ() {
Node current = front;
while(current != null){
System.out.print(current.qdata + " ");
current = current.next;
}
System.out.println();
}
public boolean isEmpty()
{
return (front == null);
}
}