Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:26:45+00:00 2026-05-25T21:26:45+00:00

Currently, I have implemented a singly linked list, like so: struct PeerNode { struct

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:26:46+00:00Added an answer on May 25, 2026 at 9:26 pm

    LIST_ENTRY creates fields to put into your structure that are suitable for linking the elements, so you do not have to concern yourself with the specifics of those pointers.

    struct foo {
        int a, b, c;
        /* This is instead of "struct foo *next" */
        LIST_ENTRY(foo) pointers;
    };
    

    To then create a list you’d use LIST_HEAD():

    struct Torrent {
        LIST_HEAD(foo_list, foo) bar;
    };
    

    You can initialise the list header using LIST_INIT():

    struct Torrent t;
    LIST_INIT(&t.bar);
    

    You can insert elements using the LIST_INSERT_*() macros:

    struct foo *item = malloc(sizeof(struct foo));
    LIST_INSERT_HEAD(&t.bar, item, pointers);
    

    This was all taken from the list example in the man pages at http://www.manpagez.com/man/3/queue/

    For a full example:
    http://infnis.wikidot.com/list-from-sys-queue-h

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have implemented an overlay item that shows an icon for Geo-point on
Backgroup: We are looking at SAS BI Dashboard. We have currently implemented almost all
I've been trying to implement unit testing and currently have some code that does
Currently I have a ListView (using the Details View). I would like to implement
I'm currently writing a class that implements the SeekableIterator interface and have run into
I currently have an MS Access application that connects to a PostgreSQL database via
Going through classic data structures and have stopped on linked lists.Just implemented a circular
We currently have implemented a lot of SWT Actions, because it is a nice
Currently we have a project to implement an Internet Banking site, and we are
I currently have speakers set up both in my office and in my living

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.