简体   繁体   English

删除单个链表中的节点

[英]deletion of node in single linked list

The while loop is only making the head of the linked list go through and saying it's deleting it. while循环仅使链接列表的开头经过并说要删除它。 But there is no deletion. 但是没有删除。 I want to delete a node in the middle but that is not happening either. 我想删除中间的一个节点,但这也没有发生。

I've tried equals method and I've tried compareTo method 我尝试过equals方法,并且尝试过compareTo方法

//******************Deletion by Key***********************
//Method to delete a node in the Linked List by key
public static LinkedList deleteByKey(LinkedList list, String first, String last) {
    //Store head node
    Giftor currNode = new Giftor(null, null);
    currNode = list.head;
    Giftor prev = new Giftor(null, null);
    //prev= null;
    System.out.println("Deleting: " + first + " " + last);
    System.out.println("currNode.firstName " + currNode.firstName);

    //
    //Case 1:
    //If head node itself holds the key to be deleted
    if(currNode != null && currNode.firstName.equals(first) && currNode.lastName.equals(last)) {
        list.head = currNode.next;
    System.out.println(first + " " + last + " found and deleted");
    return list;
    }

    //
    //CASE 2:
    //If the key is somewhere other than head
    // 

    // Search for the key to be deleted, 
    // keep track of the previous node 
    // as it is needed to change currNode.next 
    while (currNode != null && (currNode.firstName.compareTo(first) == 0
             && currNode.lastName.compareTo(last) == 0)) { 
        // If currNode does not hold key 
        // continue to next node 
        prev = currNode;  
        currNode = currNode.next; 
    }

    //If the firstName and lastName were present, it should be at currNode
    //Therefore the currNode should not be null
    if(currNode != null) {
        //Since the firstName/lastName is at currNode
        //Unlink currNode from linked list
        prev.next = currNode.next;
        //Display the message
        System.out.println(currNode.firstName + " " + currNode.lastName + " found and deleted");
    }

    // 
    // CASE 3: The key is not present 
    // 

    // If key was not present in linked list 
    // currNode should be null 
    if (currNode == null) { 
        // Display the message 
        System.out.println(currNode.firstName + " " + currNode.lastName + " not found"); 
    } 

    // return the List 
    return list; 

}//end of deleteByKey
LinkedList: 
Micheal Womack
Randall Womack
Rita Evans
Trent Beck
Chris Baird
Lisa Adams
Deleting: Rita Evans
currNode.firstName Micheal
Micheal Womack found and deleted
LinkedList: 
Micheal Womack
Randall Womack
Rita Evans
Trent Beck
Chris Baird
Lisa Adams
LinkedList: 
Micheal Womack
Randall Womack
Rita Evans
Trent Beck
Chris Baird
Lisa Adams
Deleting: Rita Evans
currNode.firstName Micheal
Micheal Womack found and deleted
LinkedList: 
Micheal Womack
Randall Womack
Rita Evans
Trent Beck
Chris Baird
Lisa Adams

The while loop in case 2 seems wrong to me. 在情况2中的while循环对我来说似乎是错误的。 It would iterate only if the current node's first name and last name are equal to first and last . 它会遍历只有在当前节点的第一个名字和姓氏等于 firstlast Therefore, if the head node does not match, the loop body won't execute at all. 因此,如果头节点不匹配,则循环体将根本不会执行。 If it did match, case 1 would have already taken care of that. 如果匹配,情况1将已经解决。

Try changing it to something like: 尝试将其更改为:

while (currNode != null && (!currNode.firstName.equals(first) 
                            || !currNode.lastName.equals(last))) { 
        // If currNode does not hold key 
        // continue to next node 
        prev = currNode;  
        currNode = currNode.next; 
    }

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

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