繁体   English   中英

从链接列表中删除项目

[英]Remove Item from Linked List

我添加了注释来解释我的逻辑

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; 

做这样的事情,设置previous.nxt = nodetobedeleted.next; 以及 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

    }

总而言之,您的问题需要在链表中查找和删除节点,而不管其在链中的位置,因此,它可能是第一个、最后一个或中间的某个位置。

下面提供了一些关于该问题的安全假设。

  1. Game类具有以下结构。

     class Game{ public String name; public Game prev; public Game next; }
  2. 对象已在包含类中定义。

removeGame()方法必须在以下三组场景中编码,这些场景将隐式处理节点删除。 如果它返回一些东西,您可以获得有关成功删除的信息。 这里它返回布尔值。 它实际上基于主题,需要删除的主题节点必须成为垃圾收集的候选者。

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

此外,由于您正在执行 java 实现,因此更喜欢使用 java 的 util 包的LinkedList类来适应您的场景。

尝试使用此逻辑,它适用于两种方式

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