Linked lists...god help us all

This is a discussion on Linked lists...god help us all within the C++ Programming forums, part of the General Programming Boards category; Hey everyone...
So, I just spent the past hour tooling around with this code...the concept of Linked lists isnt clicking ...

Linked lists...god help us all

Hey everyone...
So, I just spent the past hour tooling around with this code...the concept of Linked lists isnt clicking in my head for some reason (I'm sure it has NOTHING to do with the size of my brain.... ) ANYWAYS! So using the tutorial from this website along with a couple of books of mine, I've been trying to piece together a program that creates a SIMPLE linked list...but I'm not exactly sure what I'm doing...the following code doesnt compile with an error of "left operand must be l-value" in reference to the commented line:

Basically what I'm trying to do is create multiple nodes...how do I go about doing this...the tutorial definitely pushed me a GIANT step forward (I cant emphasize how much it helped me progress) but I seem to be missing something...any insight would be very much appreciated...thanks! -Chap

edit: Let me give some reason as to why you should use the class. A linked list is a fancy container for your data, so having it as its own class will allow you to grasp the concept better. It will also make it easier for you to impliment. By the way, what part of the concept of a linked list isn't clicking?

Hmm...so one thing I dont understand (I guess this will take us momentarily out of the realm of linked lists and into general syntax) is if the function GetNext() returns a memory address, shouldn't I be able to allocate memory on the heap with the function call? OR hmm...interesting...ok, tell me if this sounds like I know what I'm talking about: so, m_pNext in GetNext() is temporary but the address it points to is persistent through function calls b/c it's on the heap...so if I take my existing code and create a new variable that's a pointer to a node and call it pTemp and initialize it as so:

Code:

node* pTemp = pConductor->GetNext()

That should do what I want for the reasons I just said? I'm gonna try it out and then I'm going to take your suggestion, skorman and make 2 diff classes... and Jverk...I'm going to print out the amazing picture you made and tape it to my laptop haha...seriously

Last edited by Chaplin27; 02-09-2005 at 08:33 AM.
Reason: screwed up the code tags hehe

>>a function that returns a reference to a variable can be used as a LS value.
Yes, it can, but your function doesn't

Code:

(node*)& SetNext()
{ return m_pNext; }

However, I feel that it is poor programming practice to do so, because it isn't usually done and therefore the user won't be expecting it to act that way. Instead, SetNext() should be:

Code:

void SetNext(node* newNext)
{ m_pNext = newNext; }

This way, it is absolutely clear what the function does.

**EDIT**
Another reason you shouldn't have public accessors returning references to member variables is simply because it is equivalent to making the variable public, just uglier and harder to read/use.

Hey skorman...your response was very informative...I get stuck in the wrong mentality sometimes with pointers and lose sight of even simple syntax rules...of course you'd never assign a value to a function call, even if the assigned value is the same as the return value of the function...thanks a lot for the insight and the example really drove the point home, this is one of those things that will stick with me for the rest of my soon-t-be programing career...I'm going to sit down for a few hours with linked lists, I'm starting to feel a little more comfortable with them (slightly) and all the help you've given me will definitely bring me closer to understanding LL's. Thanks!-Chap

Hey, so 2 things...1- What do you do for a living Kaelin? I'm just curious...there's no weirdness behind the question :-P hehe...and 2- Is it possible to randomly access specific nodes in a linked list or do you have to start at the beginning of the list...and if that's the case...how would you access a certain node with this method:

I think you have to draw diagrams to understand them. Draw a big box that represents the linked list and list the members of the linked list along the top of the box. For the nodes, draw boxes inside the big box and list the node members in the node box. Then, go through the code and draw arrows as the pointers are assigned to see what is happening. I recommend you use a pencil.

I didnt use any tags...just typed it out...I think I have the concept of linked lists down now. I spent about 2 hours sending all kinds of experiments to cout and seeing exactly what was doing what and when and it's making A LOT more sense...thanks for all the helpful posts (even the barrel of monkeys ) haha...thanks, Chap

HAHAHA Quzah...funniest damn thing I've ever seen! I followed your post link to the OTHER post link and got directed to the barrel of monkeys webpage hahaha...it had me on the floor...a perfect end to the difficult conquest that was linked lists!