ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The concept is pretty simple. Define a struct for your link list node. A node comprises of data, which can be anything, and a pointer to another node typically called next.
When creating a link list node initially you want to set the next pointer to null and the data pointer to whatever you want it to be. The first node is typically called head, and you never want to have a situation where you don't have a pointer to this node anymore. I also like to always have a pointer to the end of the list I call tail. When creating the first node of a linked list I set both the head and tail pointer to it.
Say N1 is a pointer to a node.
N1->next = null;
head = N1;
tail= N1;
Now... to add a node to the beginning of the list you simply create the node, set the new node->next = head, and then set head = node;
N2->next = head;
head = N2;
our list now: head (aka N2)->next = tail( aka N1)->next = NULL
To add a node to the end of the list create the node, set tail->next equal to the node and move the tail pointer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.