【问题标题】:How do I build a nested linked list using recursion?如何使用递归构建嵌套链表?
【发布时间】:2020-06-17 12:49:51
【问题描述】:

所以在课堂上我们被要求读一个文本文件。格式是这样的:

a b ( c d ( e f ) g ) h

我们需要像这样构建一个嵌套链表:

一个

b

.....c

.....d

.......e

.......f

.....g

h

每个左括号表示向下移动一个级别,然后一个右括号表示向后移动一个级别。 所以,a 链接到 b,b 有一个嵌套链接到 c,c 链接到 d,d 有一个嵌套链接到 e,e 链接到 f,d 链接到 g,然后 b 链接到 h

我们正在使用修改后的 LLNode:

公共类 LLNode {

protected LLNode<T> link;
protected LLNode<T> nested;
protected T info;
public LLNode(T info) 
{
    this.info = info;
    link = null;
    nested = null;
}
public void setInfo(T info) 
{
    this.info = info;
}
public T getInfo() 
{
    return info;
}
public void setLink(LLNode<T> link) 
{
    this.link = link;
}
public LLNode<T> getLink() 
{
    return link;
}
public void setNested(LLNode<T> nested)
{
    this.nested = nested;
}
public LLNode<T> getNested()
{
    return nested;
}

}

我们需要使用递归来读取输入文件,为每个字母创建节点并相应地链接它们。我不知道如何递归处理这个问题。非常感谢任何帮助。

【问题讨论】:

标签: java list recursion linked-list nested


【解决方案1】:

我知道我不应该给你作业的答案,但我喜欢递归,所以我无法抗拒。请花一些时间理解并可能修改代码,然后再提交。聪明的老师会通过 StackOverflow 搜索,特别是如果你提交的东西非常好:)

我必须将parent 添加到 LLNode。除了 parent 属性,您可以在 NodeParser 类中拥有一个 LLNode 列表,该列表跟踪最近的父亲、祖父等。

新的 LLNode:

public class LLNode<T> {

    protected LLNode<T> link;
    protected LLNode<T> nested;
    protected LLNode<T> parent; // New field
    protected T info;

    public LLNode(T info) {
        <old code>
        parent = null;
    }

    <old code>

    public void setParent(LLNode<T> parent) {
        this.parent = parent;
    }

    public LLNode<T> getParent() {
        return parent;
    }

}

然后是解析器(包括递归打印机!):

public class NodeParser {

private String str;

public static void main(String[] args) {
    new NodeParser();
}

public NodeParser() {
    this.str = "a b ( c d ( e f ) g ) h ( i j ( k ) l ) m n";
    LLNode<Character> topNode = new LLNode<>(null);
    parse(topNode, false);
    print("", topNode.getLink());
}

private synchronized void parse(LLNode<Character> node, boolean nested) {
    if (str.length() == 0) {
        return;
    }
    while (str.charAt(0) == ' ') {
        str = str.substring(1);
    }
    char c = str.charAt(0);
    str = str.substring(1);

    if (c == '(') {
        parse(node, true);
    } else if (c == ')') {
        parse(node.getParent(), false);
    } else if (c >= 'a' && c <= 'z') {
        LLNode<Character> nextNode = new LLNode<>(c);
        if (nested) {
            node.setNested(nextNode);
            nextNode.setParent(node);
        } else {
            node.setLink(nextNode);
            nextNode.setParent(node.getParent());
        }
        parse(nextNode, false);
    }
}

private void print(String indent, LLNode<Character> node) {
    if (node == null) {
        return;
    }
    System.out.println(indent + node.getInfo());
    print(indent + "  ", node.getNested());
    print(indent, node.getLink());
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多