title: How to use list from sys/queue.h?
body: <p>Currently, I have implemented a singly linked list, like so:</p>
  
  <pre><code>struct PeerNode {
       struct Peer* cargo;
       struct PeerNode* next;
  };
  </code></pre>
  
  <p>...and I have a struct that contains a couple of these linked lists, like so:</p>
  
  <pre><code>struct Torrent {
       ...
       struct PeerNode* peer_list;
       struct PeerNode* unchoked_peers;
       ...
  }
  </code></pre>
  
  <p>I would like to replace this by using the macros provided by <code>sys/queue.h</code>. I gather that I could replace my code with something like this:</p>
  
  <pre><code>struct Torrent {
       ...
       LIST_ENTRY(PeerNode, Peer) peer_list;
       struct PeerNode* unchoked_peers;
       ...
  }
  </code></pre>
  
  <p>Then, from looking at <code>man queue</code>, I believe I would initialize the lists by doing something like this:</p>
  
  <pre><code>LIST_INIT(&amp;peer_list);
  LIST_INIT(unchoked_peers);
  </code></pre>
  
  <p>However, I don't understand how <code>LIST_ENTRY</code> factors into usage of the list. From the <code>man</code> page, it says: "The macro <code>LIST_ENTRY</code> declares a structure that connects the elements in the list," but I don't really understand what this means. </p>
  
  <p>Why would I want to declare a structure to connect the elements in the list? Shouldn't each node be connected to the next node via a pointer, like my initial linked list implementation? How would I replace my linked lists with the implementation provided by <code>sys/queue.h</code>? How would I insert an element into the list?</p>