按字母顺序对链接列表进行排序

乔西亚·弗朗西斯(Josiah Francis):

我需要做一个作业,我必须做一个链接列表,其中包含一个人的名字和在大院中输入的车牌号。但我需要使用车牌号(例如: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
dquijada:

使用Collection.sort(),这应该很容易。

首先,您需要创建自己的Comparator,因为默认密码不起作用(您希望通过特定字段(车牌)进行订购)。

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

现在您有了Comparator,就可以使用它,这是我使用的一个示例(创建了一个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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在C中按字母顺序对链接列表进行排序

将链接列表按字母顺序排序

按字母顺序对多个列表进行排序

按项目对列表进行排序,按字母顺序排序

如何在C中按字母顺序对链接列表进行排序

按出现顺序和字母顺序对列表进行排序

按字母顺序对列表对象值进行排序

如何按字母顺序对字符串列表进行排序?

如何使用jQuery按字母顺序对列表进行排序?

按字母顺序对列表中的文本范围进行排序

按字母顺序对 v-for 列表进行排序

使用compareTo()方法按字母顺序对列表进行排序

如何按字母顺序对列表视图进行排序

如何使用 QSortFilterProxyModel 按字母顺序对列表项进行排序?

如何按字母顺序对列表视图进行排序?

按字母顺序对数组列表进行排序

在 kotlin 中按字母顺序对列表进行排序

按字母顺序对列表中的项目进行排序

Flutter:考虑“重音字符”,按字母顺序对列表进行排序

按数据属性(按字母顺序,然后按数字顺序)对列表项进行排序

首先按字母顺序对列表进行排序,然后按数字对列表进行排序?

Python尝试对列表进行数字排序后按字母顺序对列表进行排序

如何按原始列表的索引按字母顺序对Python中的列表进行排序

按字母顺序对JComboBox元素进行排序

PHP:按字母顺序对 ZIP 进行排序

按字母顺序对XML进行排序

按字母顺序对NSArray进行排序

按字母顺序对ArrayList <String []>进行排序

按字母顺序对结构数组进行排序