java - 如何从链表中删除节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22902924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:48:03  来源:igfitidea点击:

java - how to delete a node from linkedlist?

javalinked-list

提问by user3505049

This code is a table that has an option to Inert name, delete, show, and quit .

此代码是一个表,其中包含 Inert name、delete、show 和 quit 选项。

this code run's well but my only problem is on how to delete a chosen name in a node

此代码运行良好,但我唯一的问题是如何删除节点中的选定名称

class Node{

Node in;
String name;

public Node(){

    in = null;

}

public Node(String n){

    in = null;
    name = n;

}

public void setIn(Node n){

    in = n;

}

public Node getIn(){

    return in;

}

public void setName(String n){

    name = n;

}

public String getName(){

    return name;

}




 public class Main{

public static void main(String args[]){
    Scanner scan = new Scanner(System.in);
    LinkedList bi = new LinkedList();
    while(true){

        System.out.println("Choose!\n[a] Insert Name\n[b] Delete\n[c] Show\n[d] Exit");
        char c = scan.next().charAt(0);
        System.out.println();

        if(c == 'a'){

            System.out.print("Enter Name: ");
            bi.insert(scan.next());
            System.out.println();

        }
        else if(c == 'b'){

            System.out.print("Enter Name to delete: ");
            bi.delete(scan.next());
            System.out.println();
        }
        else if(c == 'c'){

            bi.show();
            System.out.println();

        }
        else if(c == 'd'){

            System.exit(0);

        }

    }

}

  }


class LinkedList{

private Node root;

public LinkedList(){

    root = null;
}

public void insert(String n){

    root = insert(root, n);

}

private Node insert(Node n, String r){

    if(n == null){

        n = new Node(r);

    }
    else{

        n.in = insert(n.in, r);

    }

    return n;

}

public void delete(String n){

    root = delete(root, n);

}

private Node delete(Node n, String r){




}

public void show(){

    show(root);

}

private Node show(Node n){
    if(n == null){

        System.out.println("Empy list!");

    }
    else{

        while(n!=null){

            System.out.println(n.getName());
            n = n.getIn();

        }

    }

    return n;
}

 }

*i don't know how to delete a node . What should i put on my delete method?

*我不知道如何删除节点。我应该在我的删除方法上放什么?

public void delete(String n){

    root = delete(root, n);

}

private Node delete(Node n, String r){




}

采纳答案by gitesh.tyagi

To delete Node you actually need to update it's previous node's in to be deleting Node's in, and the left alone Node will eventually get garbage collected.

要删除节点,您实际上需要更新它的前一个节点以删除节点,而剩下的节点最终将被垃圾收集。

Just one catch if node to be deleted is the root node then update root node.

如果要删除的节点是根节点,则只有一个问题,然后更新根节点。

private Node delete(Node root, String data)
{
    //in case list is empty then return
    if(root==null) return n;
    //in case node to be deleted is root then just return next as new root
    if (root.name.equals(data)) return root.in;

    Node curr = root;
    while(curr.in!=null)
    {
        if (curr.in.name.equals(data))
        {
            //curr.in's referenced Node will be garbage collected (or run some cleanup manually on it)
            curr.in = curr.in.in;
            //we are done and root is same
            return root;
        }
        curr = curr.in;
    }
    //if here then not found and nothing changed
    return root;
}

回答by Quasitonality

Are you trying to remove the name from the node, or remove the node from the list? To remove the node from the list, use the LinkedList.remove(int index) method. You'll need to find the index of the node you want to remove first.

您是要从节点中删除名称,还是从列表中删除节点?要从列表中删除节点,请使用 LinkedList.remove(int index) 方法。您需要先找到要删除的节点的索引。

[edit] Like the others have said, you should try to solve the problem yourself, but here's a hint: you can access each node with LinkedList.get(int index). You can get the length of the list with LinkedList.size(). This is probably a good place for a "for" loop.

[编辑] 就像其他人所说的,您应该尝试自己解决问题,但这里有一个提示:您可以使用 LinkedList.get(int index) 访问每个节点。您可以使用 LinkedList.size() 获取列表的长度。这可能是“for”循环的好地方。

回答by Stephen C

We could code this for you, but that misses the point.

我们可以为您编写代码,但这没有抓住重点。

Instead, I'm going to suggest that you draw the linked-list data structure on paper using boxes for the list nodes and fields of the nodes, and arrows for the pointers / references. Then draw more boxes for your algorithm's local variables ... and "hand execute" it. That will help you visualize what your code shouldbe doing.

相反,我建议您在纸上绘制链表数据结构,使用列表节点和节点字段的框以及指针/引用的箭头。然后为算法的局部变量绘制更多框......并“手动执行”它。这将帮助您形象化您的代码该做什么。

Once you have done this kind of thing a few times, you will be able to visualize in your head ...

这种事情做几次之后,你就可以在脑海中想象……



can you please give me a sample ?

你能给我一个样品吗?

Sorry, but No. You will learn more by working it out for yourself. See above.

对不起,但不。您将通过自己解决来了解更多信息。看上面。

回答by Kartik Imley

while (node != null) {

            if (node.getNext() == null || head.getNext() == null) {
                break;

            } else if (head.getData() == data) {
                head = head.getNext();
            } else if (node.getNext().getData()==null&&data==null||node.getNext().getData().equals(data)) {
                node.setNext(node.getNext().getNext());
            } else {
                node = node.getNext();
            }
        }
    }

    return head;