我定义了一个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];
}

但从下面的结果可以看出,蓝线与其他节点交叉。我的问题是如何避免这一点,或者移动节点1下面的蓝线,以避免边缘交叉?
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的边,最有可能的是它应该指向node1node1不应该指向任何地方。
最后,你的C实现-stHead也应该是一个指针。

10-07 13:00
查看更多