Thread: Linked lists...god help us all

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    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:
    Code:
    #include <iostream>
    using namespace std;
    
    class node
    {
    public:
       node(int age = 0):m_age(age), m_pNext(0) {}
       int GetAge() const {return m_age;}
       node* GetNext() const {return m_pNext;}
       void SetNext(node* next) {m_pNext = next;}
    private"
       int m_age;
       node* m_pNext;
    };
    
    int main()
    {
       node* pHead = new node(5);
       node* pConductor = pHead;
       if (pConductor != 0)
       {
            while (pConductor->GetNext() != 0)
                   pConductor = pConductor->GetNext();
       }
       pConductor->GetNext() = new node(9); //this is the error
       return 0;  //program ends abruptly...I know
    }
    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

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    pConductor->GetNext() is not an lvalue...which is a fancy way of saying it's not a variable. What you CAN do is instead send back a node** , store that in a temp variable and then:
    Code:
    *temp = new node(9);
    but you don't want to do that. Instead, you should make your Linked List a class of its own that uses your node class.

    Code:
    class LinkedList
    {
       public:
         AddNode(int num)
         {
             //this should be enough for you, but don't hesitate to ask some more questions
         }
        private:
          node * pHead;
    };
    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?
    Last edited by skorman00; 02-08-2005 at 11:28 PM.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Wheee, time to draw a sweet paint picture!

    *opens paint and starts doodling*

    -see the picture below for reference to these code blocks....and the first block says Head Node...it's kinda unreadable-

    Stage 1:
    Code:
    struct node
    {
        int id;
        node* next;
    };
    
    int main()
    {
        node* head=new node();
    
        return 0;
    }
    head is just a single node, not pointing to anything


    Stage 2:
    Code:
    struct node
    {
        int id;
        node* next;
    };
    
    int main()
    {
        node* head=new node();
        head->next=new node();
    
        return 0;
    }
    Now we have what is shown in Stage 2 of the image.

    And now....we can do the same thing with Stage 3:
    Code:
    struct node
    {
        int id;
        node* next;
    };
    
    int main()
    {
        node* head=new node();
        head->next=new node();
        node* nextNode=head->next;
        nextNode->next=new node();
    
        return 0;
    }


    And the problem with your code is that you can not assign a value to a function. Take the return value from the function and store it to a variable, and THEN assign the value.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    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 09:33 AM. Reason: screwed up the code tags hehe

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    You can have this line:
    Code:
    node* pTemp = pConductor->GetNext()
    but this does not work:
    Code:
    node * pTemp = NULL;
    pConductor->GetNext() = pTemp;
    Let's take pointers out of the picture for a sec in order to explain this a bit easier.

    Code:
    int Foo()
    {
       int i = 0;
       return i;
    }
    
    int main()
    {
    
        Foo() = 5;
    }
    If this were legal code, what would this do? Foo returns a number, 0. Said and done. is 0 = 5 a legal statement? Even if the return is variable, it comes down to the same situation

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    You could always do something like this

    Code:
    node* SetNext() {return &m_pNext;}
    then you could use this statement:

    Code:
    pConductor->SetNext() = new node(9);
    I'm fairly certain this should work - a function that returns a reference to a variable can be used as a LS value.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I'm fairly certain this should work
    Close.

    >>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.
    Last edited by Hunter2; 02-09-2005 at 11:50 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    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

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    The price I pay for replying at work - sometimes I need a smack in the face from either a compiler error or some erroneous run-time behaviour to wake me from my braindead moments

    Also, I agree. It is certainly not a good way to write code. It is fairly neat though - I'll do that every now and then in simple programs that are just for me just because

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    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:

    NODE 1 (head) -> NODE 2 -> NODE 3 -> NODE 4

    What if I want to only access NODE 3? Thanks again!- Chap

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What tags did you use to imbed that image?

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    the concept of Linked lists isnt clicking in my head for some reason
    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.
    Last edited by 7stud; 02-09-2005 at 05:57 PM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    Quote Originally Posted by 7stud
    What tags did you use to imbed that image?

    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!
    Last edited by Chaplin27; 02-09-2005 at 09:35 PM. Reason: etra stuff

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What tags did you use to imbed that image?
    I didnt use any tags...just typed it out.
    errr...just typed out an image? You must be pretty handy with the keyboard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM