LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2024, 11:51 AM   #1
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,002

Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
C++ linked-list traversal


I am aware of four methods for traversing a doubly-linked list in C++.

Code:
// plain c++

for (elem = list.next;
     elem != &list;
     elem = elem->next) {
}


// macro

FOREACH (elem, list) {
}


// STL

for (elem = list.begin ();
     elem != list.end ();
     ++elem) {
}


// c++11

for (auto& elem : list) {
}
I use the "plain c++" method because I have always used that. Consistency is my most important consideration. I am not feeling a need to change.

Which method do you use and why?
Ed
 
Old 01-27-2024, 12:30 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Simply:
Code:
pnext= first; 
while ((p= pnext)!=NULL) {
    pnext= p->next;
    ...
    process *p
    ...
}

Last edited by NevemTeve; 01-27-2024 at 12:37 PM.
 
Old 01-27-2024, 12:41 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,033

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
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.
 
Old 01-27-2024, 03:14 PM   #4
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,002

Original Poster
Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
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

Last edited by EdGr; 01-27-2024 at 06:28 PM.
 
Old 01-27-2024, 06:30 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
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.
 
Old 01-27-2024, 06:35 PM   #6
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,002

Original Poster
Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
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
 
Old 01-28-2024, 12:03 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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;
int found= 0;
for (!found && (pprev≈ p, p= pnext)!=NULL) {
    pnext= p->next;
    found= strcmp(newelem->key, p->key)<=0; /* insert before 'p' */
}

newelem->prev= pprev;
newelem->next= p;
if (pprev) pprev->next= newelem;
else       fist=        newelem;
if (p) p->prev= newelem;
else   last=    newelem;

Last edited by NevemTeve; 01-28-2024 at 12:09 AM.
 
Old 01-28-2024, 01:12 AM   #8
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,002

Original Poster
Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
My question is specifically about reading a list. IME, reads nearly always outnumber writes.
Ed
 
Old 01-28-2024, 08:29 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,688
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 01-28-2024, 10:02 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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.
 
Old 01-28-2024, 10:11 AM   #11
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,002

Original Poster
Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
sundialsvcs - I knew you would say that.

STL containers and iterators are fine but irrelevant to existing "plain c++" code.
Ed

Last edited by EdGr; 01-28-2024 at 10:18 AM.
 
Old 01-28-2024, 11:58 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,688
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.

Last edited by sundialsvcs; 01-28-2024 at 12:02 PM.
 
Old 01-28-2024, 05:54 PM   #13
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,002

Original Poster
Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
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
 
Old 01-28-2024, 06:51 PM   #14
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 496

Rep: Reputation: 182Reputation: 182
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;
   }

Last edited by rclark; 01-28-2024 at 07:30 PM.
 
Old 01-29-2024, 05:04 AM   #15
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,002

Original Poster
Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list traversal Deepak Inbasekaran Programming 3 05-16-2007 10:33 PM
Config Nat traversal on Mandrake 9.2 superfreeswan why1957 Mandriva 0 02-16-2004 11:08 PM
recursive directory traversal klfreese Linux - Newbie 2 08-20-2003 07:27 PM
Winxp linked to Linux Linked to home network OverboardKiller Linux - Networking 2 06-09-2003 09:59 AM
preventing directory traversal in programs tristan_vdv Linux - Security 4 06-04-2002 04:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration