0

删除节点时替换方法会是什么样子?

4

1 回答 1

1

以下逻辑应该起作用:

if (position.c > 0) {
    position.c--; 
    if (position.c == 0) {
        // delete entire node
        if (previous == null) {
            // delete head node
            head = position.next;
        } else {
            // delete a node with a predecessor
            previous.next = position.next;
        }
    }
}

请注意,我已经next从您的代码中撤消了分配。position您所做的只是在(从那时起)使用循环引用终止列表previous.next == position

顺便说一句,您不需要if这里的声明:

found = true;
if(found==true){
    . . .
}

它可以是:

found = true;
. . .
于 2013-04-19T03:44:19.223 回答