首页 > 解决方案 > 如何根据用户输入的名称删除某个节点

问题描述

我想删除链表中的某个节点,但是我不能使用java.util.LinkedList

最后的结果应该是这样的。

如何根据名称找到节点?

能不能根据名字找到nodes索引,按原路删除?

L = (mon, wed, fri)   
Please enter the desired data you want to delete. >>  wed    
L = (mon, fri)

添加我的原始代码,希望它会有所帮助。

package week4;

public class C {
    public static void main(String args[]) {
        LinkedList L = new LinkedList();
        System.out.println("Add three nodes.");     
        L.insertLastNode("mon");
        L.insertLastNode("wed");
        L.insertLastNode("sun");
        L.printList(); 
        
        System.out.println("Add fri behind wed.");          
        ListNode pre = L.searchNode("wed");
        if(pre == null) 
            System.out.println("Error >> No data found");
        else {
            L.insertMiddleNode(pre, "fri");
            L.printList();
        } 
        
        System.out.println("Delete last node.");
        L.deleteLastNode();
        L.printList();
        
        System.out.println("Please enter the desired data you want to delete. >> ");
    }
}
class LinkedList{
    private ListNode head;                          
    public LinkedList() {                       
        head = null;        
    }                   
    public void insertMiddleNode(ListNode pre, String data) {
        ListNode newNode = new ListNode(data);      //-> new |data|null|
        newNode.link = pre.link;                    //-> new |data|pre.link|
        pre.link = newNode;                         //-> pre |predata|new.link|
    }       
    public void insertLastNode(String data) {
        ListNode newNode = new ListNode(data);      //-> new |data|null|
        if(head == null) {                          
            this.head = newNode;                    
        } 
        else{                    
            ListNode temp = head;
            while(temp.link != null)                
                 temp = temp.link;                  
            temp.link = newNode;                            
        }
    }
    public void deleteLastNode() {
        ListNode pre, temp;
        if(head == null) 
        return;
        if(head.link == null) {
            head = null;
        }
        else {
            pre = head;
            temp = head.link;
            while(temp.link != null) {
                pre = temp;
                temp = temp.link;
            }
            pre.link = null;
        }
    }   
    public ListNode searchNode(String data) {
        ListNode temp = this.head;
        while(temp != null) {
            if(data == temp.getData())
                return temp;
            else temp = temp.link;
        }
        return null;
    }
    public void reverseList() {
        ListNode next = head;
        ListNode current = null;
        ListNode pre = null;
        while(next  != null) {
            pre = current;
            current = next;
            next = next.link;
            current.link = pre;
        }
        head = current;
    }
    public void printList() {
        ListNode temp = this.head;
        System.out.printf("L = (");
        while(temp != null) {
            System.out.printf(temp.getData());
            temp = temp.link;
            if(temp != null) {
                System.out.printf(", ");
            }
        }
        System.out.println(")");        
    }   
}

class ListNode {
    private String data;
    public ListNode link;
    public ListNode() {                           
        this.data = null;
        this.link = null;               
    }
    public ListNode(String data) {                
        this.data = data;
        this.link = null;
    }
    public ListNode(String data, ListNode link) { 
        this.data = data;
        this.link = link;
    }
    public String getData() {                    
        return this.data;
    }
}

标签: javalinked-listnodes

解决方案


您必须有一个方法来标识要删除的数据节点之前的节点。你已经有一个searchNode,所以它应该是相似的。

但是searchNode有一个问题:它没有以正确的方式比较字符串。您应该使用该.equals方法。更改此行:

    if(data == temp.getData())

到:

    if(temp.getData().equals(data))

这是您需要删除一个(或多个)具有给定数据的节点的方法:

public void deleteNode(String data) {
    if (head == null) 
        return;
    if (head.getData().equals(data)) {
        head = head.link;
        return;
    }
    ListNode temp = head;
    while (temp.link != null) {
        if (temp.link.getData().equals(data)) {
            temp.link = temp.link.link;
        } else {
            temp = temp.link;
        }
    }
}

在主程序中你会这样做:

System.out.println("Please enter the desired data you want to delete. >> ");
Scanner scanner = new Scanner(System.in);
String data = scanner.nextLine();
L.deleteNode(data);
L.printList();

推荐阅读