我试图对一个自实现的双链接列表进行排序,但是它似乎无休止地迭代,我不确定我做错了什么。任何帮助都将不胜感激。
public void sortLine() {
Module current = this.getLeftMostModule();
Module next = this.getLeftMostModule().getRight();
while(next != this.getRightMostModule()) {
while(next != null) {
if(current.getName().compareToIgnoreCase(next.getName()) > 0) {
swap(current, next);
}
next = next.getRight();
}
current = current.getRight();
}
}
public void swap(Module current, Module next) {
boolean isLeft = false;
boolean isRight = false;
if(current.isLeftMostModule()) {
isLeft = true;
}
if(next.isRightMostModule()) {
isRight = true;
}
Module temp = current;
current = next;
next = temp;
if(isLeft) {
next.setLeftMostModule();
current.setNonMostModule();
}
else if(isRight) {
next.setNonMostModule();
current.setRightMostModule();
}
}
我还调用了一个交换函数。谢谢您。我不允许添加setName方法,并且不能访问getName之外的name字段。不过,我可以向这个类添加helper方法。