首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用graphviz绘制双链接列表

使用graphviz绘制双链接列表
EN

Stack Overflow用户
提问于 2021-12-21 21:28:41
回答 2查看 249关注 0票数 0

我使用Graphviz (点)绘制了一个双链接列表,如下所示,但是节点没有对齐。如何对齐节点?

代码语言:javascript
运行
复制
digraph "Doubly Linked List" {
        rankdir=LR;
        node [shape=record];
        e [label="nil" shape=circle];
        a [label="{ <ref1> | <data> 1 | <ref2>  }"]
        b [label="{ <ref1> | <data> 5 | <ref2>  }"];
        c [label="{ <ref1> | <data> 7 | <ref2>  }"];
        d [label="nil" shape=circle];
        e -> a:ref1:c      [arrowhead=dot, arrowtail=vee, dir=both, headclip=false];
        a:ref2:c -> b:data:n [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        b:ref2:c -> c:data:n [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        c:ref2:c -> d      [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        c:ref1:c -> b:data:s [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        b:ref1:c -> a:data:s [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
}

EN

回答 2

Stack Overflow用户

发布于 2021-12-22 00:07:26

在高层次上,答案是{rank=same ... nodes here ... }

但是,记录形状的节点和排序相同的边缘会导致点心灼伤,或者更准确地产生以下错误消息:

代码语言:javascript
运行
复制
Warning: flat edge between adjacent nodes one of which has a record shape - replace records with HTML-like labels
  Edge e -> a
Error: lost e a edge
Error: lost a b edge
Error: lost b a edge
Error: lost b c edge
Error: lost c b edge
Error: lost c d edge

因此,如果我们将节点更改为“html样”形状(https://graphviz.org/doc/info/shapes.html#href),您将得到:

代码语言:javascript
运行
复制
digraph "Doubly Linked List" {
        node [shape=plaintext]  // for correct display of table
    {rank=same              // all on same rank
        e [label="nil" shape=circle];
        a [label=<<table border="0" cellspacing="0" cellborder="1"><tr>
          <td port="ref1" width="28" height="36" fixedsize="true"></td>
      <td port="data" width="28" height="36" fixedsize="true">1</td>
      <td port="ref2" width="28" height="36" fixedsize="true"></td>
          </tr></table>>]

        b [label=<<table border="0" cellspacing="0" cellborder="1"><tr>
      <td port="ref1" width="28" height="36" fixedsize="true"></td>
      <td port="data" width="28" height="36" fixedsize="true">5</td>
      <td port="ref2" width="28" height="36" fixedsize="true"></td></tr>
          </table>>]

        c [label=<<table border="0" cellspacing="0" cellborder="1"><tr>
      <td port="ref1" width="28" height="36" fixedsize="true"></td>
      <td port="data" width="28" height="36" fixedsize="true">7</td>
      <td port="ref2" width="28" height="36" fixedsize="true"></td>
          </tr></table>>]

        d [label="nil" shape=circle];
    }
        e -> a:ref1:c        [arrowhead=dot, arrowtail=vee, dir=both, headclip=false];
        a:ref2:c -> b:data:n [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        b:ref2:c -> c:data:n [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        // added :w to straighten edge
        c:ref2:c -> d:w        [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        c:ref1:c -> b:data:s [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        b:ref1:c -> a:data:s [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
}

(对不起)但它给了你:

票数 1
EN

Stack Overflow用户

发布于 2021-12-22 01:20:54

Sroush提供了一种类似html形状的解决方案,但我找到了一种使用记录节点here的方法,如下所示:

代码语言:javascript
运行
复制
digraph "Doubly Linked List" {
        rankdir=LR;
        node [shape=record];
        e [label="nil" shape=circle];
        a [label="{ <ref1> | <data> 1 | <ref2>  }"]
        b [label="{ <ref1> | <data> 5 | <ref2>  }"];
        c [label="{ <ref1> | <data> 7 | <ref2>  }"];
        d [label="nil" shape=circle];
        e -> a:ref1:c      [arrowhead=dot, arrowtail=vee, dir=both, headclip=false];
        a:ref2:c -> b:data:n [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        b:ref2:c -> c:data:n [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        c:ref2:c -> d      [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        c:ref1:c -> b:data:s [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        c:ref1:c -> b:data:w[weight = 100, style = invis]; <- add this
        b:ref1:c -> a:data:s [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        b:ref1:c -> a:data:w[weight = 100, style = invis]; <- add this
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70441786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档