Thread: help understanding linked list

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    77

    help understanding linked list

    I have this code perfectly working for representation of one way linked list in C..
    using test dummy constants in main()
    I formed the list

    13 -> 23 -> 24 -> 45 -> 34 -> 45

    At few points on this code..I am still unable to figure out the use of function pointer

    Take the example *Getnode()...(it returns a node if memory alloction for a node is possible)
    and please explain to me what is the purpose of '*' in getnode()





    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #define NULL 0
    typedef struct node
    {
    int DATA;
    struct node *LINK;
    }NODE;
    NODE *Getnode();
    NODE *InsertAtEnd(NODE *,int);
    void Display(NODE *);
    NODE *Getnode()
    {
    NODE *New;
    New=(NODE *)malloc(sizeof(NODE));
    New->LINK=NULL;
    return New;
    }
    NODE *InsertAtEnd(NODE *START,int x)
    {
    NODE *New;
    NODE *Copy;
    Copy=START;
    New=Getnode();
    if(New==NULL)
    {
    printf("MEMORY ALLOCATION ERROR\n");
    }
    New->DATA=x;
    if(START==NULL)
    {
    Copy=New;
    }
    
    while(START->LINK!=NULL)
    {
    START=START->LINK;
    }
    START->LINK=New;
    return Copy;
    }
    void Display(NODE *START)
    {
    while(START!=NULL)
    {
    printf("%d ",START->DATA);
    START=START->LINK;
    }
    }
    int main()
    {
    NODE *S=NULL;
    clrscr();
    S=InsertAtEnd(S,13);
    S=InsertAtEnd(S,23);
    S=InsertAtEnd(S,34);
    S=InsertAtEnd(S,45);
    S=InsertAtEnd(S,34);
    S=InsertAtEnd(S,45);
    Display(S);
    getch();
    return 0;
    }
    //END

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by progmateur View Post
    I have this code perfectly working for representation of one way linked list in C..
    Looks like I have a different idea of "perfectly working". After removing the unnecessary <conio.h> functions I get:
    Code:
    $ make foo
    cc -Wall -Wextra -ggdb3    foo.c   -o foo
    foo.c:4:0: warning: "NULL" redefined [enabled by default]
    /usr/lib/gcc/i686-linux-gnu/4.6.1/include/stddef.h:402:0: note: this is the location of the previous definition
    $ gdb -q ./foo
    Reading symbols from foo...done.
    (gdb) r
    Starting program: foo 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x080484b3 in InsertAtEnd (START=0x0, x=13) at foo.c:36
    36      while(START->LINK!=NULL)
    You don't differentiate between inserting a node into an empty list and adding nodes to a list.

    And please indent your code!

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    well I have it working here in Turbo C...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by progmateur
    At few points on this code..I am still unable to figure out the use of function pointer

    Take the example *Getnode()...(it returns a node if memory alloction for a node is possible)
    and please explain to me what is the purpose of '*' in getnode()
    The * means that the function returns a pointer. Also, be aware that "function pointer" can mean something other than what you are probably thinking to some of us.




    Code:
    New=Getnode();
    if(New==NULL)
    {
    printf("MEMORY ALLOCATION ERROR\n");
    }
    New->DATA=x;
    So... if the memory allocation fails you print a message and then go on about using the bad pointer?





    Code:
    NODE *Getnode()
    {
    NODE *New;
    New=(NODE *)malloc(sizeof(NODE));
    New->LINK=NULL;
    return New;
    }
    Again, you need to make sure you aren't accessing a bad pointer BEFORE you start trying to use it. Also, you should not cast the return value of malloc in C programs; C++ would force you to do so but not C. From a maintenance standpoint, it is usually better to use the variable itself in the sizeof call. This helps if you change the type of your pointer... you don't need to hunt down and change all those sizeof calls to use the new type.

    Code:
    NODE *Getnode()
    {
        NODE *New;
        New = malloc(sizeof(*New));
        if( New != NULL )             // Make sure your pointer is good...
            New->LINK=NULL;   // ...before you try to use it
        return New;
    }




    Code:
    #include <malloc.h>
    The proper C header to use the malloc function is stdlib.h. The malloc.h header is non-standard I believe and so should be avoided.





    Code:
    #define NULL 0
    This should be unnecessary. NULL is already defined at least one of the standard headers (stddef.h?) and it can typically be found in many others. For you, stdio.h and stdlib.h are the most likely candidates that should have a definition available.




    Quote Originally Posted by progmateur
    well I have it working here in Turbo C...
    A bit of a joke of a compiler to many on this forum... this does not fill us with confidence. There are many options of much more up-to-date (and free) IDEs/compilers available for you to choose from.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I could not help not posting again the list with which I was introduced to them. Here it is. Fully commented
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly Linked List - Understanding...
    By hi-0ctane in forum C Programming
    Replies: 3
    Last Post: 05-31-2009, 05:27 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. understanding linked list
    By sara.stanley in forum C Programming
    Replies: 5
    Last Post: 04-14-2006, 04:32 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Replies: 2
    Last Post: 11-28-2003, 11:50 AM