Page 31 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 31

Advanced Data Structure and Algorithms




                    Notes          While defining linked list we must have recursive defi nitions:

                                   struct node
                                   {
                                      int data;
                                      struct node * link;
                                   }
                                   Here, link is a pointer of struct node type i.e. it can hold the address of variable of struct node type.
                                   Pointers permit the referencing of structures in a uniform way, regardless of the organization
                                   of the structure being referenced. Pointers are capable of representing a much more complex
                                   relationship between elements of a structure than a linear order.

                                   Initialization:
                                   main()
                                   {
                                       struct node *p, *list, *temp;
                                      list = p = temp = NULL;
                                      .
                                      .
                                      .
                                   }




                                      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 the existing list is empty then insert a new node as the
                                                 starting node */
                                                    if(p==NULL)
                                                {
                                                   p=(struct node *)malloc(sizeof(struct node)); /* creates new
                                                   node data value passes
                                                as parameter */
                                                   if(p==NULL)
                                                  {
                                                printf(“Error\n”);




          26                               LOVELY PROFESSIONAL UNIVERSITY
   26   27   28   29   30   31   32   33   34   35   36