简体   繁体   中英

Remove String node from single linked list java

Hello I'm learning singly linked list and I'm using an example from a java book, I'm trying to delete a node given an string value. I already coded but i doesnt delete anything, anyone can give me any advice? Im already frustrated because I dont know what Im doing wrong. Thanks.

   public class LinkedStringLog implements StringLogInterface {
  protected LLStringNode log; // reference to first node of linked 
                              // list that holds the StringLog strings
  protected String name;      // name of this StringLog

  public LinkedStringLog(String name)
  // Instantiates and returns a reference to an empty StringLog object 
  // with name "name".
  {
    log = null;
    this.name = name;
  }
  public void remove(String element){
  LLStringNode currentNode;
  LLStringNode temporal;
  currentNode = log;
  temporal = currentNode.getLink();

  if(element.equalsIgnoreCase(currentNode.getInfo())){
      log = currentNode.getLink();

  } 

 while(currentNode!=null){
      if(element.equalsIgnoreCase(currentNode.getInfo())){
          temporal.setLink(currentNode.getLink());
      }
      else{
          currentNode.getLink();
          temporal = currentNode;
      }

  }

I guess you're entering in a infinite loop, because you're not updating currentNode variable in the while loop.

You may want something like this:

while(currentNode!=null){
      if(element.equalsIgnoreCase(currentNode.getInfo())){
          //don't you want to update the link of the node before currentNode here?
      }
      else{
          currentNode = temporal; //update currentNode variable
          temporal = currentNode.getLink(); //update temporal variable
      }

  }

You seem to have many errors.

One of the main issues was that you failed to maintain a prevNode reference as you traverse the linked list, so you were not able to keep all the items in the list linked together.

Also, where do you set log to the head item of the linked list?

In any case, this version of remove might work better (as long as log is actually non-null):

     public void remove(String element) {
         if (log == null) {
             return;
         }
         LLStringNode currentNode = log;
         LLStringNode prevNode = null;

         while (currentNode != null) {
             LLStringNode nextNode = currentNode.getLink();
             if (element.equalsIgnoreCase(currentNode.getInfo())) {
                 if (currentNode.equals(log)) {
                     log = nextNode;
                 }
                 if (prevNode != null) {
                     prevNode.setLink(nextNode);
                 }
             } else {
                 prevNode = currentNode;
             }
             currentNode = nextNode;
         }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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