简体   繁体   English

从链接列表中删除项目

[英]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;做这样的事情,设置previous.nxt = nodetobedeleted.next; and empty and resources the notobedeleted might have.以及 notobedeleted 可能拥有的空和资源。

        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. Game类具有以下结构。

     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. removeGame()方法必须在以下三组场景中编码,这些场景将隐式处理节点删除。 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.此外,由于您正在执行 java 实现,因此更喜欢使用 java 的 util 包的LinkedList类来适应您的场景。

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

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

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