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.
I don't use any, I can't recall when I used linked lists last time.
But anyway all of them are acceptable, there is no preferred way. Most probably I would pick the one which is already in use.
NevemTeve - I will count that method as "plain c++". I used to code list traversals with "while", but then I found that putting the list traversal inside a "for" construct was clearer. K&R's "for" loop design was ingenious.
pan64 - Yes, using the existing convention wins. The hurdle with the STL method is that old and new code alike has to use it.
Ed
I am curious how much adoption the STL and c++11 methods have. I have seen both in relatively new code, while newer parts of old programs continue to use "plain c++". I have seen the macro method only in C code.
Of course, all are workable and the differences are really minor.
Ed
Unless this is homework or you are simply curious, “this is the wrong way to do it.” C++ provides a rich abundance of so-called container classes in its standard libraries, and GitHub and SourceForge (among others …) provide even more.
Some of these classes use linked lists. But the key point is that they do it for you. If used properly, they are guaranteed to work and you never have to look under the hood. “Just drive the car.”
Last edited by sundialsvcs; 01-28-2024 at 08:30 AM.
That's mostly true. But what do you do if you have to maintain a linked list in a multi-process-shared memory? In such shared memries you cannot use pointers, you have to use offsets instead. Which means the predefined tools cannot be used, so either you know how to handle a linked list, or you have to ask a programmer for help.
Yes, there are certainly “edge cases” where STL containers can’t be used. And, there’s a fair amount of “C++ -ized” C-code out there. In the aforementioned edge-cases, you can usually find where someone else has written and then given-away what you need. (That’s why I referred to git-hub sites and C++ code sites.) Yes, you can certainly find multiprocess, shared-memory containers “off the shelf,” once you find the right shelf.
But it’s fairly pointless to engage in “C-style pointer twiddling” nearly all of the time, if the project began in that language. This isn’t to say that you won’t find production code out there that still uses null-terminated arrays as “strings.”
If you do have to do “malloc()” and “pointer-twiddling,” your code is not “++.” You do it just like ten thousand online examples show you to do, and you debug it as though it had never been written before. And sometimes that can’t be helped.
Last edited by sundialsvcs; 01-28-2024 at 12:02 PM.
I've always used in c, so of course in c++ do the same things. We learned a lot of different data structures back in college (probably the most useful computer class that was offered), so used extensively at work where needed. linked lists, double linked lists, binary trees, red-black, etc. Now kids don't have to know 'implementation'. Just here is a 'library' for lists, hash (dictionaries), binary tree, etc. Go use it.
Same as above:
Code:
node = nodeList;
while (node != NULL)
{
// process node
node = node->next;
}
And sorted insert is just as easy
Code:
node = nodeList;
prev = NULL;
while (node != NULL)
{
if (newNode->key > node->key) // say key is integer type
{
break;
}
prev = node;
node = node->next;
}
if (prev == NULL)
{
newNode->next = nodeList;
nodeList = newNode;
}
else
{
newNode->next = prev->next;
prev->next = newNode;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.