c - 如何在不跨节点的情况下在graphviz中绘制链表?

标签 c struct linked-list graphviz

我定义了一个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 .dot output

最佳答案

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

增加蓝色边缘的权重可提供最可接受的布局。

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

关于c - 如何在不跨节点的情况下在graphviz中绘制链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29922826/

相关文章:

c - 退出函数的 noreturn 属性是否必要?

c - 从函数返回链表的头

C: union 体中结构域的位置

c - C 中的堆没有释放

c++ - 在链表中插入 'n' 节点并打印其数据(C++)

java - 在Java中的LinkedList的开头插入一个新节点

检查 C 中 DOB 的条件

c - erlang nif 共享库上的 undefined symbol

java - 删除链表中的元素

c - 在C中连续每秒切换 bool 值