CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    20

    Unhappy PLEASE help explain linked lists code to me!

    Hello... at the moment I am EXTREMELY confused with how linked lists work. I understand everything conceptually, but I just don't get how to code them into a simple program at all...

    Our professor gives us this code to work with for some sample code, and I was wondering if for starters someone could help me decypher what all this code does (meaning, what does it do in and of itself, but also what the heck does it do in the grand scheme of things for linked lists?):

    struct Node{
    int data;
    Node *next;
    Node(): data(0), next(NULL) {}
    Node(int d) : data(d), next(NULL) {}
    Node(int d, Node *n): data(d), next(n){}
    };

    In the lecture notes, immediately after showing this code, she mentions some example calls as being the following:

    Node *p = new Node(3);
    Node *q = new Node(5, p);
    Node *r = new Node(12, NULL);

    Of course, since I don't understand the former code, I have no clue what this means, as well! I'd appreciate any and all help in explaining what's goin on with this. Please note that I've tried googling help on linked lists a couple times and the webpages confused me even more; please keep it simple for me, I'm slow at this!

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: PLEASE help explain linked lists code to me!

    Have you read the wiki about linked lists ?

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: PLEASE help explain linked lists code to me!

    The first part is simply defining the type that will be the backbone of a linked list: A Node. Each Node object carries some payload (an int in this case), and a pointer to the next Node in the list. If that pointer is NULL, then it is the last Node in a list. The class has three constructors.

    The second part creates two linked lists. The first one is 5 -> 3 -> x, the second is 12 -> x, where x indicates list end or the NULL pointer. These two lists are pointed to by q and r respectively.

  4. #4
    Join Date
    Feb 2009
    Posts
    42

    Re: PLEASE help explain linked lists code to me!

    Linked lists are very easy to understand:

    You have
    class Node
    {
    Node* parent;
    Node* child;
    };

    consider it as:

    class Human
    {
    Human *leftHand;
    Human *rightHand;
    };

    You have four people holding hand
    Man A with his left hand hold nobody's hand;
    Man A with his right hand holds man's B left hand
    meaning that B hold's with his left hand A's right hand .
    B holds with right hand C's left hand etc...
    Then you will have
    Human *A, *B, *C;
    A->leftHand = NULL; // holding nobodys hand
    A->rightHand = B; // A right hand holds B hand
    B->leftHand = A; // B left hand holds A hand
    B->rightHand = C; // B right hand holds C hand
    C->leftHand = B; // C holds with his left hand B's hand

    and they are linked by hands...

    meaning in node example
    root Node ( first node don't have parent ) // holding nobodys hand
    but its child member hold another node // A right hand holds B hand
    ant that's child parent member is actually previous node // B left hand holds A hand

    Node *A, *B, *C;
    A->parent = 0;

    A->child = B; B->parent = A;

    B->child = C; C->child = B;

    get it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured