understand lnked list

This is a discussion on understand lnked list within the C Programming forums, part of the General Programming Boards category; Hi can someone explain me this function Code: struct Node{ char name[39]; sruct Node *next Ptr;} typedef struct Node Node; ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    39

    understand lnked list

    Hi can someone explain me this function
    Code:
    struct Node{
    char name[39];
    sruct Node *next Ptr;}
    typedef struct Node Node;
    
    void insert(Node** firstHndl,char s[])
    {Node*prt malloc(sizeof(char(Node));
    strcpy(ptr->name,s);
    ptr->nextPtr=*firstHndl;
    *firstHndl=ptr;}
    The bolded code this what I need to understand. how the insertion is made.
    Thank you
    B.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Draw a list on paper.
    "animate" what happens to the pointers as you "run" that code by hand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    first of all u need a proper indentation. this is how it looks when properly indented
    Code:
    struct Node
    {
        char name[39];
        struct Node *next Ptr;
    }typedef struct Node Node;
    
    void insert(Node** firstHndl,char s[])
    {
        Node*prt malloc(sizeof(char(Node));
        strcpy(ptr->name,s);
        ptr->nextPtr=*firstHndl;
        *firstHndl=ptr;
    }
    > The bolded code this what I need to understand. how the insertion is made.
    this last two line are the one which actually forms the lined list. if u can think of soemthing like follow
    Code:
    |____|__| -> |_____|__| -> |____|_/_|   <-- assume a linked list
       ^
      *firstHndle
    
    |_____|_/_|   |_____|__| -> |_____|__| -> |____|_/_|
                     ^
                     *firstHndle
     ptr->nextPtr=*firstHndl;
    |_____|__| -> |_____|__| -> |_____|__| -> |____|_/_|
                    ^
                   *firstHndle
    
    *firstHndl=ptr;
    |_____|__| -> |_____|__| -> |_____|__| -> |____|_/_|
      ^
    *firstHndle
    if u go thorugh the diagram clearly u will understand what that two statment means

    EDIT: Tried my level best to keep the *firstHndle to point to the right node

    ssharish2005
    Last edited by ssharish2005; 04-22-2006 at 09:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 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