Java链表搜索方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20298496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:55:59  来源:igfitidea点击:

Java Linked List Search Method

javasearchlinked-list

提问by Xavier

Hello so i was unable to complete my assignment because I had difficulties figuring out how to set up the search method within my linked list.. Now I still want to learn how to configure the linked list and i cant make any of the other methods such as remove, or replace if i cant figure out the search method. for some reason my results keep turning out null This is what i have so far..

您好,我无法完成我的作业,因为我很难弄清楚如何在我的链表中设置搜索方法。现在我仍然想学习如何配置链表,但我无法使用任何其他方法,例如如果我无法弄清楚搜索方法,请删除或替换。出于某种原因,我的结果一直为空 这就是我目前所拥有的..

public class WordList {
private WordMeaningNode list;
WordList()
{
    list = null;
}
void add(WordMeaning b)
{
    WordMeaningNode temp = new WordMeaningNode(b);
    try
    {
        temp.Next = list;
        list = temp;
    }
    catch(NullPointerException e)
    {
        System.out.println("Null Pointer Exception");
    }
}
public String toString()
{
    String result="";
    WordMeaningNode current = list;
    while (current != null)
    {
        result += current.Words.word + " - " + current.Words.meaning + "\n";
        current = current.Next;
    }
    return result;
}
public WordMeaning findword (WordMeaning Word)
{

    WordMeaningNode Checker = new WordMeaningNode(Word);

    boolean found = false;

    if (list.isEmpty() == true)
    {
        return null;
    }
    else
    {
        while(list.Next != null && !found)
        {
            if(list.Words.word.compareTo(Checker.Words.word)== 0)
            {
                found = true;
            }
            else
            {
                list = list.Next;
                found = false;
            }
        }
    }
    if(found == true)
    {
        return list.Words;
    }
    else
    {
        return null;
    }
}

回答by Domi

You version actually changes the list(which is actually the root (or first) node of your list) itself in every iteration which breaks the list. Your query method should not alter the list itself. Simply use a temporary (local) node variable (mine is called current) in your while loop instead, like so:

您的版本实际上会list在每次破坏列表的迭代中更改(实际上是列表的根(或第一个)节点)本身。您的查询方法不应更改列表本身。只需current在 while 循环中使用临时(本地)节点变量(我的称为),如下所示:

public WordMeaning findWord (WordMeaning word)
{
    if (list.isEmpty())
    {
        return null;
    }

    boolean found = false;
    WordMeaningNode current = list;
    while(current != null)
    {
        if(current.Words.word.compareTo(word)== 0)
        {
            found = true;
            break;
        }
        current = current.Next;
    }
    return current;
}

回答by Dima Condur

This is an example of how to find an element in a linked list:

这是如何在链表中查找元素的示例:

public class LinkedList<T> {

    private LinkedList<T> next;
    private T data;

    public LinkedList(T value) {
        data = value;
    }

    public LinkedList<T> next() {
        return next;
    }

    public T value() {
        return data;
    }

    public void setNext(LinkedList<T> element) {
        next = element;
    }

    public void setValue(T value) {
        data = value;
    }
}

public LinkedList<Integer> find(LinkedList<Integer> head, int data) {
    LinkedList<Integer> elem = head;
    while (elem != null && elem.value() != data) {
        elem = elem.next();
    }
    return elem;
}