Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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_Data Structures_Singly Linked List - Fatal编程技术网

Java 二维链表

Java 二维链表,java,data-structures,singly-linked-list,Java,Data Structures,Singly Linked List,有二维链表,如何使用单链表来构建此数据结构?A> B>C>D是链表,一个节点有1>2>3个链表 A [] [] > 1[] > 2[] > 3[null] | B [] [] > 1[] > 2[null] | C [] [] > 1[] > 2[] > 3[] > 4[] > 5[null] | D[null][] > 2[] > 3[] > 4[null] 如何添加链接列表中的每个节点都有

有二维链表,如何使用单链表来构建此数据结构?A> B>C>D是链表,一个节点有1>2>3个链表

A [] [] > 1[] > 2[] > 3[null]
   |
B [] [] > 1[] > 2[null]
   |
C [] [] > 1[] > 2[] > 3[] > 4[] > 5[null]
   |
D[null][] > 2[] > 3[] > 4[null]
  

如何添加链接列表中的每个节点都有一个关联的链接列表。

这样,您将无法将值放入正确的节点中。

A [] [] > 1[] > 2[] > 3[null]
   |
B [] [] > 1[] > 2[null]
   |
C [] [] > 1[] > 2[] > 3[] > 4[] > 5[null]
   |
D[null][] > 2[] > 3[] > 4[null]
  
public class Node 
public Object data;   
public Node next;
public Node down;
public Node (Object data) {
    this.data = data;
    this.next = null;
    this.down = null;
}

public Node (Object data, Node next) {
    this.data = data;
    this.next = next;
}

private Node head;
private Node tail;

public void addHead(Object item) {
        if (isEmpty()) {
            head = tail = new ListNode(item);
        } else {
            head = new ListNode(item, head);
        }
    }

public void addTail(Object item) {
    if (isEmpty()) {
        head = tail = new Node(item);
    } else {
        tail.next = new Node(item);
        tail =  tail.next;
    }
}
在这种情况下,您必须执行类似操作

s.addToTail('A');
s.addToTail(1);
s.addToTail(2);
s.addToTail(3);
System.out.println(s);

假设父节点是
{A,B,C,D}
,子节点是每个父节点的子节点

要插入父节点,请执行以下操作:

s.addToTail('A');
Node tail = s.tail;
tail.addToTail(1);
tail.addToTail(2);
tail.addToTail(3);
System.out.println(s);
我们可以使用以下代码将父节点添加到链接的末尾

1. add node at the end / down of parent node link. 
例如,如果您有父节点列表
{A,B,C,D}
,并且您想要添加另一个父节点
k
,那么上面的代码将在父节点链接的最后位置添加节点
k

static Node head, tail;
private static void addHeadNode(Object item) {
    if(head == null && tail == null) {
        head = new Node(item);
        tail = head;
    }else {
        /* add node at the end */
        tail.down = new Node(item);
        /* make tail points to at the end again */
        tail = tail.down;
    }
}
现在,要为特定父节点添加子节点,您可以按照以下过程进行操作:

|Before Adding Node K|                    |After Adding Node K|

     A                                           A
     |                                           |
     B                                           B
     |                                           |
     C                                           C
     |                                           |
     D                                           D
                                                 |
                                                 K
上述程序的代码应为:

1. Traverse the parent node
1.1. if node found add node at the end of this parent
1.2. if node not found, do nothing.
遍历父节点:

private static void addOnHead(Object headNode, Object item) {
    Node temp = head;
    // traverse the parent node.
    while(temp != null && temp.data != headNode) {
        temp = temp.down;
    }
    /* if parent node found, add child node at the end of the parent */
    if(temp != null) {
        Node temp1 = temp;
        
        while(temp1.next != null) {
            temp1 = temp1.next;
        }
        temp1.next = new Node(item);
    }else {
        /* do nothing */
        System.out.println("no head node found!");
    }
}
遍历特定子节点:

private static void traversingParentNodes() {
    Node temp = head;
    
    while(temp != null) {
        System.out.println(temp.data);
        temp = temp.down;
    }
}
遍历整个列表:

private static void traverseAChildNode(Object item) {
    Node temp = head;
    
    while(temp != null && temp.data != item) {
        //System.out.println(temp.data);
        temp = temp.down;
    }
    
    if(temp != null) {
        Node temp1 = temp.next;
        while(temp1 != null) {
            System.out.print(temp1.data+" ");
            temp1 = temp1.next;
        }
    }else {
        System.out.println("head node not found!");
    }
}

“如何使用单链表构建此数据结构”是指不需要指针“向下”并将所有元素存储到单链表中,并找出哪个节点是谁的子节点来描述二维链表?是的,链表中的每个节点都有一个关联的链表。A> B>C>D和A>1>2>3为什么不使用像LinkedHashMap这样的关联数组结构呢?因为有一些需要遵循的要求,所以只能使用链表来构建此数据结构:(请明确您必须使用的结构。这是上面对节点的定义(有
数据
下一个
下一个
成员)还是别的什么(只有
数据
下一个
成员)?我非常感谢您的帮助。