我的程序没有对列表进行排序,我无法找出问题所在。排序前和排序后的列表是相同的。
public void SelectionSort(){
for (Node index = head; ((index != null)&&(index.getnext()!=null)); index = index.getnext()) {
Node min = index;
for (Node test = min.getnext(); test != null; test = test.getnext()) {
if (test.getAcc().compareTo(min.getAcc()) < 0){
min = test;
}
}
if(index!=min){
Node temp = new Node();
temp=index;
index=min;
min =temp;
}
}
}
下面是我的类节点:
public class Node {
private int ID;
private String Acc;
private String Symbol;
private String Function;
private String UniGene;
private String Chromosome;
private Node next;
public Node(){
}
public Node(int id, String acc,String unigene, String symbol, String chromosome, String function){
ID=id;
Acc=acc;
Symbol=symbol;
UniGene = unigene;
Chromosome = chromosome;
Function=function;
}
public void displayNode() // display
{
System.out.print("{"+ID+","+Acc+","+Symbol+","+Function+"} n");
}
int getID(){
return ID;
}
String getAcc(){
return Acc;
}
String getUniGene(){
return UniGene;
}
String getSymbol(){
return Symbol;
}
String getChromosome(){
return Chromosome;
}
String getFunction(){
return Function;
}
void setnext(Node newnode)
{
next = newnode;
}
Node getnext()
{
return next;
}
}
我认为
问题是您在移动节点时需要注意next
指针。在原始代码中,您只交换 2 个引用,但不会更改列表中的顺序:
Node next = min.getnext();
min.setnext(index);
index.setnext(next);
这不会直接起作用,但问题就在那里。你需要保存"previous"节点,并设置previous.setnext(index)或类似的东西。
顺便说一句:
Node temp = new Node();
temp=index;
您创建了一个新节点,但不使用它,因为在下一行中您将索引分配给 temp。
Node temp = index;