Page 34 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 34

Unit 2: Linked Lists





                      p=(struct node *)malloc(sizeof(struct node));                             Notes
                      if(p==NULL)
                      {
                   printf(“Error\n”);
                          exit(0);
                   }
                   p-> data = n;
                   p-> link = NULL;
                }
                else
                      p->link = insert(p->link,n);/* the while loop replaced by
             recursive call */
                   return (p);
                }
                void printlist ( struct node *p )
                {
                      printf(“The data values in the list are\n”);
                      while (p!= NULL)
                      {
                   printf(“%d\t”,p-> data);
                         p = p-> link;
                      }
                }
                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 );
                }






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