Java 包含LinkedList的LinkedList节点
作为我正在做的一个项目的一部分,我使用自制的linkedList无需泛型,我需要设置一个linkedList myLL的节点以包含另一个linkedList myList的节点,同时按客户端对其进行排序,即客户端为其完成作业1、2和3,而myList包含这些作业,当myLL包含客户端时,您对如何在linkedList节点中包含linkedList有什么建议吗?您看起来像这样吗Java 包含LinkedList的LinkedList节点,java,linked-list,nodes,Java,Linked List,Nodes,作为我正在做的一个项目的一部分,我使用自制的linkedList无需泛型,我需要设置一个linkedList myLL的节点以包含另一个linkedList myList的节点,同时按客户端对其进行排序,即客户端为其完成作业1、2和3,而myList包含这些作业,当myLL包含客户端时,您对如何在linkedList节点中包含linkedList有什么建议吗?您看起来像这样吗 LinkedList<LinkedList> listOfList = new LinkedList<
LinkedList<LinkedList> listOfList = new LinkedList<LinkedList>();
LinkedList list1 = new LinkedList();
//Add elements to list1
LinkedList list2 = new LinkedList();
//Add elements to list2
LinkedList list3 = new LinkedList();
//Add elements to list3
listOfList.add(list1);
listOfList.add(list2);
listOfList.add(list3);
您可以用ArrayList替换LinkedList,您需要为节点创建两个类。一个包含客户端,另一个包含客户端的作业 例如:您将创建如下的客户端节点类:
public class Client{
int clientNumber;
String clientName;
Client clientLink;
Job jobLink;}
public class Job {
String jobName;
int jobNumber;
Job jobLink;}
作业节点类如下所示:
public class Client{
int clientNumber;
String clientName;
Client clientLink;
Job jobLink;}
public class Job {
String jobName;
int jobNumber;
Job jobLink;}
您将注意到,两个节点都有一个类型相同的类所有者变量,即将节点彼此链接
然后您将获得您的链接列表代码:
import java.util.Scanner;
public class LinkedList
{
Scanner sc = new Scanner(System.in);
Client List;
Client Location;
Client PredLocation;
void CreateList()
{
List = null;
System.out.println("The List Have Been Created/Destroyed Successfully!");
}
void DestroyList()
{
List = null;
}
boolean EmptyList()
{
if ( List == null ) return true;
else return false;
}
void PrintAllClients()
{
if (EmptyList() == true)
System.out.println("The List is Empty!");
else
{
Client P = List;
System.out.println("Client Number"+"\t"+"Client Name"+"\t");
while (P != null)
{
System.out.println(P.clientNumber+"\t\t"+P.clientName+"\t\t");
P = P.clientLink;
}
}
}
void Search(int key)
{
Location = List;
PredLocation = null;
while (Location != null)
{
if (Location.clientNumber == key)
break;
else if (Location.clientNumber > key)
Location = null;
else
{
PredLocation = Location;
Location = Location.clientLink;
}
}
}
void InsertClient()
{
if (List == null)
{
Client P = new Client();
P.clientNumber = 1;
System.out.println("Enter the Name of the First Client: ");
P.clientName = sc.nextLine();
List = P;
System.out.println("The Insertion of new Client was Successfull");
}
else
{
Client P = List;
while (P.clientLink != null)
P = P.clientLink;
P.clientLink = new Client();
P.clientLink.clientNumber = P.clientNumber + 1;
System.out.println("Enter the Client Name: ");
P.clientLink.clientName = sc.nextLine();
System.out.println("The Insertion of new Client was Successfull");
}
}
void RetrieveClient(int key)
{
Search(key);
if (Location == null)
System.out.println("Client is not Found!");
else
{
System.out.println("Client Number"+"\t"+"Customer Name"+"\t");
System.out.println(Location.clientNumber+"\t\t"+Location.clientName+"\t\t");
}
}
void ModifyClient(int key)
{
Search(key);
if (Location == null)
System.out.println("Client is not Found!");
else
{
System.out.println("");
System.out.println("Client Number"+"\t"+"Client Name"+"\t");
System.out.println(Location.clientNumber+"\t\t"+Location.clientName+"\t\t");
System.out.println("");
System.out.println("Enter the new Name: ");
Location.clientName = sc.nextLine();
System.out.println("The Modifying of the client was Successfull");
}
}
void DeleteClient(int key)
{
Search(key);
if (Location == null)
System.out.println("Client is not Found!");
else if (Location.jobLink == null)
{
if (PredLocation == null)
List = Location.clientLink;
else
PredLocation.clientLink = Location.clientLink;
System.out.println("The Deletion of the Client was Successfull");
}
else
System.out.println("Cannot Delete a Client who has Jobs!");
}
void AddJob(int key)
{
Search(key);
if (Location == null)
System.out.println("Client is not Found!");
else if (Location.jobLink == null)
{
System.out.println("Enter The First Job name:");
String A = sc.next();
System.out.println("Enter The First Job number:");
int B = sc.nextInt();
Job P = new Job();
P.jobName = A;
P.jobNumber = B;
Location.jobLink = P;
System.out.println("The job Addetion was Successfull");
}
else
{
System.out.println("Enter The Job Name");
String A = sc.next();
System.out.println("Enter The Job Number");
int B = sc.nextInt();
Job P = Location.jobLink;
while (P.jobLink != null)
P = P.jobLink;
P.jobLink = new Job();
P.jobLink.jobName = A;
P.jobLink.jobNumber = B;
System.out.println("The Job Addition was Successfull");
}
}
void PrintJob(int key)
{
Search(key);
if (Location == null)
System.out.println("Client is not Found!");
else if (Location.jobLink == null)
System.out.println("Client has no Jobs!");
else
{
Job P = Location.jobLink;
System.out.println("Client Number"+"\t"+"Client Name"+"\t");
System.out.println(Location.clientNumber+"\t\t"+Location.clientName+"\t\t");
System.out.println("");
System.out.println("Job Name"+"\t"+"Job Number");
while (P != null)
{
System.out.println(P.jobName+"\t\t"+P.jobNumber);
P = P.jobLink;
}
}
}
}
我希望这是有帮助的您能提供相关代码吗?LL只是相互链接的节点。因此,您可以将第一个LL的最后一个节点链接到第二个LL的第一个节点。