从链接列表中删除

时间:2013-12-14 06:49:31

标签: java linked-list nodes

我正在写一个程序作为字典。在程序中,您可以添加和删除单词,并打印当前和已删除的单词。我的问题在于我的删除方法,显然它正在删除单词,但它没有创建一个链接列表存储那些删除的单词然后打印它们。因此,它只打印我删除的最后一个单词而不是我删除的所有单词。这是方法:

public void delete(String b) 
{
    WordMeaningNode current = this.list;
    WordMeaningNode previous = null;
    WordMeaningNode temp;

    //While list is empty, return.
    if (this.list == null) 
    {
        return;
    }

    // While it is not empty..
    while (current != null) 
    {
        if (current.word.getWord().equalsIgnoreCase(b))
        {
            if (current == this.list)
            {

                this.list = this.list.next;
            } 
            else 
            {

                previous.next = current.next;
            }

            temp = current;
            del = temp;
            current.next = null;
            return;
        }

        previous = current;
        current = current.next;
    }
}

有人能看出我的错误吗?

1 个答案:

答案 0 :(得分:0)

我不确定del是什么,但假设它是已删除字词链表中的一个节点,您所做的就是将删除的字词分配给del = temp中的del。如果要将其添加到del链接列表中,则必须创建一个新节点并将其添加到例如...

WordMeaningNode newDelNode;
newDelNode = temp;
del.next = temp;

类似的东西......我不完全确定,因为阅读你的代码有点困难。