包含链接列表的链接列表节点

时间:2014-06-05 08:42:47

标签: java linked-list nodes

作为项目的一部分,我正在使用自制的链接列表(没有泛型)我需要设置一个linkedList(myLL)的节点以包含另一个linkedList(myList)的节点在客户端对它们进行排序时(即客户端为他完成了作业1,2和3,myList包含这些作业,而myLL包含客户端)您是否有关于如何在LinkedList节点内包含linkedList的建议?

2 个答案:

答案 0 :(得分:0)

你看起来像这样吗?

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

答案 1 :(得分:0)

您需要为节点创建两个类。一个包含客户端,另一个包含客户端作业。

例如:您将创建一个像这样的客户端节点类:

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

我希望这是有帮助的