0
我正嘗試對java中的一組鏈接節點進行排序。 我剛開始編程,所以我不確定自己做錯了什麼。如何對一組鏈接節點進行排序
package rAymond;
public class RandomLinkLists {
public static void main(String[] args) {
int temp = 0;
Listnode holder;
Listnode start;
Listnode a;
Listnode b;
Listnode c;
Listnode d;
Listnode e;
Listnode end;
start = new Listnode(5);
a = new Listnode(10);
b = new Listnode(0);
c = new Listnode(3);
d = new Listnode(4);
e = new Listnode(100);
end = new Listnode(20);
holder = new Listnode(0);
start.setNext(a);
a.setNext(b);
b.setNext(c);
c.setNext(d);
d.setNext(e);
e.setNext(end);
holder.setNext(start);
while(start!=null && start.getNext()!=null){
if((Integer)start.getData() > (Integer)start.getNext().getData()){
temp = (Integer) start.getData();
start.setData(start.getNext().getData());
start.getNext().setData(temp);
}
start = start.getNext();
}
while(holder != null && holder.getNext()!=null){
System.out.println(holder.getData());
holder = holder.getNext();
}
}
}
和...
package rAymond;
public class Listnode {
//*** fields ***
private Object data;
private Listnode next;
private Listnode previous;
//*** methods ***
// 2 constructors
public Listnode(Object d) {
this(d, null);
}
public Listnode(Object d, Listnode n) {
data = d;
next = n;
}
// access to fields
public Object getData() {
return data;
}
public Listnode getPrevious(){
return previous;
}
public Listnode getNext() {
return next;
}
// modify fields
public void setData(Object ob) {
data = ob;
}
public void setNext(Listnode n) {
next = n;
}
public void setPrevious(Listnode n) {
previous = n;
}
public void remove(Listnode current, Listnode parent){
parent.setNext(current.getNext());
}
}
任何幫助將不勝感激。
最終輸出是:0 5 0 3 4 10 20
此外,有沒有對列表進行排序任何簡單的方法?普通的數組排序技術,如氣泡排序和快速排序,可以在這裏應用嗎?