How are linked list possible?

This is a discussion on How are linked list possible? within the C Programming forums, part of the General Programming Boards category; ive been trying to learn about linked list and so far i have gotten no where slowly. what i can ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    How are linked list possible?

    ive been trying to learn about linked list and so far i have gotten no where slowly. what i can actually find (thats in c and that compiles) isnt that useful.

    how is a linked possible? it sound like you creating a new struct with the exact same name, how can that be?

    does any one know of a good howto on this stuff, one with commensts?

    sorry for asking a "well document" question, but ive only found complete crap so far (and c++ code, if theres a difference )

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I believe this site has a tutoral. A linked list is built from objects that have a pointer to their same type as one [or more] of their data members. This pointer is used to point to another member of the same type, forming in effect, links of a chain.
    Code:
    struct node
    {
        struct node *next;
        data_type data_name;
    }
    You build a list by first creating a member of this type, then make another, and have it either point to the first, have the first point to it, or both. It's best to provide an anchor point for your list, and to have this either always point to the first, or the last element in your list. Consider the following:
    Code:
    struct node *list;
    struct ndoe *ptr;
    
    ptr = malloc( sizeof struct node );
    ptr->next = NULL;
    
    list = ptr;
    Here, we make the list anchor point to the new (first) element of our list. We make that object's 'next' pointer not point to anything. This is an easy way to tell if you are at the end of your list.

    To continue, we build onto our list, by prepending to it.
    Code:
    ptr = malloc( sizeof struct node );
    ptr->next = list; 
    list = ptr;
    Here, we first allocate a new list. Remember now that as per above, the 'list' pointer points to the first element we made.

    Next, we make the new node we just allocated's 'next' pointer point to the top of the list.

    Finally, we update the list pointer so that it points to the new node.

    This is one way to build a single linked list.

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

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, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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