Page 103 - DCAP407_DATA_STRUCTURE
P. 103

Data Structure



                                         else
                                         p=LIST1[p];
                                         }
                                         return(L);
                                         }

                                         Output:
                                         When you run the program, the following output is obtained:
                                         Traversed List:
                                         75   55   19  10   45   60  20  8  80  35  12  68
                                         Enter the number to search
                                         8
                                         Number 8 is present at index location 9 in the list
                                         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 16 in the list.
                                         6.   The first while loop prints the value present in LIST[16] which is (75) and
                                              traverses to the third element of  LIST1[16]  whose value is  0. The list then
                                              traverses  back to  LIST[0]  and then prints  its value  (55)  as shown in the
                                              output.
                                         7.   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 program prompts the input from the user to search a specific number in
                                              the list.

                                         9.   If the number entered is equal to HEAD, then the while loop is terminated.
                                              Otherwise, the  LIST1[p]  is stored in  p. The  while  loop  iterates again and
                                              again until p reaches the end of the list.
                                         10.  If the  NUM_LOC  has not reached the end of the list,  then the  NUM  and
                                              NUM_LOC  will be printed. Otherwise,  a message saying “number not
                                              present in the list” is displayed.





                          Did you know?   Accessing data in a linked list takes more time when compared to arrays because to
                                        access a specific element, the list has to be traversed from starting point to the end
                                        point of the list, which consumes more time.
                          6.3   Inserting a Node into a Linked List

                          Insertion is one of the basic operations in linked lists. There are three types of insertions that can be
                          carried out:

                          1.  Inserting a node at the beginning of a list
                          2.  Inserting a node at the end of the list
                          3.  Inserting a node after a given node




                          96                      LOVELY PROFESSIONAL UNIVERSITY
   98   99   100   101   102   103   104   105   106   107   108