Currently, I have implemented a singly linked list, like so:
struct PeerNode {
struct Peer* cargo;
struct PeerNode* next;
};
...and I have a struct that contains a couple of these linked lists, like so:
struct Torrent {
...
struct PeerNode* peer_list;
struct PeerNode* unchoked_peers;
...
}
I would like to replace this by using the macros provided by sys/queue.h
. I gather that I could replace my code with something like this:
struct Torrent {
...
LIST_ENTRY(PeerNode, Peer) peer_list;
struct PeerNode* unchoked_peers;
...
}
Then, from looking at man queue
, I believe I would initialize the lists by doing something like this:
LIST_INIT(&peer_list);
LIST_INIT(unchoked_peers);
However, I don't understand how LIST_ENTRY
factors into usage of the list. From the man
page, it says: "The macro LIST_ENTRY
declares a structure that connects the elements in the list," but I don't really understand what this means.
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 sys/queue.h
? How would I insert an element into the list?
<sys/queue.h>
and it seems like the example link is no longer valid .. perhaps do you have an alternate Link ? – Kabob