微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

二维链表

如何解决二维链表

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 是链表,A 节点有 1>2>3 个链表。

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

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

解决方法

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

插入父节点:

1. add node at the end / down of parent node link. 

我们可以使用以下代码将父节点添加到链接的末尾。

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

例如,如果您有父节点列表{A,D},并且您想添加另一个父节点k,那么上面的代码在父节点链接的最后位置添加节点k

|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!");
    }
}

遍历整个列表:

  private static void traverse2DLinkedList(Object item) {
    Node parent = head;
    while(parent != null) {
        Node child = parent.next;
        
        while(child != null) {
            System.out.print(child.data+" ");
            child = child.next;
        }
        System.out.println();
        parent = parent.down;
    }
}
,

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

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

在这种情况下,你必须做类似的事情

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。