【问题标题】:remove function in my linked list not working我的链接列表中的删除功能不起作用
【发布时间】:2020-10-16 21:17:27
【问题描述】:

我应该构建一个方法来删除单链表中给定值的第一个实例。但是,每当我尝试测试此方法时,它都会卡住,我必须强制终止代码。

编辑:根据建议,我制作了一个修改版本的方法 Contains,该方法现在运行良好,并且消除了 Contains 的无意义重复。很高兴现在代码可以正常工作了!

这是我的方法代码:

public boolean remove(Anything m) {
    //INCOMPLETE
    
     if (this.first==null) {
        System.out.println("there are no values in the list");
        return false;
    }
    
    boolean returnValue;
    returnValue=false;
    if (this.contains(m)==true) { 
       Node temp=first;
        while(temp.next!=null) {
            if (temp.next.data==m) {
                temp=temp.next.next;
                temp.next=null;
                returnValue=true;
        
    }
            else 
            returnValue=false;
        }
    }
    return returnValue;
}

这是我测试该方法的代码:

 list13.addFirst("node5"); list13.addFirst("node4"); list13.addFirst("node3"); list13.addFirst("node2"); list13.addFirst("node1");
         System.out.println("5-element list: " + list13);
System.out.println("Testing remove...");
        System.out.println(list13.remove("node3"));

为了以防万一,如果需要,这是我的作业附带的预构建代码:

public class CS2LinkedList<Anything>
{  
    // the Node class is a private inner class used (only) by the LinkedList class
    private class Node
    {
        private Anything data;
        private Node next;
        
        public Node(Anything a, Node n)
        {
            data = a;
            next = n;
        }
    }
    
    private Node first;
    private Node last;
    
    
    public CS2LinkedList()
    {
        first = null;
    }
    
    public boolean isEmpty()
    {
        return (first == null);
    }
    
    public void addFirst(Anything d)
    {
         Node temp = first;
         first = new Node(d,temp);
    }
    
    
    public void clear()
    {
        first = null;
    }
    
    public boolean contains(Anything value)
    {
        for (Node curr = first; curr != null; curr = curr.next)
        {
            if (value.equals(curr.data))              {
                return true;
            }
        }
        return false;
    }
    
    
    public String toString()
    {
        StringBuilder result = new StringBuilder();  //String result = "";
        for (Node curr = first; curr != null; curr = curr.next)
            result.append(curr.data + "->");  //result = result + curr.data + "->";
        result.append("[null]");
        return result.toString();   //return result + "[null]";
    }
    ```

【问题讨论】:

  • 你试过使用调试器吗?此外,您不需要在删除方法中使用contains(),因为它会遍历整个列表
  • 同意伊万。看看 contains 的实现——它只是你想要的,除了它不删除节点。您的问题是您在删除节点后继续遍历列表,因此您将 returnValue 设置回 false。此外,您删除节点的步骤存在错误。尝试使用几个局部变量,以免您感到困惑。请记住,当您找到节点时,您需要知道前一个节点是什么,因为您必须更改它的下一个指针。
  • 既然这是一个单链表,我该如何跟踪前一个节点?
  • 由于这是一个单链表,我将如何跟踪前一个节点? - 你不会。单链表元素by definition 不包含对前一个元素的引用。

标签: java linked-list singly-linked-list


【解决方案1】:

一些问题:

  • 在比赛中,您将在要删除的节点之后之后的节点重新分配给temp,然后然后清除temp.next。这会在要删除的节点之后 破坏列表。

  • if 条件不成立时,while 循环不会更改temp 的值。所以循环可以挂起。

  • 确定要删除的节点后,您可以停止搜索。因此,您不需要while 循环内的else

        while(temp.next!=null) {
            if (temp.next.data==m) {
                // skip the node by modifying `temp.next`:
                temp.next = temp.next.next;
                returnValue=true;
                break; // we removed the targeted node, so get out
            }
            temp = temp.next; // must move to next node in the list
        }

很遗憾,您先用this.contains(m) 迭代列表,只是再次迭代它以找到相同的节点再次。我将删除 if 行,然后执行任何方式的循环:它将检测列表是否包含该值。

请注意,您的函数没有删除列表的第一个节点的规定。它开始比较第一个节点之后。您可能希望涵盖这种边界情况。

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多