最简单的方法来排序此链接列表

时间:2018-04-24 15:28:38

标签: java linked-list singly-linked-list

我有一个我想要排序的节点列表。

节点:

df2  <- data.frame(paragraph = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.",
              filename = "./data/RevCon_2015_C1_Austria_05_06.txt", conference = "RevCon", year = "2015", country= "Austria", date = "06.05.2015", stringsAsFactors = FALSE)

列表:

package lab.pkg14;

class Node {

    int data;
    Node next;

    Node(int data) {
        this.data = data;
    }

    public void display() {
        System.out.println(this.data);
    }

    @Override
    public String toString() {
        String out = "";
        return out + data;
    }
}

主:

class LinkedList {

    private Node head;     //First item in LL
    private int length;    //Number of items.
    boolean isEmpty;

    public LinkedList() {
        this.length = 0;
        isEmpty = true;
    }

    public int getLength() {        //Getter, NO setter
        return length;
    }

    public void add(int item) {  //Adds node at the front.
        Node myNode = new Node(item);
        myNode.next = this.head;
        this.head = myNode;
        this.length++;
        isEmpty = false;

    }

    public int peek() {             //Returns value at head of list. Doesn't alter the list.
        return head.data;
    }

    public boolean find(int item) {        //Looks through the list for the int.
        boolean foundit = false;           //You'll want to use .equals() if its generic. //code
        return foundit;
    }

    public void displayList() {
        Node runner = head;
        while (runner != null) {
            runner.display();
            runner = runner.next;

        }
    }

    public int addList() {
        Node runner = head;
        int sum = 0;
        while (runner != null) {
            sum += runner.data;
            runner = runner.next;
        }
        return sum;
    }

    public Node remove() {  //Removes the head.
        Node headData = head;
        if (!isEmpty) {
            this.head = head.next;
        }
        return headData;
    }

    public boolean remove(int item) {      //Removes first instance of the specified item.
        boolean foundData = false;
        Node current = head;
        Node previous = head;
        if (!isEmpty) {
            while (current.data != item) {
                if (current.next == null) {
                    foundData = false;
                } else {
                    previous = current;
                    current = current.next;

                }

            }
            if (current == head) {
                head = head.next;
                foundData = true;
            } else {
                previous.next = current.next;
                foundData = true;
            }

        }
        return foundData;
    } //Returns true if it found it and removed it.
    public void sort() {   //Obvious, right?             

    }


    @Override
    public String toString() {             //Makes debugging easier.
        String out = "";
        Node current = head;
        for (int i = 1; i <= this.length; i++) {
            out = out + current.data + " ";
            current = current.next;
        }

        return out;
    }

}

我正在努力学习如何自己编码,但似乎无法得到这个。我一直试图将这几个数字排序超过3天。我尝试了其他的例子和所有这些,但我无法深究它。对这些数字进行排序的最简单方法是什么?

1 个答案:

答案 0 :(得分:0)

LinkedList(您的实现或https://docs.oracle.com/javase/9/docs/api/java/util/LinkedList.html)会按照添加项目的顺序保留项目。在Java Collections LinkedList中,新添加的项目放在列表的末尾,而您的实现将它们放在开头 - 但无论是哪种方式都是它们的添加顺序,而不是与每个项目的'value'相关的一些顺序用于维持秩序的项目。

如果要根据值对列表中的项进行排序(在您的情况下我假设为int值),您可以:

  1. 添加新项目时保持所需的顺序,即不只是创建一个新节点并将其作为头/尾 - 迭代遍历列表中的任何现有节点,将int的值与每个节点进行比较,直到您找到正确的位置以保持所需的顺序,并将新节点放在那里。随着列表的增长,将自动维护正确的订单。
  2. 实现您已经存根的排序方法,以便对条目进行重新排序(即在LinkedList中移动它们),或者返回LinkedList的有序副本,保持现有的LinkedList按原样排序。这将是一项更大的任务,您可以使用许多不同的排序算法来实现(请参阅https://en.wikipedia.org/wiki/Sorting_algorithm了解一些想法)
  3. 还有一些其他数据结构可能更适合此任务,并且可以免费提供给您的数据。 TreeSet(https://docs.oracle.com/javase/9/docs/api/java/util/TreeSet.html