安装
pip install PrettyPrintTree
具体实现
from PrettyPrint import PrettyPrintLinkedList
class Node:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# class Node:
# def __init__(self, val):
# self.val = val
# self.next_node = None
# self.prev_node = None
def list2link(lst):
head = Node(lst[0])
p = head
for i in range(1, len(lst)):
p.next = Node(lst[i])
p = p.next
return head
def link2list(head):
p = head
lst = []
while p:
lst.append(p.val)
p = p.next
return lst
lst = [1,2,6,3,4,5,6]
head = list2link(lst)
pt = PrettyPrintLinkedList(
lambda x: x.val,
lambda x: x.next,
orientation=PrettyPrintLinkedList.Horizontal,
)
# pt = PrettyPrintLinkedList(
# lambda x: x.val,
# lambda x: x.next_node,
# lambda x: x.prev_node,
# orientation=PrettyPrintLinkedList.Horizontal,
# )
pt(head)