我有一个我想要排序的节点列表。
节点:
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天。我尝试了其他的例子和所有这些,但我无法深究它。对这些数字进行排序的最简单方法是什么?
答案 0 :(得分:0)
LinkedList(您的实现或https://docs.oracle.com/javase/9/docs/api/java/util/LinkedList.html)会按照添加项目的顺序保留项目。在Java Collections LinkedList中,新添加的项目放在列表的末尾,而您的实现将它们放在开头 - 但无论是哪种方式都是它们的添加顺序,而不是与每个项目的'value'相关的一些顺序用于维持秩序的项目。
如果要根据值对列表中的项进行排序(在您的情况下我假设为int值),您可以:
还有一些其他数据结构可能更适合此任务,并且可以免费提供给您的数据。 TreeSet(https://docs.oracle.com/javase/9/docs/api/java/util/TreeSet.html)