Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用递归构建嵌套链表?_Java_List_Recursion_Linked List_Nested - Fatal编程技术网

Java 如何使用递归构建嵌套链表?

Java 如何使用递归构建嵌套链表?,java,list,recursion,linked-list,nested,Java,List,Recursion,Linked List,Nested,所以在课堂上,我们被要求阅读一个文本文件。格式如下: a b(cd(ef)g)h 我们需要构建一个嵌套的链表,如下所示: a b ……c ……d ……e ……f ……g h 每个左括号表示向下移动一个级别,然后右括号表示向上移动一个级别。 所以,a链接到b,b有一个到c的嵌套链接,c链接到d,d有一个到e的嵌套链接,e链接到f,d链接到g,然后b链接到h 我们正在使用修改的LLNode: 公共类LLNode { 受保护的LLNode链路; 受保护的节点嵌套; 受保护的T信息; 公共LLT节点(信

所以在课堂上,我们被要求阅读一个文本文件。格式如下:

a b(cd(ef)g)h

我们需要构建一个嵌套的链表,如下所示:

a

b

……c

……d

……e

……f

……g

h

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

我们正在使用修改的LLNode:

公共类LLNode {

受保护的LLNode链路;
受保护的节点嵌套;
受保护的T信息;
公共LLT节点(信息)
{
this.info=info;
link=null;
嵌套=空;
}
公共无效设置信息(T信息)
{
this.info=info;
}
公共T getInfo()
{
退货信息;
}
公共无效设置链接(LLNode链接)
{
this.link=link;
}
公共LLNode getLink()
{
返回链接;
}
公共void setNested(LLNode嵌套)
{
this.nested=nested;
}
公共LLNode getNested()
{
返回嵌套;
}
}


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

我知道我不应该给你家庭作业的答案,但我喜欢递归,所以我无法抗拒。请花一些时间理解并可能修改代码,然后再上交。聪明的老师会在StackOverflow中搜索,特别是当你交的东西出奇的好的时候:)

我必须向LLNode添加一个
父节点。在NodeParser类中可以有一个LLNode列表,而不是parent属性,该列表用于跟踪最近的父亲、祖父等

新的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;
    }

}
公共类LLNode{
受保护的节点链路;
受保护的节点嵌套;
受保护的LLNode父节点;//新字段
受保护的T信息;
公共LLT节点(信息){
parent=null;
}
公共void setParent(LLNode parent){
this.parent=parent;
}
公共LLNode getParent(){
返回父母;
}
}
然后是解析器(包括递归打印机!):

公共类节点分配器{
私有字符串str;
公共静态void main(字符串[]args){
新的NodeParser();
}
公共节点分配器(){
this.str=“ab(cd(ef)g)h(ij(k)l)mn”;
LLNode topNode=新的LLNode(空);
解析(topNode,false);
打印(“,topNode.getLink());
}
私有同步的void解析(LLNode节点,布尔嵌套){
如果(str.length()==0){
返回;
}
而(str.charAt(0)=''){
str=str.substring(1);
}
字符c=str.charAt(0);
str=str.substring(1);
如果(c=='('){
parse(node,true);
}如果(c=='),则为else){
解析(node.getParent(),false);
}否则,如果(c>='a'&&c请参见:
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());
}

}