简体   繁体   中英

Remove Item from Linked List

I have added comments to explain my logic

public void removeStrin(String name) throws IllegalArgumentException {
    // If the name is equal to null throw an exception
    if(name == null) {
        throw new IllegalArgumentException();
    }

    //set the removeTempString to the head
    Game Rem = head; 

Do something like this, set the previous.nxt = nodetobedeleted.next; and empty and resources the notobedeleted might have.

        public void removeGame(String name) throws IllegalArgumentException {
            // If the name is equal to null throw an exception
            if(name == null) {
                throw new IllegalArgumentException();
            }

            //set the removeTempString to the head
            Game removeTempString = head;
            Game prev = null;
            //while it is not null
            while(removeTempString != null) {
                //if the removeTempString.name equals the String name
                if(removeTempString.name.equals(name)){
                     if(prev != null){
                          prev.next  = removeTempString.next;
                     }
                }
                // set prev to current`enter code here`
                prev = removeTempString;
                // to iterate set it to next node in list
                removeTempString = removeTempString.next;//the next pointer you have

    }

Summatively, your problem calls for finding and removing a node in linked list irrespective of its position in chain, wherefore, it could be the first one, last one or somewhere in the middle.

Provided underneath are some safe assumption(s) regarding the problem.

  1. The Game class has the following structure.

     class Game{ public String name; public Game prev; public Game next; }
  2. The head object is already defined in the containing class.

The removeGame() method must be coded in the following three set of scenarios which will take care of node deletions implicitly. If it returns something, you can get infos on successful deletions. Here it returns boolean. Its actually based on the theme that the subject node which needs to be deleted must be made a candidate of garbage collection.

public boolean removeGame(Game txt)
{
if(txt==null)return false;
Game itr = head;

//if its header
if(itr.name.equals(txt.name))
{
Game newHead = head.next;
head=newHead;
//The previous header hence gets derefrenced and becomes a candidate of garbage collection
return true;
}

//if its midway or end
itr = head.next;
while(itr!=null){
Game subjectNode = itr;
if(subjectNode.name.equals(txt.name))
 {
 Game newPrev = subjectNode.prev;
 Game newNext = subjectNode.next;
 newPrev.next = subjectNode.next;
 newNext.prev= subjectNode.prev;

 subjectNode=null;
//This makes it derefrenced and hence subject to garbage collections after refrence swaps.
     return true;
     }
    itr = itr.next;
     }
    return false;
     }

Also, since you are doing java implementation, prefer fitting your scenario using LinkedList class of java's util package.

Try to use this logic, it applies both ways

public Object delete(Object key) // works
{
    Node prev = null;
    Node curr = top;
    Object result = null;

    //search for the key
    while((curr != null) && (!curr.getData().equals(key)))
    {
        prev = curr;
        curr = curr.getNext();
    }

    //found the item we are looking for!
    if ( curr != null )
    {
        //wait! is the item we are looking for the first element in the list?
        if ( prev != null )//nah, it's not 
        {
            prev.setNext(curr.getNext());
        }else //yes! it is 
        {
            top = curr.getNext();
        }
    }

    return result;
}

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