def test_linking_nodes(self):
     node1 = Node('A')
     node2 = Node('B')
     node3 = Node('C')
     # Link nodes together
     node1.next = node2
     node1.prev = None
     node2.next = node3
     node2.prev = node1
     node3.prev = node2
     node3.next = None
     # Node links should be transitive
     assert node1.prev is None
     assert node1.next is node2  # One link
     assert node2.prev is node1
     assert node1.next.next is node3  # Two links
     assert node3.prev is node2
     assert node3.next is None
        def FlattenNode(pre: Node, cur: Node) -> Node:
            if not cur:
                return pre

            pre.next = cur
            cur.prev = pre

            tmpnode = cur.next

            childnode = FlattenNode(cur, cur.child)
            cur.child = None

            return FlattenNode(childnode, tmpnode)
Пример #3
0
def test_node_attributes():
    head = Node(1)
    assert head.prev is None
    assert head.next is None

    previous = Node(0)
    head.prev = previous
    assert head.prev is previous
    assert head.next is None
    assert head.data == 1
    assert head.prev.prev is None
    assert head.prev.data == 0
    assert repr(previous) == "Node(0)"

    next = Node(2)
    head.next = next
    assert head.prev is previous
    assert head.next is next
    assert head.data == 1
    assert head.next.next == None
    assert head.prev.prev == None
    assert head.next.data == 2
    assert repr(next) == "Node(2)"