Page 90 - DCAP407_DATA_STRUCTURE
P. 90

Unit 5: Introduction to Linked List



                                   #include<stdio.h>
                                   struct new_list
                                   {
                                         int value;
                                         struct new_list *next_element;
                                   } n1, n2, n3, n4;       //Creates four nodes of type new_list

                                   void main()
                                   {
                                    int j;
                                   n1.value = 200;        //Assigning value to node1
                                   n2.value = 400;        //Assigning value to node2
                                   n3.value = 600;        //Assigning value to node3
                                   n4.value = 800;        //Assigning value to node4

                                   n1.next_element = &n2;      //Assigning address of node2 to node1
                                   n2.next_element = &n3;      //Assigning address of node3 to node2
                                   n3.next_element = &n4;      //Assigning address of node4 to node3
                                   n4.next_element = 0;      //Assigning 0 to node4 to indicate the end of the list

                                   j = n1.next_element->value;       //Storing the value of node1 in variable j
                                   printf(“%d\n”, j);
                                   /* you can use this statement to print the value present in node1 or print j
                                   directly as depicted in the above statement*/
                                   printf(“%d\n”, n1.next_element->value);
                                   printf(“%d/n”, n4.next_element->value);         //Printing the value of node4
                                   printf(“%d/n”, n2.next_element->value);        //Printing the value of node2
                                   printf(“%d/n”, n3.next_element->value);        //Printing the value of node3
                                   }
                                   Output:
                                   When you run the program, the following output is obtained:
                                   400
                                   0
                                   600
                                   800
                                   In this example:
                                   1.   First a structure named new_list is created. The list contains an integer
                                        data variable named value to store data and a pointer variable named
                                        next_element to point to next node.
                                   2.   Then, four objects namely, n1, n2, n3, and n4 are created to access the
                                        structure elements. In the program they act as nodes in a list.
                                   3.   In the main() function, the value for the four nodes n1, n2, n3, and n4 are
                                        assigned.
                                   4.   Then, the address of n2 is stored in n1, address of n3 is stored in n2, and
                                        address of  n4 is  stored in n3. The address of  n4 is  assigned  zero to
                                        indicates the end of the list.
                                   5.   Finally, the value present in n1, n4, n2 and n3 are printed.




                           Write a simple singly linked list program to accept the elements from the user and store
                           it in a list.







                                        LOVELY PROFESSIONAL UNIVERSITY                           83
   85   86   87   88   89   90   91   92   93   94   95