我定义了一个list_t
结构和一个list_node_t
结构如下:
typedef struct taglist_node_t {
struct taglist_node_t *pstNext;
void *pData;
} list_node_t;
typedef struct taglist_t {
list_node_t stHead;
list_node_t *pstTail;
unsigned int uiCount;
} list_t;
我决定使用 graphviz 绘制上面的链表,代码如下:
digraph g {
bgcolor="#BBCAF2";
label="\nSingle Linked List\n";
graph[rankdir=LR, center=true, margin=0.2, nodesep=1, ranksep=1]
edge[arrowsize=1.0, arrowhead=vee]
node[shape = Mrecord, fontname="Consolas", fontsize=20, width=1, height=1, fixedsize=false];
list[label = "<name> slist_t | <head> stHead | <tail> *pstTail | uiCount"];
node0[label = "<name> list_node_t | <next> *pstNext | *pData"];
node1[label = "<name> list_node_t | <next> *pstNext | *pData"];
head[label = "pstList"];
head -> list:name[style=bold, color=red, dir=both, arrowtail=dot];
list:head:e -> node0:name[dir=forward, arrowtail=normal];
list:tail:e -> node1:name[dir=both, arrowtail=dot];
node0:next:e -> list:head:w[dir=both, arrowtail=dot];
node1:next:e -> list:head:w[dir=both, arrowtail=dot, color=blue];
}
但是从下面的结果可以看出,蓝色线穿过其他节点。我的问题是如何避免这种情况或将蓝线移动到 node1 下方以避免边缘交叉?
GraphViz 结果:
最佳答案
这个问题基本上没有解决办法。您可以使用参数来优化具体布局,但一般情况下不行。
增加蓝色边缘的权重可提供最可接受的布局。
digraph g {
bgcolor="#BBCAF2";
label="\nSingle Linked List\n";
graph[rankdir=LR, center=true, margin=0.2, nodesep=1, ranksep=1]
edge[arrowsize=1.0, arrowhead=vee]
node[shape = Mrecord, fontname="Consolas", fontsize=20, width=1, height=1, fixedsize=false];
list[label = "<name> slist_t | <head> stHead | <tail> *pstTail | uiCount"];
node0[label = "<name> list_node_t | <next> *pstNext | *pData"];
node1[label = "<name> list_node_t | <next> *pstNext | *pData"];
head[label = "pstList"];
head -> list:name[style=bold, color=red, dir=both, arrowtail=dot];
list:head:e -> node0:name[dir=forward, arrowtail=normal];
list:tail:e -> node1:name[dir=both, arrowtail=dot];
node0:next:e -> list:head:w[dir=both, arrowtail=dot];
node1:next:e -> list:head:w[dir=both, arrowtail=dot, color=blue, weight=10];
}
但是 node0
无论如何都不应该有 list
的边,它很可能应该指向 node1
。 node1
不应指向任何地方。
最后是您的 C 实现 - stHead
也应该是一个指针。
关于c - 如何在不跨节点的情况下在graphviz中绘制链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29922826/