Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在打印链表的时候结构不是很清晰 #57

Closed
Sclock opened this issue Mar 16, 2022 · 1 comment
Closed

在打印链表的时候结构不是很清晰 #57

Sclock opened this issue Mar 16, 2022 · 1 comment

Comments

@Sclock
Copy link

Sclock commented Mar 16, 2022

打印链表会输出

<Node 0x18136a4bd30
  .next = <Node 0x18136a4b3d0
    .next = <Node 0x18136dd5750
      .next = <Node 0x18136dd5930
        .next = <Node 0x18136dd5810
          .next = <Node 0x18136dd5870
            .next = None,
            .value = 'node_5'
          >,
          .value = 'node_4'
        >,
        .value = 'node_3'
      >,
      .value = 'node_2'
    >,
    .value = 'node_1'
  >,
  .value = 'Head'
>

可以考虑在打印属性为对象的属性的时候缩进幅度大一些

<Node 0x18136a4bd30
    .next = <Node 0x18136a4b3d0
            .next = <Node 0x18136dd5750
                    .next = <Node 0x18136dd5930
                                .next = <Node 0x18136dd5810
                                        .next = <Node 0x18136dd5870
                                                .next = None,
                                                .value = 'node_5'
                                        >,
                                        .value = 'node_4'
                                >,
                                .value = 'node_3'
                    >,
                    .value = 'node_2'
            >,
            .value = 'node_1'
     >,
     .value = 'Head'
>

或者结构修改一下

<Node 0x18136a4bd30 ->  .next = <Node 0x18136a4b3d0 ->  .next = <Node 0x18136dd5750 ->  .next = <Node 0x18136dd5930 ->  .next = <Node 0x18136dd5810 ->  .next = <Node 0x18136dd5870 ->  .next = None,
                                                                                                                                                                                        .value = 'node_5'
                                                                                                                                                                                        >,         
                                                                                                                                                        .value = 'node_4'
                                                                                                                                                        >,
                                                                                                                        .value = 'node_3'
                                                                                                                        >,
                                                                                        .value = 'node_2'
                                                                                        >,
                                                        .value = 'node_1'
                                                        >,
                        .value = 'Head'
                        >

简单的建议,希望能优化一下!

@gaogaotiantian
Copy link
Owner

调整缩进可以用indent,比如op(node, indent=4),就是你想要的第一个效果。也可以用config做全局的设置。

后面那个显示明显是有问题的,屏幕宽度是有限的,这几个node宽度就没了,如果你在terminal下运行,会自动换行,然后就会乱七八糟。一个更合理的链表显示应该是把它显示成数组的样子,每个node按顺序摆出来,用同一个缩进。

问题是,objprint无法知道你这个东西是不是一个链表,所以不可能智能识别做这个事情。肯定是要用户去单独说明,按照链表的形式打印,同时还要给出next指针是什么。

这不是不能做,可以是一个feature,但是这样的情况就挺多的,比如二维数组,也是很常见的可以以特殊形式打印的数据结构,甚至包括二叉树啥的。

对于这种特殊数据结构来说,用户可以考虑自己在外面wrap一个打印函数,然后用objstr之类的来做单个node的内容。

链表的事情我有想过,但是目前无论从优先级还是具体实现上,都谈不上价值比较高,所以我先close掉这个issue了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants