【问题标题】:Java Circular Linked list, Deleting inconsistenciesJava循环链表,删除不一致
【发布时间】:2015-10-28 22:50:00
【问题描述】:

好的,所以我的想法是移动到循环列表中的每个节点(在这种情况下为用户)并询问他们是否要注销,他们会随机给出是或否的答案,直到每个人都登录离开。这似乎是我运行程序的大部分时间的情况,但有时用户正在重新登录,这不应该发生,我将发布我正在使用的删除方法和显示方法。

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
                    }
                }

【问题讨论】:

    标签: java adt nodes circular-list


    【解决方案1】:

    我认为你的问题出在这一行

    else if(!rand)
    

    添加一个布尔值来检查用户是否已被删除

    else if(!rand && !userExists)
    

    【讨论】:

    • 要创建一个 userExists 方法,我必须去命名我的所有节点?
    • @MickO'Gorman 好问题。您可能只能在创建对象时使用该对象的哈希码。见tldrify.com/c6m
    【解决方案2】:

    在上面的代码中,您在将temp 变量从该列表中删除之后引用了它。这可能会导致一些问题。调整为下面的代码。

        do {
             boolean rand=randomBoolean();
    
             if(rand) {
                 System.out.println("USER : " + temp.data + " Logged off ");
    
                 Node placeholder = temp.next; //save value of temp.next before we delete temp
                 delete(temp);
                 Node.numOfUsers--;
                 temp = placeholder; //reassign "temp" to the appropriate next value.
             } else {
                 System.out.println("USER : " + temp.data + " Logged on ");
                 temp = temp.nextNode; 
             }
    
         } while(Node.numOfUsers != 0);
    

    另外,有趣的事实。在您最初发布的代码中无需执行else if(!rand)。通过将您的第一个案例设置为if(rand),唯一正确的情况是rand == true 对吗?所以唯一的另一个合乎逻辑的情况是rand == false,所以甚至不需要第二个if 语句来检查这个,因为我们知道它不可能是别的。

    【讨论】:

    • 刚刚完成,我认为它可以工作几次,但最后一次我尝试其中一个用户重新登录时,我只是尝试添加 4 个用户
    • 出现不一致的原因是 rand 是随机的。有时它会偶然打印“登录”,因为您没有强制执行其他检查。考虑在您的节点类中添加一个布尔变量“isLoggedOut”,然后您可以在他们登录时说“else if(!temp.isLoggedOut)”,并获得可靠的结果。确保在从链表中删除时也将值切换为 false。
    • 好的,现在就尝试一下,我认为一旦我从列表中删除了一个节点,我就不需要担心它会通过登录而不是注销的过程返回。
    • 只是为了好的设计。您是否验证过您的删除方法 100% 有效
    • 好的,我做到了,但是这次经过几次之后,我让某人注销了两次,我很确定我之前检查过删除方法
    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 2011-08-02
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2020-12-03
    • 2015-07-31
    • 2018-08-30
    相关资源
    最近更新 更多