[英]LinkedList nodes containing LinkedLists
作為項目的一部分,我正在使用自制的linkedLists(無泛型)的地方進行操作,我需要設置一個linkedList(myLL)的節點以包含另一個linkedList(myList)的節點,同時由客戶端對它們進行排序(也就是說,一個客戶為他完成了工作1、2和3,而myList包含了這些工作,而myLL包含了該客戶)您是否對如何在LinkedList節點中包含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);
您可以將LinkedList替換為ArrayList
您將需要為節點創建兩個類。 一個包含客戶端,另一個包含客戶端作業。
例如:您將制作如下的客戶端節點類:
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;
}
}
}
}
我希望這會有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.