Page 101 - DCAP407_DATA_STRUCTURE
P. 101

Data Structure



                                          150
                                          750
                                          650
                                          660
                                          120
                                          390
                                          350
                                          800

                                          In this example:
                                          1.   Two integer lists, named LIST[ ] and LIST1[ ] are declared with an element
                                               storage capacity of 20.
                                          2.   An integer pointer named PTR is declared to point to the next element.
                                          3.   An integer variable named HEAD is declared to specify the current position
                                               of the PTR.
                                          4.   A list of values are assigned for LIST[ ] and LIST1[ ].
                                          5.   The PTR is set at position 11 in the list.
                                          6.   The first  while  loop, prints the value present in  LIST[11]  (6)  and then
                                               traverses to the eleventh element of LIST1[11] whose value is 16. The list then
                                               traverses back to LIST[16] and prints its value (79) as shown in the output.
                                          7.   The steps 5 and 6 are repeated for each element until the PTR is not equal to -
                                               1, which depicts the end of the elements in a list.
                                          8.   The second  while  loop calls the function  MULT(), which multiplies the
                                               traversed data by  10  and prints the value. For example, the value in
                                               LIST[11]=6 is multiplied by 10 and the result 60 is printed.

                                          9.   Step 8 is repeated until the end of the loop is reached, i.e., until PTR is not
                                               equal to -1.



                                      Write a function to print the reverse of elements present in a list.


                          6.2   Searching a Linked List

                          Searching is the most common operation performed in a linked list. The search  operation involves
                          traversing through the list to search for a specific item. Several iterations may be performed until the
                          desired number is found or until the end of the list is reached. In search operation, every element in the
                          list is associated with a key element. The element is searched in the list by using a key.

                                         The program searches for a specific number entered by  the user by traversing
                                          through each  node present in a list. The program  also  displays the number’s
                                         position in the list.

                                         #include<stdio.h>
                                         #include<conio.h>
                                         int LIST[20];
                                         int LIST1[20];
                                         int HEAD;
                                         int SEARCH(int);
                                         void main()
                                         {



                          94                      LOVELY PROFESSIONAL UNIVERSITY
   96   97   98   99   100   101   102   103   104   105   106