Linked List

This is a discussion on Linked List within the C++ Programming forums, part of the General Programming Boards category; I understand the concept of the linked list but do not actually understand how it works. For instance, I get ...

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136

    Linked List

    I understand the concept of the linked list but do not actually understand how it works. For instance, I get how each node points to the one after it. However, I am not sure why it doesn't create a node with the same name every time. An explanation would be greatly appreciated. Thanx.

    P.S. If I am totally wrong with the concept of it, explain it to me please.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > However, I am not sure why it doesn't create a node with the
    > same name every time. An explanation would be greatly
    > appreciated

    Same 'name'? Not sure the question:

    node *n;

    n = new( node );
    n->next = new( node );
    n->next->next = new( node );

    All we're doing is allocating a block of memory the size of a 'node' and pointing at it. It has no name. It's "name" is the name of whatever variable you use to point at that block:

    node *someName;

    someName = n->next->next;

    someName now points to a block of memory we allocated, which happens to be the third item in our list now.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    o, so the second and third and fourth node do not actually have a name such a the head node with a title. for instance:

    --> means that it points too

    head node -->second node (next node)--> third node (next next node from first node)


    basically, the third node will not a unique name like third and only the head node will have a unique name like first or whatever??

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Unique name? I guess, you mean a name for each node? The linked list is treated as a datastructure on it self. Though you could put an identifier in the node-structure which identifies the node:

    typedef struct node *pnode;
    struct node
    {
    int id;
    pnode next;
    }

    The list could be defined as:

    pnode list;

    That pointer points to the start of the list and shouldn't be changed. Since then you lose information. If you do:

    list = list->next;

    Then you can never go to the start of the list. In order to walk through the list you should define another pointer which points to an element of the list.

    pnode list_ptr;

    Let this pointer point to the start of the list:

    list_ptr = list;

    And walk through the list by:

    while (list_ptr != NULL)
    {
    list_ptr = list_ptr->next;
    }

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136

    Thanx

    Thanx everyone, I got it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21