簡體   English   中英

按字母順序對鏈接列表進行排序

[英]Sorting a Linked List in alphabetical order

我有一個作業,我必須做一個鏈接列表,其中包含一個人的名字和在一個大院中輸入的車牌號。 但是我需要使用車牌號(例如:ABS1234)按字母順序對列表進行排序。 我一直在進行排序方面的研究,例如合並排序或使用collection.sort,但無法解決。 如果我能對如何做到這一點有所幫助,那將是很棒的。 提前致謝。

public class Node {
//data fields
public String regPlate; // Registration Plate
public String firstName;
public String lastName;

//refrence link
public Node link;

//default constructor
public Node()
{
    regPlate = "";
    firstName = "";
    lastName = "";
}//end of constructor.

}//end of node class

這是我創建的Node類。

public class LinkedList {

Node head;
Node tail;
Node current;
int listLength;
Scanner input = new Scanner(System.in);

//default constructor
public LinkedList () 
{
    head = null;
    listLength = 0;
}

//inserting new node in the beginning of the list
public void insertFirst(String fN, String lN, String rP)
{

    Node newNode = new Node();
    newNode.firstName = fN;
    newNode.lastName = lN;
    newNode.regPlate = rP;

    //make newNode point to the first node in the life
    newNode.link = head;

    //makes head point to the new first node
    head = newNode;

    if(head == null)
        tail = newNode;

    ++listLength;
}//end of insertFirst


public void displayDataLog()
{
    Node current;

    current = head;

    while(current != null)
    {
        System.out.print("\n FullName: " + current.firstName + " " + current.lastName +
                        "\n Registration Plate Number: " + current.regPlate);

        current = current.link;
    }

}//end of display vehicles

public void totalVehicles()
{
    System.out.print("\n Total Vehicle on the campus: " + listLength);
}//end of total vehicles


}//end of linked list

使用Collection.sort(),這應該很容易。

首先,您需要創建自己的Comparator ,因為默認的Comparator不起作用(您希望通過特定字段(車牌)進行訂購)。

class NodeComparator implements Comparator<Node> {
    @Override
    public int compare(Node n1, Node n2) {
        return n1.regPlate.compareTo(n2.regPlate);
    }
}

現在您有了比較器,只需使用它,這是我使用的一個示例(創建了一個ArrayList並介紹了列表中的每個元素):

private static LinkedList sortList(LinkedList list) {
    // I use an ArrayList so I can just use Collections.sort
    LinkedList sortedList = new LinkedList();
    Node current = list.head;
    ArrayList<Node> array = new ArrayList<Node>();

    while (current != null) {
        array.add(current);
        current = current.link;
    }
    array.sort(new NodeComparator());

    for (int i = array.size()-1; i >= 0; i--) {
        sortedList.insertFirst(array.get(i));
    }

    return sortedList;
}

我做了一些更改,很可能很容易適應您自己的程序。 您可以在此處查看完整的工作程序: https : //code.sololearn.com/cgzN5sQOI8uW



為了完成起見(以及以防萬一鏈接停止工作),整個代碼:

import java.util.*; 
import java.lang.*; 
import java.io.*;

class Playground {
    public static void main(String[ ] args) {
        LinkedList list = new LinkedList();
        list.insertFirst("Robert", "Rodriguez", "RB123");
        list.insertFirst("Andrew", "Andrews", "AB123");
        list.insertFirst("Thomas", "Thomson", "TB123");

        System.out.println(list); // Prints unordered list, opposite order as they were introduced, since they were introduced from the beggining of the list.

        LinkedList sorted = sortList(list);
        System.out.println("\n"+sorted);
    }

    private static LinkedList sortList(LinkedList list) {
        // I use an ArrayList so I can just use Collections.sort
        LinkedList sortedList = new LinkedList();
        Node current = list.head;
        ArrayList<Node> array = new ArrayList<Node>();

        while (current != null) {
            array.add(current);
            current = current.link;
        }
        System.out.println("\nTemp Array:");
        System.out.println(array);

        array.sort(new NodeComparator());

        System.out.println("\nTemp Array (now sorted):");
        System.out.println(array);

        for (int i = array.size()-1; i >= 0; i--) {
            sortedList.insertFirst(array.get(i));
        }

        return sortedList;
    }
}

class NodeComparator implements Comparator<Node> {
    @Override
    public int compare(Node n1, Node n2) {
        return n1.regPlate.compareTo(n2.regPlate);
    }
}

class Node {
    //data fields
    public String regPlate; // Registration Plate
    public String firstName;
    public String lastName;

    //refrence link
    public Node link;

    //default constructor
    public Node()
    {
        regPlate = "";
        firstName = "";
        lastName = "";
    }//end of constructor.

    public String toString() {
        return this.regPlate;
    }



}//end of node class

class LinkedList {

    Node head;
    Node tail;
    Node current;
    int listLength;

    //default constructor
    public LinkedList () 
    {
        head = null;
        listLength = 0;
    }

    //inserting new node in the beginning of the list
    public void insertFirst(String fN, String lN, String rP)
    {

        Node newNode = new Node();
        newNode.firstName = fN;
        newNode.lastName = lN;
        newNode.regPlate = rP;

        insertFirst(newNode);
    }//end of insertFirst

    public void insertFirst(Node newNode) {
        //make newNode point to the first node in the life
        newNode.link = head;

        //makes head point to the new first node
        head = newNode;

        if(head.link == null)
            tail = newNode;

        ++listLength;
    }

    public void displayDataLog()
    {
        Node current;

        current = head;

        while(current != null)
        {
            System.out.print("\n FullName: " + current.firstName + " " + current.lastName +
                            "\n Registration Plate Number: " + current.regPlate);

            current = current.link;
        }

    }//end of display vehicles

    public void totalVehicles()
    {
        System.out.print("\n Total Vehicle on the campus: " + listLength);
    }//end of total vehicles

    public String toString() {
        String str = "List:\nhead -> [ ";
        Node current = head;

        while (current != null) {
            str = str + current + (current == tail ? " ] <- tail" : ", ");
            current = current.link;
        }

        return str;
    }

}//end of linked list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM