Friday, April 8, 2011

Inserting a node after the specified node in a singly linked list


Program

   # include <stdio.h>
   # include <stdlib.h>
   int length ( struct node * );
   struct node
   {
      int data;
      struct node *link;
   };

   /* a function which appends a new node to an existing list used for
building a list */
   struct node *insert(struct node *p, int n)
   {
      struct node *temp;
      if(p==NULL)
      {
          p=(struct node *)malloc(sizeof(struct node));
         if(p==NULL)
         {
                 printf("Error\n");
            exit(0);
         }
         p-> data = n;
         p-> link = NULL;
      }
      else
      {
         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);
   }
   /* a function which inserts a newly created node after the specified
node */
   struct node * newinsert ( struct node *p, int node_no, int value )
   {
      struct node *temp, * temp1;
      int i;
      if ( node_no <= 0 || node_no > length (p))
      {
              printf("Error! the specified node does not exist\n");
         exit(0);
      }
      if ( node_no == 0)
      {
         temp = ( struct node * )malloc ( sizeof ( struct node ));
         if ( temp == NULL )
         {
                 printf( " Cannot allocate \n");
                 exit (0);
         }
         temp -> data = value;
         temp -> link = p;
         p = temp ;
   }
   else
   {
       temp = p ;
      i = 1;
      while ( i < node_no )
      {
            i = i+1;
            temp = temp-> link ;
      }
      temp1 = ( struct node * )malloc ( sizeof(struct node));
      if ( temp == NULL )
      {
            printf ("Cannot allocate \n");
            exit(0)
      }
      temp1 -> data = value ;
      temp1 -> link = temp -> link;
      temp -> link = temp1;
   }
   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 list before deletion is\n");
      printlist ( start );
      printf(" \n Enter the node no after which the insertion is to be
done\n");
      scanf ( " %d",&n);
      printf("Enter the value of the node\n");
      scanf("%d",&x);
      start = newinsert(start,n,x);
      printf("The list after insertion is \n");
      printlist(start);
   }

No comments:

Post a Comment