2015-04-28 82 views
1

我已經定義了一個list_t結構和list_node_t結構如下:如何在graphviz中繪製鏈表而不通過節點?

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

+0

這不是關於graphviz部分,而是在單個鏈表(list_node_t類型)中有兩個*元素,但它們之間沒有鏈接。無論我如何看待它,這看起來像一個分支結構。或者我完全錯過了什麼? – user1666959

回答

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也應該是一個指針。