简体   繁体   English

包含LinkedLists的LinkedList节点

[英]LinkedList nodes containing LinkedLists

作为项目的一部分,我正在使用自制的linkedLists(无泛型)的地方进行操作,我需要设置一个linkedList(myLL)的节点以包含另一个linkedList(myList)的节点,同时由客户端对它们进行排序(也就是说,一个客户为他完成了工作1、2和3,而myList包含了这些工作,而myLL包含了该客户)您是否对如何在LinkedList节点中包含linkedList有任何建议?

Are you looking something like this? 你看起来像这样吗?

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);

You can replace LinkedList with ArrayList 您可以将LinkedList替换为ArrayList

You would need to create two classes for the nodes. 您将需要为节点创建两个类。 One containing the client, the other containing the job-of-client. 一个包含客户端,另一个包含客户端作业。

for example: you will make a client node class like this: 例如:您将制作如下的客户端节点类:

public class Client{
int clientNumber;
String clientName;

Client clientLink;
Job jobLink;}

and the job node class like this: 和作业节点类如下:

public class Job {
String jobName;
int jobNumber;

Job jobLink;}

you will notice that both node have a variable of the same type of the class owner and that is to link the nodes with each other. 您会注意到,两个节点都具有与类所有者相同类型的变量,并且该变量将节点彼此链接。

and then you will have your linked list code: 然后您将拥有自己的链接列表代码:

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;
            }
        }
    }
}

I hope this was helpfull 我希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM