java实现链表冒泡排序,用冒泡排序和交换方法对双链表排序

我试图对一个自实现的双链接列表进行排序,但是它似乎无休止地迭代,我不确定我做错了什么。任何帮助都将不胜感激。

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方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值