简体   繁体   English

按升序打印自定义链接列表的元素

[英]Print elements of a custom linked list in ascending order

I'm relatively new to Java and trying to learn on my free-time before I start taking on bigger data structures. 在开始使用更大的数据结构之前,我对Java相对较新,并试图在空闲时间学习。 Tonight its all about the Linked List. 今晚全部关于链接列表。 I have a LinkedList and trying to put the list in ascending order. 我有一个LinkedList并试图按升序排列列表。 Say we have numbers 5, 4, 6, 9, 1. 假设我们有5号,4号,6号,9号,1号。

How would you write a method - to actually make it 1, 4, 5, 6, 9? 你会怎么写一个方法 - 实际上让它成为1,4,5,6,9?

Right now, I'm currently trying to do this and all I'm having is: 现在,我正在尝试这样做,而我所拥有的只是:

public void order (LinkedList head) {
    if (head == null)
      return;
    else {
      order(head.next);
      System.out.print(" " + head.data);
      return;

Instead of ascending order its printing out the reverse of the original linked list: 1, 9, 6, 4, 5. 而不是按升序排列,其打印出原始链表的反向:1,9,6,4,5。

What am I doing wrong here? 我在这做错了什么?

If LinkedList is a class defined by yourself, either you 如果LinkedList是您自己定义的类,那么您

1) Write your own sort method to perform sort or 1)编写自己的排序方法来执行排序或

2) Let your Node class from which forms up your LinkedList to implement Comparable . 2)让构成LinkedList Node类实现Comparable Create an ArrayList and add the nodes into it. 创建一个ArrayList并将节点添加到其中。 After that you will be able to sort it with Collections.sort(yourNewArrayList) . 之后,您将能够使用Collections.sort(yourNewArrayList)对其进行排序。

You will have something like: 你会有类似的东西:

public class Node implements Comparable<Node>{
    private int value;
    //constructor, getters and other members not shown

    @Override
    public int compareTo(Node n){
        return this.getValue() - n.getValue();
    }
}

ArrayList list<Node> = new ArrayList<Node>();
while(linkedList.hasNext()){
    list.add(linkedList.next());
}

Collections.sort(list);

It is also worth taking note that the sort implemented by Java uses Dual-Pivot Quicksort which gives you O(n(log n) performance. 值得注意的是,Java实现的排序使用了Dual-Pivot Quicksort,它可以提供O(n(log n)性能。

You have two choices here, depending on your goals: 根据您的目标,您有两种选择:

  1. For code that works, use java.util.LinkedList or java.util.ArrayList and java.util.Collections.sort() . 对于有效的代码,请使用java.util.LinkedListjava.util.ArrayListjava.util.Collections.sort() These are written by very good programmers and used all of the time by millions of programmers around the world. 这些是由非常优秀的程序员编写的,并且一直被全世界数百万程序员使用。 So you do not have to worry about bugs and can get to work on writing something useful. 因此,您不必担心错误,并且可以开始编写有用的东西。

  2. If you are interested in learning the nuts and bolts of data structures, it appears that you have already written your own LinkedList class. 如果您有兴趣学习数据结构的LinkedList ,那么您似乎已经编写了自己的LinkedList类。 Now you need to write your own sort() method. 现在你需要编写自己的sort()方法。 Sorting is a very well understood problem and there are many online resources available that explain different sorting algorithms. 排序是一个非常好理解的问题,并且有许多可用的在线资源可以解释不同的排序算法。 These are good learning tools because there are so many variations with different strengths and weaknesses. 这些都是很好的学习工具,因为有很多不同的优点和缺点。

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

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