C++ linked-list traversal
I am aware of four methods for traversing a doubly-linked list in C++.
Code:
// plain c++ Which method do you use and why? Ed |
Simply:
Code:
pnext= first; |
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 personally agree with NevemTeve's form.
What's it matter though? Do what works best for you or what is also efficient and acceptable with peers if you happen to work with a team on projects. |
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 |
Let's pick a use case: we want to insert a new elem into a sorted list. I'm afraid neither of the listed method works.
Code:
Elem *p= NULL, pprev= NULL, pnext= first; |
My question is specifically about reading a list. IME, reads nearly always outnumber writes.
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.” |
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.
|
sundialsvcs - I knew you would say that.
STL containers and iterators are fine but irrelevant to existing "plain c++" code. Ed |
So, @EdGr, “my reputation precedes me.” :)
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. |
Okay, I counted three votes for "plain c++", one for "STL", and one with no preference.
I think our sample size is too small. ;) Ed |
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; Code:
|
Agreed, the data structures class was highly useful. The implementation of common data structures does not require much code.
4 votes for plain c++ 1 vote for STL 1 vote for no preference Ed |
All times are GMT -5. The time now is 02:50 AM. |