1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

linked list

Discussion in 'C' started by himanshu belwal uit, Dec 11, 2011.

  1. himanshu belwal uit

    himanshu belwal uit New Member

    Joined:
    Dec 11, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hey i m new in c language and i cannot understands the use of linked list in it
     
  2. mailme.rohit91

    mailme.rohit91 New Member

    Joined:
    Nov 9, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    hyderabad,india.
    hi bro, linked list is used to store various kinds of data. take an example of a school. if they have to maintain a list of students in a serial no(i.e., who joined the school first ) then they can store data by creating link list. when the user will try to access the list he will get all the info regarding students in that serial only. we use pointers in link list. hope you know about pointers. pointers are used to store the address of another variable(address of another memory location). so, that you can use pointer to access data stored in other memory location. in link list also, we take a data type struct and create a node containing necessary data type which can store the info.
    eg: struct class
    {
    int rollno; //to store the roll no. of student
    char name[20]; //to store name of student
    int tot_marks; //to store student's total marks
    struct class *next; //it is a self referential pointer which stores address of
    another location of data type struct as we have created
    above(i.e., struct class)
    };

    link list is of 2 types -i)single linked list- which use only one pointer to access another element(node , here class) which is inserted after it.
    ii)double linked list-uses two pointer; one pointer to access data inserted after it and another to access data inserted before it.

    there is circular link list also which is made by pointing the pointer of the last node (*next of the last node which you have inserted) to the first node instesd of pointing it to NULL.

    i.e., consider this figure . it is a node(think of struct class;it will contain 4 part)
    [int rollno|char name|int tot_mark|*next]
    |
    |------>[int rollno|char name|int tot_mark|*next]
    |
    NULL<---|
    here the first block contains the roll no,name,total marks of a student and a pointer *next ehich will point to a second node. consider the second node as the last node then the *next of last node will point to NULL.

    this is a case of single link list.
    initially when no info is there in the linked list the first node i.e., struct class *first should be initialized to NULL(i.e., struct class *first=NULL).
    if you have to insert some info in the class do this by using malloc(keyword for allocating memory in runtime)
    eg: struct class *first; //create a pointer by name first to point on the first node
    first=(struct class*)malloc(sizeof(struct class)) ; //to allocate memory in runtime


    to enter roll no, name and marks use printf and scanf then and use the allocated runtime memory space to save them in that space.

    eg: printf("enter the roll no:");
    scanf("%d",&first->rollno);
    printf("enter the name:");
    scanf("%s",&first->name);
    printf("enter the marks:");
    scanf("%d",&first->tot_mark);
    first->next=NULL;

    above statements are used to enter or access the required data in the first(name of node).
    the last statement in the above is used to point to the next node(here we haven't created any other node therefore it is pointed to NULL since the first and the last node is same.)

    hope u must have understood something....
     
  3. mailme.rohit91

    mailme.rohit91 New Member

    Joined:
    Nov 9, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    hyderabad,india.
    --------------------------------------------------------------------------------

    hi bro, linked list is used to store various kinds of data. take an example of a school. if they have to maintain a list of students in a serial no(i.e., who joined the school first ) then they can store data by creating link list. when the user will try to access the list he will get all the info regarding students in that serial only. we use pointers in link list. hope you know about pointers. pointers are used to store the address of another variable(address of another memory location). so, that you can use pointer to access data stored in other memory location. in link list also, we take a data type struct and create a node containing necessary data type which can store the info.
    eg: struct class
    {
    int rollno; //to store the roll no. of student
    char name[20]; //to store name of student
    int tot_marks; //to store student's total marks
    struct class *next; //it is a self referential pointer which stores address of
    another location of data type struct as we have created
    above(i.e., struct class)
    };

    link list is of 2 types -i)single linked list- which use only one pointer to access another element(node , here class) which is inserted after it.
    ii)double linked list-uses two pointer; one pointer to access data inserted after it and another to access data inserted before it.

    there is circular link list also which is made by pointing the pointer of the last node (*next of the last node which you have inserted) to the first node instesd of pointing it to NULL.

    i.e., consider this figure . it is a node(think of struct class;it will contain 4 part)
    [int rollno|char name|int tot_mark|*next]
    |
    |------>[int rollno|char name|int tot_mark|*next]
    |
    NULL<---|
    here the first block contains the roll no,name,total marks of a student and a pointer *next ehich will point to a second node. consider the second node as the last node then the *next of last node will point to NULL.

    this is a case of single link list.
    initially when no info is there in the linked list the first node i.e., struct class *first should be initialized to NULL(i.e., struct class *first=NULL).
    if you have to insert some info in the class do this by using malloc(keyword for allocating memory in runtime)
    eg: struct class *first; //create a pointer by name first to point on the first node
    first=(struct class*)malloc(sizeof(struct class)) ; //to allocate memory in runtime


    to enter roll no, name and marks use printf and scanf then and use the allocated runtime memory space to save them in that space.

    eg: printf("enter the roll no:");
    scanf("%d",&first->rollno);
    printf("enter the name:");
    scanf("%s",&first->name);
    printf("enter the marks:");
    scanf("%d",&first->tot_mark);
    first->next=NULL;

    above statements are used to enter or access the required data in the first(name of node).
    the last statement in the above is used to point to the next node(here we haven't created any other node therefore it is pointed to NULL since the first and the last node is same.)

    hope u must have understood something....
     

Share This Page