gpt4 book ai didi

java - 循环队列可能尚未初始化

转载 作者:行者123 更新时间:2023-11-30 07:43:07 28 4
gpt4 key购买 nike

    import linearstructures.*;
import non_lineardatastructures.*;
import workers.*;
import binarynodes.*;
import linearnodes.*;
import dataobjects.*;
import java.util.*;
public class Main{
public static Scanner input= new Scanner(System.in);

public static BinarySearchTree tree;
public static String surname;
public static String surname2;
public static int seqNum;
public static double pay;
public static Employee name;
Node front;
Node rear;

//public static String surname3;

public static void main(String args[])
{
int menu;

do{
System.out.println("\f\f");
System.out.println("1. Construct an Empty Queue and Binary Tree");
System.out.println("2. Populate Queue with Employees and Part-Timers");
System.out.println("3. List all objects in Queue");
System.out.println("4. Edit an Object in Queue");
System.out.println("5. Populate BST with Queue objects");
System.out.println("6. Search BST by surname");
System.out.println("7. List BST In-Order");
System.out.println("8. Exit menu");
System.out.println("Enter your choice: ");

menu=input.nextInt();
switch(menu){
case 1:CQueue queue = new CQueue(); BinarySearchTree tree = new BinarySearchTree();System.out.println("A new Queue and BST have been constructed!");break;
case 2:System.out.println("Do you want to create an Employee or a Part-Timer?");
System.out.println("Enter e or p");
String choice=input.next();
if(choice.equals("e")){
queue=null;
Employee name=new Employee();
System.out.println("Enter surname:");
surname=input.next();
name.setSurname(surname);

System.out.println("Enter seqNum:");
seqNum=input.nextInt();
name.setSeq(seqNum);
System.out.println("Enter pay:");
pay=input.nextDouble();
name.setPay(pay);
queue.put(name);
System.out.println("A new Employee has been created!");
}
else if(choice.equals("p")){
queue=null;
PartTimer pt=new PartTimer();
System.out.println("Enter surname:");
surname2=input.next();
pt.setSurname(surname);

System.out.println("Enter seqNum:");
int seqNum=input.nextInt();
pt.setSeq(seqNum);
System.out.println("Enter pay:");
double pay=input.nextDouble();
pt.setPay(pay);
System.out.println("Enter hours worked:");
int hours=input.nextInt();
pt.setHours(hours);
queue.put(pt);
System.out.println("A new Part-Timer has been created!");
}
else System.out.println("Enter a valid choice!");;break;
case 3:queue=null;queue.listAll();break;
case 4:System.out.println("Enter surname:");
String surname3=input.next();
System.out.println(surname3);

if(queue!=null){
System.out.print("not empty!");
if((queue.searchKey(surname3))!=null){
queue.editObject(surname2);
queue.listAll();
}
}else System.out.print("empty");break;
case 5: tree.populateFromQueue(queue);break;
case 6:System.out.println("Enter surname:");
surname3=input.next();
tree.search(surname3);break;
case 7: tree.inorderBST();break;
case 8:break;
default: System.out.print("Enter a valid choice!");
}
System.out.println("Press any key to continue..");
String cont=input.next();
}while(menu!=8);
}




}
package linearstructures;
import dataobjects.*;
import linearnodes.*;
import java.util.*;
import workers.*;

package linearstructures;
import dataobjects.*;
import linearnodes.*;
import java.util.*;
import workers.*;

public class CQueue
{
public static Node front, rear,temp;
public static int maxNodes; // maximum number of nodes allowed
public static int currNodes;// current number of nodes in the queue
public static boolean full = false;
String key;

Scanner input= new Scanner(System.in);
// Constructor. Limits size of th queue to the number of nodes.
public CQueue()
{
front = null;
rear = null;
maxNodes = 20;
currNodes = 0;
}

// Appends the given newObj to the queue if able. Returns true if successful, otherwise false
public static boolean put(AnyClass newObj)
{


if (currNodes < maxNodes)
{
Node newNode = new Node(newObj);
/* if(full){
return false;
}else{
rear=rear.next;
rear.obj=newObj;

if(rear.next==front){
full=true;
}
currNodes++;
return true;
} */

if (rear == null)
{
front = newNode;
rear = newNode;
}
else
{

rear.next=newNode;
rear = rear.next;
}

currNodes++;

return true;
}
else
return false;


}

// Returns reference to the front object of AnyClass or null if queue is empty. First node is removed from the Queue.
public AnyClass serve()
{
if(front==null){
return null;
}
else{
AnyClass obj=front.obj;
Node firstNode=front;
front=front.next;
firstNode=null;
return obj;
}
}

// Part 3
public static void listAll()
{
if(rear!=null){
temp=front;
do{
for (int i= 0; i<currNodes; i++)
{
System.out.println(temp.obj.getData());
temp = temp.next;
}

}while(!(temp==rear.next));
} else System.out.println("Empty queue!");
}
public AnyClass searchKey(String key)
{
temp=front; // first node
//CQueue queue = new CQueue();
do
{
if ((temp.obj.getKey()).equals(key))
return temp.obj;
temp = temp.next;
} while (temp!=rear.next);
return null;
}

public AnyClass editObject(String key){
int choice, newSeqNo;
double newPay;
boolean exit = false;
Employee etemp = new Employee();

AnyClass searchResult = searchKey(key);

if(searchKey(key)!= null){
temp.obj.getData();
System.out.println();
System.out.println("------------------------");
System.out.print("Enter new Salary: ");
newPay = input.nextDouble();
System.out.println("------------------------");

if(searchResult != null ){
// Check if searchResult is an Employee object
if( searchResult instanceof Employee )
// Cast AnyClass to Employee
etemp = (Employee) searchResult;

// Your logic here...

// Set properties of the Employee object
etemp.setPay(newPay);
System.out.print("edited:"+etemp.getData());

// ... System.out.print("edited:"+etemp.getData);

return etemp;
}

}else{
System.out.println("NO OBJECT WAS FOUND!");
return null;
} return etemp;
}

}

我创建了一个循环队列。但是,当我尝试对队列进行操作时,它返回 null,当我列出队列中的所有项目时,列表是正确的,但是当我尝试编辑功能时,它显示队列为空。我不知道这怎么可能。

最佳答案

您正在 switch 语句中声明队列和树。在外部声明它们:

CQueue queue = new CQueue(); 
BinarySearchTree tree = new BinarySearchTree();
switch(menu){
// here you can re-create them if the user chooses to do so
case 1:
queue = new CQueue();
tree = new BinarySearchTree();
System.out.println("A new Queue and BST have been constructed!");
break;
....

关于java - 循环队列可能尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34380069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com