Page 33 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 33

Advanced Data Structure and Algorithms




                    Notes                      temp=temp->link;
                                               } while (temp!= p);
                                         }
                                         else
                                            printf(“The list is empty\n”);
                                      }
                                      void main()
                                      {
                                         int n;
                                         int x;
                                         struct node *start = NULL ;
                                         printf(“Enter the nodes to be created \n”);
                                         scanf(“%d”,&n);
                                         while ( n -- > 0 )
                                         {
                                      printf( “Enter the data values to be placed in a node\n”);
                                            scanf(“%d”,&x);
                                            start = insert ( start, x );
                                         }
                                         printf(“The created list is\n”);
                                         printlist ( start );
                                      }

                                   2.3 Inserting a Node using Recursive Programs

                                   A linked list is a recursive data structure. A recursive data structure is a data structure that has
                                   the same form regardless of the size of the data. You can easily write recursive programs for such
                                   data structures.




                                      Lab Exercise   Program
                                     # include <stdio.h>
                                        # include <stdlib.h>
                                        struct node
                                        {
                                        int data;
                                        struct node *link;
                                        };
                                        struct node *insert(struct node *p, int n)
                                        {
                                           struct node *temp;
                                           if(p==NULL)
                                           {



          28                               LOVELY PROFESSIONAL UNIVERSITY
   28   29   30   31   32   33   34   35   36   37   38