Java循环链接列表,删除不一致之处 [英] Java Circular Linked list, Deleting inconsistencies
本文介绍了Java循环链接列表,删除不一致之处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好,所以对我而言,想法是移动到循环列表中的每个节点(在本例中为用户)并询问他们是否要注销,他们将给出随机的是或否答案,直到每个人都登录为止关。在大多数情况下,我运行该程序似乎是这种情况,但有时用户会重新登录不应该发生的情况,我会发布我正在使用的delete方法和display方法。
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)
这篇关于Java循环链接列表,删除不一致之处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文