【问题标题】:How to draw linked-list in graphviz without cross the node?如何在graphviz中绘制链表而不跨越节点?
【发布时间】:2015-04-28 14:59:14
【问题描述】:

我定义了一个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 结果:

【问题讨论】:

  • 这与graphviz 部分无关,但您的单链表(list_node_t 类型)中有两个 元素,但它们之间没有链接。不管我怎么看,这看起来像是一个分支结构。还是我完全错过了什么???

标签: c struct linked-list graphviz


【解决方案1】:

这个问题基本上没有解决办法。您可以使用参数来优化具体布局,但不是一般情况。

增加蓝色边缘的权重可以获得最可接受的布局。

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 也应该是一个指针。

【讨论】:

    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2018-11-22
    • 2011-04-19
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多