Page 95 - DCAP407_DATA_STRUCTURE
P. 95

Data Structure



                                         h.previous = &n3;               //Assigning address of node3 to HEAD node

                                         j = n1.next_element->value;                 //Storing the value of node1 in variable j
                                         printf(“%d\n”, j);
                                         //printf(“%d\n”, n1.next->value);   // 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”, n2.next->value);                //Printing the value of node2
                                         printf(“%d/n”, n3.next->value);                //Printing the value of node3
                                         printf(“%d/n”, h.next->value);                  //Printing the value of HEAD node
                                         printf(“%d/n”, n1.previous->value);       //Printing the previous value of node1
                                         printf(“%d/n”, n2.previous->value);       //Printing the previous value of node2
                                         printf(“%d/n”, n3.previous->value);       //Printing the previous value of node3
                                         printf(“%d/n”, h.previous->value);         //Printing the previous value of HEAD
                                         node
                                         }

                                         Output:
                                         When you run the program, the following output is obtained:
                                         15
                                         20
                                         3
                                         10
                                         3
                                         10
                                         15
                                         20

                                         In this example:
                                         1.   First,  a structure named  list  is created. The  list  contains an  integer data
                                             variable named value to store data, a pointer variable named next_element to
                                             point to next node, and a pointer variable named previous_element to point to
                                             previous node.
                                         2.   Then, the four objects namely,  n1, n2, n3,  and  h  are created to access  the
                                             structure elements. In  this program, these objects  act as nodes in a list. The
                                             HEAD node (h) contains the total number of nodes present in the list.
                                         3.   In the main() function, the value for the nodes n1, n2, n3, and h are assigned.
                                         4.   Then, the address of n2 is stored in n1 and the address of n3 is stored in n2. In
                                             order to traverse backwards, the address of h is stored in n3 and address of n1
                                             is stored in h.
                                         5.   Finally, the values present in n1, n2, n3, and h are printed.



                                               Write a C program to store 20 integers in descending order in a linked list.












                          88                      LOVELY PROFESSIONAL UNIVERSITY
   90   91   92   93   94   95   96   97   98   99   100