简体   繁体   English

使用方法对Java中的单链接列表进行排序

[英]Using a method to sort a Singly Linked List in Java

Implementing a Linked List, store up to 10 names, ordered in first in First Out. 实施链接列表,最多可存储10个名称,先进先出顺序排列。 Then implement two methods, one of which to sort it alphabetically by last names. 然后实现两种方法,一种方法是按姓氏的字母顺序对其进行排序。 This is where I am having some trouble. 这是我遇到麻烦的地方。 Here is what I tried: 这是我尝试过的:

  1. Recursion. 递归。 The method calls two nodes, compare them, swap if needed and then calls itself. 该方法调用两个节点,比较它们,如果需要交换,然后调用自身。 Doesn't work with odd number of names and tends to be full bugs. 不能使用奇数个名称,并且往往是完整的错误。

  2. Collection but I don't know enough about it to use it effectively. 收藏,但我对它的了解不足以有效地使用它。

  3. Sorting algorithms (ex. bubble sort): I can go though the list but have a hard time getting the nodes to swap. 排序算法(例如冒泡排序):我可以浏览列表,但是很难交换节点。

My question is: What is the easiest way to do this? 我的问题是:最简单的方法是什么?

public class List
{
    public class Link
    {
        public String firstName;
        public String middleName;
        public String lastName;
        public Link next = null;

    Link(String f, String m, String l)
    {
        firstName = f;
        middleName = m; 
        lastName = l;
    }
}

private Link first_;
private Link last_;

List()
{
    first_ = null;
    last_ = null;
}

public boolean isEmpty()
{
    return first_ == null;
}

public void insertFront(String f, String m, String l)
{
    Link name = new Link(f, m, l);
    if (first_ == null)
    {
        first_ = name;
        last_ = name;
    }
    else
    {
        last_.next = name;
        last_ = last_.next;
    }
}

public String removeFront()
{
    String f = first_.firstName;
    String m = first_.middleName;
    String l = first_.lastName;
    first_ = first_.next;
    return f + " " + m + " " + l;
}

public String findMiddle(String f, String l)
{
    Link current = first_;
    while (current != null && current.firstName.compareTo(f) != 0 && current.lastName.compareTo(l) != 0)
    {
        current = current.next;
    }
    if (current == null)
    {
        return "Not in list";
    }
    return "That person's middle name is " + current.middleName;
}
}

public class NamesOfFriends
{
    public static void main(String[] args)
    {
        List listOfnames = new List();
        Scanner in = new Scanner(System.in);

    for(int i = 0; i < 3; i++)
    {
        if(i == 0)
        {
            System.out.println("Please enter the first, middle and last name?");
            listOfnames.insertFront(in.next(), in.next(),in.next());
        }
        else
        {
            System.out.println("Please enter the next first, middle and last name");
            listOfnames.insertFront(in.next(), in.next(),in.next());
        }
    }

    System.out.println("To find the middle name, please enter the first and last name of the person.");
    System.out.println(listOfnames.findMiddle(in.next(),in.next()));
    }
}

Edit 编辑

After working on it a bit, I figured out how to go about sorting it. 经过一番努力之后,我想出了如何对其进行排序。 For that purpose, I am trying to implement a remove method that can remove a node anywhere on the list. 为此,我试图实现一个remove方法,该方法可以删除列表上任何位置的节点。 While it does compile, it doesn't do anything when I run the program. 虽然可以编译,但是在运行程序时它什么也没做。

public Link remove(String lastName)
{
    Link current_ = first_;
    Link prior_ = null;
    Link temp_ = null;
    while (current_ != null && current_.lastName.compareTo(lastName) != 0)
    {
        prior_ = current_;
        current_ = current_.next;
    }
    if (current_ != null)
    {
        if (current_ == last_)
        {
            temp_ = last_;
            last_ = prior_;
        }
        else if (prior_ == null)
        {
            temp_ = first_;
            first_ = first_.next;
        }
    }
    else
    {
        temp_ = current_;
        prior_.next = current_.next;
    }
    return temp_;
}

2: Collections is the easiest, but it seems to be not allowed in your homework 2:最简单的是收藏,但是在您的家庭作业中似乎是不允许的

3: BubbleSort is easy but the worst known sorting algo, however for your homework it probably is ok 3:BubbleSort很简单,但是排序算法最差,但是对于您的作业来说可能还可以

1: This is the same as bubble sort, but is prefered to be done without recursion 1:这与冒泡排序相同,但最好不进行递归

In BubbleSort you loop through your elements again and again till no swaps are neeeded anymore, then you are ready. 在BubbleSort中,您一次又一次地循环遍历您的元素,直到不再需要交换时,您才准备就绪。

Collection is the easiest way to accomplish this. 收集是完成此操作的最简单方法。

  • Implement Comparable 实施Comparable
  • Override hashcode and equals 覆盖hashcodeequals
  • Collection.sort()

You already has the linked list implemented, that is good. 您已经实现了链表,这很好。

Have you considered implementing MergeSort as the sorting algorithm? 您是否考虑过将MergeSort作为排序算法? Being the divide&conquer algorithm, you will always end up with only two elements to form a list with. 作为分而治之的算法,您最终总是只用两个元素组成一个列表。

The merge part is going to be trickier, but also easy. 合并部分将比较棘手,但也很容易。 Basically you just create a new list and start filling it up with elements you get by comparing the first values of the two merging sets. 基本上,您只是创建一个新列表,并通过比较两个合并集的第一个值开始使用获得的元素来填充它。

So for instance if you have two sets to merge: 因此,例如,如果有两个要合并的集合:

[A]->[C]->[D]
[B]->[E]->[F]

the mergin process will go: 合并过程将进行:

[A]

[C]->[D]
[B]->[E]->[F]

[A]->[B]

[C]->[D]
[E]->[F]

[A]->[B]->[C]

[D]
[E]->[F]

[A]->[B]->[C]->[D]

[E]->[F]

[A]->[B]->[C]->[D]->[E]

[F]

[A]->[B]->[C]->[D]->[E]->[F]

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

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