首页 > 解决方案 > 如何使用字符串输入在通用链接列表中搜索节点的内容?

问题描述

我正在尝试返回与给定字符串输入匹配的所有节点内容。我想要做的本质上是一个非常简单的搜索引擎,用户可以在其中输入一个字符串,程序会返回它可以在链接列表中找到的所有特征相似的内容。链表本身是从一个文件构建的,格式为

<<游戏名称 0>>\t<<游戏控制台 0>>\n

<<游戏名称 1>>\t<<游戏控制台 1>>\n

其中行用 \n 分隔,游戏及其对应的控制台用 \t 分隔。

我目前的方法是使用 while 循环搜索链接列表,为头部分配一个临时值,并在它沿着列表向下时将其重新分配给它的链接。一旦循环在节点中找到与当前输入匹配的内容,它就会停止循环并返回在节点中找到的数据。我还没有尝试是否可以使用 for 循环来完成,因为 while 循环很可能在找到匹配项后不知道何时继续。我也不确定 while 循环参数是否是最有效的参数,因为我对它的理解非常少。我相信 !temp.equals(query) 是在声明“temp 不等于查询”,但我觉得这可以以更有效的方式完成。

这是我到目前为止所拥有的,为了上下文,我将提供整个 Generic 链表类,但我要质疑的方法是最后一个,在第 126 行找到。

我明确提出的问题是如何搜索链接列表的内容并通过控制台返回这些内容。

import java.io.FileNotFoundException;
import java.util.Scanner;

public class GenLL<T>
{
    private class ListNode
    {
        T data;
        ListNode link;

        public ListNode(T aData, ListNode aLink)
        {
            data = aData;
            link = aLink;
        }
    }

    private ListNode head;
    private ListNode current;
    private ListNode previous;
    private int size;

    public GenLL()
    {
        head = current = previous = null;
        this.size = 0;
    }

    public void add(T aData)
    {
        ListNode newNode = new ListNode(aData, null);
        if (head == null)
        {
            head = current = newNode;
            this.size = 1;
            return;
        }
        ListNode temp = head;
        while (temp.link != null)
        {
            temp = temp.link;
        }
            
        temp.link = newNode;
        this.size++;
    }

    public void print()
    {
        ListNode temp = head;
        while (temp != null)
        {
            System.out.println(temp.data);
            temp = temp.link;
        }
    }

    public void addAfterCurrent(T aData)
    {
        if (current == null)
            return;

        ListNode newNode = new ListNode(aData, current.link);
        current.link = newNode;
        this.size++;
    }

    public T getCurrent()
    {
        if(current == null)
            return null;

        return current.data;
    }

    public void setCurrent(T aData)
    {
        if(aData == null || current == null)
            return;

        current.data = aData;
    }

    public void gotoNext()
    {
        if(current == null)
            return;

        previous = current;
        current = current.link;
    }

    public void reset()
    {
        current = head;
        previous = null; 
    }

    public boolean hasMore()
    {
        return current != null;
    }
    
    public void removeCurrent()
    {
        if (current == head)
        {
            head = head.link;
            current = head;
        }
        else
        {
            previous.link = current.link;
            current = current.link;
        }

        if (this.size > 0)
            size--;
    }

    public int getSize()
    {
        return this.size;
    }

    public T getAt(int index)
    {
        if(index < 0 || index >= size)
            return null;
        ListNode temp = head;
        for(int i=0;i<index;i++)
            temp = temp.link;
        return temp.data;
    }

    public void setAt(int index, T aData)
    {
        if(index < 0 || index >= size || aData == null)
            return;

        ListNode temp = head;
        for (int i = 0; i < index; i++)
            temp = temp.link;
        temp.data = aData;
        
    }

    public T search() throws FileNotFoundException {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Search: ");
        String query = keyboard.nextLine();

        ListNode temp = head;

        while(!temp.equals(query))
            temp = temp.link;

        return temp.data;

        //plus some sort of print function to display the result in the console
    }
}

标签: javalinked-list

解决方案


您可以申请regex每个节点的内容,如果数据类型是string应用它,如果它是其他数据类型,string则尽可能将其转换为,否则抛出一些异常。


推荐阅读