Thursday, September 16, 2010

C Program for insertion and deletion operations in a "Doubly Linked List"

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
    struct node *left;
    int data;
    struct node *right;
};

void d_append(struct node **s,int num)
{
   struct node *r,*q=*s;
   if(*s==NULL)
   {
     /* Create a New Node */
     (*s)=(struct node*)malloc(sizeof(struct node));
     (*s)->left=NULL;
     (*s)->data=num;
     (*s)->right=NULL;
   }
   else
   {
      /* Traverse the Linked List till the last node is reached */
      while(q->right != NULL)
      q=q->right;

      /* Add a new node at the end */
      r=(struct node*)malloc(sizeof(struct node));
      r->right=NULL;
      r->left=q;     //*s
      q->right=r;
      r->data=num;
   }
}
void d_display(struct node *q)
{
  printf("\n");
  /* Traverse the entire linked list */
  while(q != NULL)
  {
    printf(" %d\t",q->data);
    q=q->right;
  }
}

void d_addatbeg(struct node **s,int num)
{
    struct node *q;
    /* Create a new node */
    q=(struct node*)malloc(sizeof(struct node));

    /* Assign data and pointer to the new node */
    q->left=NULL;
    q->data=num;
    q->right=(*s);
    /* Make new node the head node */
    (*s)->left=q;
    (*s)=q;
}
void d_addafter(struct node *q,int loc,int num)
{
   struct node *temp;
   int i;

   /* Skip tp desired position */
   for(i=0;i<loc;i++)
   {
     q=q->right;
     /* If end of list is encountered   */
     if(q==NULL)
     {
 printf("\n There are less than %d elements ",loc);
 return;
     }
   }

   /* Insert new node */
     q=q->left;
     temp=(struct node*)malloc(sizeof(struct node));
     temp->data=num;
     temp->left=q;
     temp->right=q->right;
     temp->right->left=temp;
     q->right=temp;
}

void d_delete(struct node **s,int num)
{
   struct node *q=*s;

   /* Traverse the entire linked list */
   while(q != NULL)
   {
     /* If node to be deleted is found */
     if(q->data==num)
     {
 /* If node to be deleted is the first node */
 if(q==*s)
 {
    (*s)=(*s)->right;
    (*s)->left=NULL;
 }
 else
 {
  /* If node to be deleted is the last node */
  if(q->right==NULL)
    q->left->right=NULL;

    else
  /* If node to be deleted ia any intermediate node */
    {
 q->left->right=q->right;
 q->right->left=q->left;
    }
    free(q);
  }
  return;
  }
  q=q->right;   /* Go to next node */
  }
  printf("\n%d not found ",num);
}

void main()
{
    struct node *head;
    clrscr();
    head=NULL;
    d_append(&head,1);
    d_append(&head,2);
    d_append(&head,3);
    d_append(&head,4);
    d_append(&head,5);
    printf("\n\n The Linked List is :\n");
    d_display(head);
    printf("\n\n After insertion at the beginning ");
    d_addatbeg(&head,100);
    d_display(head);
    printf("\n\n Insertion after a specified node ");
    d_addafter(head,2,200);
    d_display(head);
    printf("\n\n After Deletion of an element");
    d_delete(&head,200);
    d_display(head);
    getch();
}

No comments:

Post a Comment