Page 93 - DCAP407_DATA_STRUCTURE
P. 93

Data Structure





                                               The program shows the implementation of a circular linked list consisting of
                                               three nodes. The program displays the value present in each node.

                                               #include<stdio.h>
                                               Struct list
                                               {
                                                    int value;
                                                          struct list *next_element;
                                               } n1, n2, n3;               //Creates four nodes of type new_list
                                               void main()
                                               {
                                                int j;

                                               n1.value = 35;                // Assigning value to node1
                                               n2.value = 65;                // Assigning value to node2
                                               n3.value = 85;                 //Assigning value to node3

                                               n1.next_element = &n2;             //Assigning address of node2 to
                                               node1
                                               n2.next_element = &n3;             //Assigning address of node3 to
                                               node2
                                               n3.next_element = &n1;             //Assigning address of node3 to
                                               node1

                                               j = n1.next_element->value;        //Storing the value of node1 in
                                               variable j
                                               printf(“%d\n”, j);             //Printing the value of j
                                               /* you can use this statement to print the value present in node1*/
                                               printf(“%d\n”, n1.next_element->value);

                                               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:
                                               65
                                               65
                                               85
                                               35
                                               In this example:
                                               1.  First, a structure named 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, the three objects namely, n1, n2, and n3 are created to access the
                                                   structure elements. In this program, these objects act as nodes in a list.
                                               3.  In the main() function, the value for nodes n1, n2 and n3 are assigned.
                                               4.  Then, the address of n2 is stored in n1 and address of n3 is stored in
                                                   n2. Since, it is a circular list, the address of n3 is assigned to n1 instead
                                                   of NULL value.
                                               5.  Finally, the values present in n1, n2, and n3 are printed.




                          86                      LOVELY PROFESSIONAL UNIVERSITY
   88   89   90   91   92   93   94   95   96   97   98