简体   繁体   English

Java LinkedList删除最后一个节点

[英]Java LinkedList removing the last node

I have a Java class which consists of a node list, WordNode , which has the attributes of the class Word and a WordNode object called next used as reference to the next node, as follows: 我有一个Java类,它由节点列表WordNode组成,该节点列表具有Word类的属性和一个称为next的WordNode对象,该对象用作对下一个节点的引用,如下所示:

class WordNode
{
    Word word;
    WordNode next;

    WordNode(Word w)
    {
        word = w;
        next = null;
    }

    Word getWord()
    {
        return word;
    }
}

And the class Word has a String called name : Word类有一个名为name的字符串:

class Word 
{
    String name;

    Word(String n)
    {
            this.name = n;
    }

    public String getName()
    {
            return name;    
    }

    public void setName(String n)
    {
        name = n;
    }
}

I have a class which is a custom LinkedList to which I must add and remove words by specifying the word name. 我有一个类,它是一个自定义LinkedList,必须通过指定单词名称在其中添加和删除单词。 I am able to add without problems, but when I want to delete I have some issues. 我能够添加而不会出现问题,但是当我要删除时会遇到一些问题。 Below is the method to delete: 以下是删除方法:

boolean remove(Word w)
{
    WordNode wm = new WordNode(w);

    if (list == null) return false; //can't delete on an empty list
    else
    {
        WordNode aux = list;

        while(aux != null)
        {
            if (wm.word.getName().compareTo(aux.word.getName()) == 0 ) //if the word to delete is found
            { 
                if (aux.next == null) //to erase the last element
                {
                    aux = null;       
                }
                else
                {
                    aux.word.setName(aux.next.word.getName()); //set current node's name to equal next node's

                    WordNode temp = aux.next.next;
                    aux.next = null; //to erase current node
                    aux.next = temp; //re-refer
                }                        
                return true;
            }
            else aux = aux.next;
        }

        return false; //reachable if word is not found
    }
}

Where list is supposed to be the LinkedList that holds all the nodes. list应该是包含所有节点的LinkedList。 aux is an auxiliary list that will cycle through the list to avoid un-linking. aux是一个辅助列表,它将在list循环显示,以避免取消链接。 So, if I choose to remove a WordNode I compare names. 因此,如果我选择删除WordNode,则会比较名称。 It is actually removing well when the node is at any place, except the last node: 实际上,当节点位于最后一个节点之外的任何位置时,它的移除效果很好:

if (aux.next == null) //to erase the last element
{
    aux = null;       
}

I'm hoping to make that node null to mark a new end to the list, but it doesn't get erased. 我希望将该节点设置为null,以将列表标记为新的结尾,但不会被擦除。 What could I change to erase the last element? 我可以更改哪些内容来擦除最后一个元素? Thank you for any help/suggestions in advance 感谢您的任何帮助/建议

You have to clear the WordNode's "next" pointer. 您必须清除WordNode的“下一个”指针。 As you don't have a "previous" pointer, you have to keep track of the previous WordNode manually. 由于没有“上一个”指针,因此必须手动跟踪上一个WordNode。

boolean remove(Word w)
{
    WordNode wm = new WordNode(w);

    if (list == null) return false; //can't delete on an empty list
    else
    {
        WordNode aux = list;
        WordNode prev = aux;

        while(aux != null)
        {
            if (wm.word.getName().compareTo(aux.word.getName()) == 0 ) //if the word to delete is found
            { 
                if (aux.next == null) //to erase the last element
                {
                    prev.next = null;
                    // Takes care of the case of a one-item list
                    aux = null;
                }
                else
                {
                    aux.word.setName(aux.next.word.getName()); //set current node's name to equal next node's

                    WordNode temp = aux.next.next;
                    aux.next = null; //to erase current node
                    aux.next = temp; //re-refer
                }                        
                return true;
            }
            else {
                prev = aux;
                aux = aux.next;
        }

        return false; //reachable if word is not found
    }
}

Add a head node. 添加head节点。 Then in your check for last node you will have: 然后,在检查最后一个节点时,您将拥有:

if (aux.next == null) //to erase the last element
{
   aux.head.next = null;
}

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

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