Thread: linked list

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

    linked list

    What is linked list. Can I have some example?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A linked list is a series of objects that point to the next item in the list (or to the previous and next, in the case of a double linked list).

    Code:
    struct mystruct {
        struct mystruct *next;
        ... other stuff here...
    };
    This is the building block of a single linked list. Create a pointer to a structure:

    struct mystruct *list;

    Allocate space for the first node, using malloc:

    list = malloc( sizeof( struct mystruct ) );

    Assign the other elements values.
    Assign the 'next' pointer either NULL, or the value of the next element.

    list->next = newNode( );

    This assumes that we have a function called 'newNode( )' which returns a pointer to a freshly allocated structure of the same type.

    This would in effect add one on to the next end of 'list'.

    Visualize a linked list as:

    list -> list1 -> list2 -> list3 -> etc...

    Where you have a list place holder which always holds the "top of the list", and you then prepend everything onto it.

    ptr = list;
    list = newNode( );
    list->next = ptr;

    Here we've prepended a new node on to the top of the list. If you do this, and the very first node you've ever created's "next" element pointer is NULL, you can then easily cycle through the list untill you reach the end by looking for null:

    for( ptr = list; ptr != NULL; ptr = ptr->next )

    This will run through the entire list.

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

  3. #3
    Master Horsecock
    Guest

    Re: linked list

    Originally posted by kewell
    What is linked list. Can I have some example?
    Be more specific.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Blimey Quzah, that's a rather full answer for such a crappy question.

    Kewell, try a little research before asking a question... there's a 1001 sites out there that give good detail on linked lists. Google knows them all

    To get you started, google found me this one.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can I have some example?
    Here's an example. It works, but whether or not it's a good example of a linked list is left to you as an exercise.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct list_single_node *link;
    typedef struct list_full_list llist;
    
    struct list_single_node
    {
      int item;
      link next;
    };
    
    struct list_full_list
    {
      link root;
      link curr;
      int count;
    };
    
    static void li_Init ( llist *l )
    {
      l->root = l->curr = NULL;
      l->count = 0;
    }
    
    static void li_Add_End ( llist *l, int val )
    {
      if ( l->root == NULL ) {
        l->curr = l->root = malloc ( sizeof *l->curr );
        if ( l->curr != NULL ) {
          l->curr->item = val;
          l->curr->next = NULL;
          l->count++;
        }
      }
      else {
        l->curr->next = malloc ( sizeof *l->curr );
        if ( l->curr->next != NULL ) {
          l->curr = l->curr->next;
          l->curr->item = val;
          l->curr->next = NULL;
          l->count++;
        }
      }
    }
    
    static void li_Print_List ( llist *l )
    {
      l->curr = l->root;
      while ( l->curr != NULL ) {
        printf ( "%d%s", l->curr->item, 
          l->curr->next != NULL ? "->" : "\n" );
        l->curr = l->curr->next;
      }
    }
    
    static void li_Kill_List ( llist *l )
    {
      link run;
      l->curr = l->root;
      while ( l->curr != NULL ) {
        run = l->curr->next;
        free ( l->curr );
        l->curr = run;
      }
      l->count = 0;
    }
    
    int main ( void )
    {
      llist list;
      li_Init ( &list );
      while ( list.count < 10 )
        li_Add_End ( &list, list.count );
      li_Print_List ( &list );
      li_Kill_List ( &list );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    Thanks

    THANK YOU VERY MUCH FOR THOSE WHO GIVE ME THE BIG HELP!!!

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, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM