Page 36 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 36

Unit 2: Linked Lists





                else                                                                            Notes
                {
                   temp = p;
                   while (temp-> link != NULL)
                    temp = temp-> link;
                    temp-> link = (struct node *)malloc(sizeof(struct node));
                   if(temp -> link == NULL)
                   {
                         printf(“Error\n”);
                      exit(0);
                   }
                   temp = temp-> link;
                   temp-> data = n;
                   temp-> link = NULL;
                   }
                   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 );
                }




                                           LOVELY PROFESSIONAL UNIVERSITY                                    31
   31   32   33   34   35   36   37   38   39   40   41