简体   繁体   English

使用java删除链表中的节点

[英]removing node in linkedlist using java

I am trying to code a method for deleting the last Node in a linkedlist(for learning how to modify a linkedlist..I am not using the java library LinkedList class)..I tried to deal with the usecase where the passed linkedlist has only one Node. 我正在尝试编写一种方法来删除链表中的最后一个节点(用于学习如何修改链表。。我不使用Java库LinkedList类)。我试图处理传递的链表仅包含的用例一个节点。

However when I try to print the linkedlist before and after the removal, it gives the same output as if the node was not removed. 但是,当我尝试在删除前后打印链表时,它给出的输出与未删除节点时相同。

class NodeProcessing{
    public static void removeLastNode(Node f){
        if (f==null) return;
        if(f.next == null){//if linkedlist has single node
            f = null;
            return;
        }
        ...
    }

    public static void showList(Node first){
        System.out.println("linked list=");
        for(Node x = first; x != null; x = x.next){
            System.out.print(x.item+" ,");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node a = new Node();
        a.item = "one";
        showList(a);
        removeLastNode(a);
        showList(a);
    }

}
class Node{
    String item;
    Node next;
}

Output: 输出:

linked list= one , 链表=一个,

linked list= one , 链表=一个,

update: when I used the debugger,I can see that the Node a in main() has address : Node@a61164 and the Node f inside removeLastNode() also has : Node@a61164 更新:当我使用调试器时,我可以看到main()中的Node a地址为: Node@a61164removeLastNode()内部的Node f地址也为: Node@a61164

Setting f to null does nothing - that just changes the value of the parameter , which is just a local variable. f设置为null不会执行任何操作-只会更改parameter的值,而那只是一个局部变量。 It's important to understand that Java always uses pass-by-value for parameters - the value is a reference when the parameter type is a class type, but that reference is passed by value. 重要的是要理解Java始终对参数使用按值传递-当参数类型是类类型时,该值是引用,但该引用是按值传递的。 Changing the parameter value to a different reference doesn't change the caller's argument. 将参数值更改为其他引用不会更改调用方的参数。

Ideally you'd want to separate the concept of "the list" from "a node in the list" (just as the Java API does). 理想情况下,您希望将“列表”的概念与“列表中的节点”分开(就像Java API一样)。 That way when you call remove on a list , it mutates the list itself . 这样,当您在列表上调用remove时,它会更改列表本身 In your current model of "a node is just the head of a list" there's no way you can remove the final node - you can't destroy the node itself. 在当前的“节点只是列表的头”模型中,您无法删除最后一个节点-您无法销毁节点本身。

The closest you could come would be to make removeLastNode return a Node reference - which could return null if the notional list is now empty. 最接近的方法是使removeLastNode 返回一个Node引用-如果概念列表现在为空,则它可以返回null

The Node f is local variable in the following method 在以下方法中,Node f是局部变量

public static void removeLastNode(Node f)

Since it is a copy of the reference to the actual argument, the changes made to the parameters will not affect the arguments. 由于它是对实际参数引用的副本,因此对参数所做的更改不会影响参数。

you are doing 你在做

f = null

where as 在哪里

Node first

will be still pointing to the existing node. 仍将指向现有节点。 You are using the argument to print the linked list, Hence the result. 您正在使用参数来打印链接列表,从而显示结果。

You can use the java provided LinkedList data structure to do this. 您可以使用Java提供的LinkedList数据结构来执行此操作。

    LinkedList items = new LinkedList();
    items.add("one");
    items.add("two");

    System.out.println(items);
    items.removeLast();
    System.out.println(items);

This will generate the desired output. 这将生成所需的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM