java移走失效软链接,Java循环链接列表,删除不一致之处

Ok so the idea for me is to be moving to each node(user in this case) in a circular list and asking if they would like to log off,they will give a random yes or no answer,until everyone has logged off. this seems to be the case most of the time i run the program but sometimes users are logging back on which shouldn't happen,I will post the delete method and the display method i am using.

public void displayLinkedList() {

temp=first;

int i = 1;

do {

boolean rand=randomBoolean();

if(rand) {

System.out.println("USER : "+temp.data+" Logged off ");

temp.isloggedOut=true;

Node placeholder = temp.nextNode; //save value of temp.next before we delete temp

delete(temp);

Node.numOfUsers--;

temp = placeholder; //reassign "temp" to the appropriate next value.

} else if(!temp.isloggedOut) {

System.out.println("USER : "+temp.data+" Logged on ");

temp=temp.nextNode;

}

} while(Node.numOfUsers!=0);

}

public void delete(Node n) {

if(Node.numOfUsers == 0 || n == null) return; // 0 nodes or null parameter.

Node temp = first;

if(temp.nextNode == null) { //only one node

temp = null; //simply delete it

} else {

while(temp.nextNode != n) {

temp = temp.nextNode;

if(temp == first) { //if we circle the entire list and don't find n, it doesn't exist.

return;

}

}

temp.nextNode = n.nextNode; // perform the switch, deleting n

}

}

解决方案

I think your problem is in this line

else if(!rand)

Add a boolean that checks if the user has been deleted

else if(!rand && !userExists)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值