Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, so this is called a directed graph. And there are about a thousand ways you could implement it. The "right" way depends entirely on how you will use it, which you haven't described. Since you did seem to limit this to linked lists or doubly linked lists I'll just use nothing but doubly linked lists.</p> <p>Forward declare your data types</p> <pre><code>typedef struct ItemInfo_s ItemInfo; typedef struct DoubleLinkedListNode_s DoubleLinkedListNode; </code></pre> <p>Create a ListNode like you always do:</p> <pre><code>struct DoubleLinkedListNode_s { DoubleLinkedListNode *next; DoubleLinkedListNode *prev; ItemInfo *data; }; </code></pre> <p>Then create your ItemInfo:</p> <pre><code>struct ItemInfo_s { DoubleLinkedListNode *children; DoubleLinkedListNode *parents; ... /* other item data */ }; </code></pre> <p>Also, for sanity's sake create a list of all created nodes:</p> <pre><code>DoubleLinkedListNode *items; </code></pre> <p>Now, I'm not going to write all of the linked list management functions, but I'm sure you can figure it out. By convention I'll write (B) as a node pointing to item B (node.data = &amp;B). I'll also indicate any two nodes linked together with an '=', and a '-' as an unlinked (null valued) node linkage. I'll write a chain of elements [ -(1)=(2)=(3)- ] and by convention pointers into a chain of items will always point to the first node in the chain (the (1) in this example). Your given graph looks like this in memory:</p> <pre><code>items = [ -(A)=(B)=(C)=(E)=(F)=(G)=(I)=(J)=(K)- ] A.children = [ -(B)=(C)- ] A.parents = [] B.children = [ -(E)- ] B.parents = [ -(A)- ] C.children = [ -(E)=(G)- ] C.parents = [ -(A)- ] E.children = [ -(I)=(F)- ] E.parents = [ -(B)=(C)- ] F.children = [ -(J)- ] F.parents = [ -(E)- ] G.children = [ -(K)- ] G.parents = [ -(C)- ] I.children = [] I.parents = [ -(E)- ] J.children = [] J.parents = [ -(F)- ] K.children = [] K.parents = [ -(G)- ] </code></pre> <p>In total that is 9 ItemInfos and 27 DoubleLinkedListNodes. I can think of almost no reason I would ever implement this in practice, but it's implemented only using double linked lists. It might make the list management easier to do doubly linked rings (connecting the head and tail of the list together) but that's harder to show in text form. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload