首页 > 解决方案 > Graphviz 中的循环列表?或如何弯曲边缘

问题描述

我一直在尝试在 Graphviz 中做我认为很简单的事情,如下所示:

在此处输入图像描述(取自https://tex.stackexchange.com/questions/394432/how-to-draw-circular-linked-list

我正在尝试用 Graphviz 做类似的事情,这就是它现在的样子:

在此处输入图像描述

我阅读了文档并已经尝试使用neato和circo图表但没有成功......我怎样才能以我需要的方式弯曲边缘?我现在可以尝试使用坐标,p3:e -> p1:w但这会从记录的中间删除起点。

到目前为止,这是我的代码:

digraph {
  node[shape=record];
  graph[pencolor=transparent];
  rankdir=LR;
  p1[label="{<data> 12|<next>}"];
  p2[label="{<data> 99|<next>}"];
  p3[label="{<data> 37|<next>}"];

  edge[tailclip=false,arrowtail=dot,dir=both];

  p1:next:c -> p2:data;
  p2:next:c -> p3:data;
  p3:next:c -> p1:data[constraint=false];
}

标签: graphviz

解决方案


你需要一些辅助节点(p0 和 p4)

digraph {
  node[shape=record];
  graph[pencolor=transparent];
  rankdir=LR;
  p1[label="{<data> 12|<next>}"];
  p2[label="{<data> 99|<next>}"];
  p3[label="{<data> 37|<next>}"];

  edge[tailclip=false,arrowtail=dot,dir=both];

  {node[shape=point height=0] p0 p4} // make p0 and p4 to small to see
  p0:n -> p1[arrowtail=none]
  p0:s -> p4:s[dir=none] // add edge with no arrow to make it look like one long edge, also make p0 the tail so we don't have a recursition that may course dot to rearange the nodes
  p1:next:c -> p2:data;
  p2:next:c -> p3:data;
  //p3:next:c -> p1:data[constraint=false];
  p3:next:c -> p4:n[arrowhead=none]
}

在此处输入图像描述

您可能可以通过使用 html 表格标签并将图像添加到其中一个单元格来做尾部交叉


推荐阅读