java - 如何从链表中删除节点? [英] java - how to delete a node from linkedlist?

查看:35
本文介绍了java - 如何从链表中删除节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This code is a table that has an option to Inert name, delete, show, and 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!
[a] Insert Name
[b] Delete
[c] Show
[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){




}

推荐答案

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

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

这篇关于java - 如何从链表中删除节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆