Sunday, March 10, 2013

Write C program to Delete nodes at Beginning, Middle and at End using functions in Single Linked List


Write C program to Delete nodes at Beginning, Middle and at End using functions

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

  struct node * getnode()
  {
    struct node *p;
    p=(struct node *)malloc(sizeof(struct node));
    printf("enter data into the node:");
    scanf("%d",&p->data);
    p->link=NULL;
    return p;
  }

  int  countnodes(struct node *f)
   {
    struct node *temp;
    int ct=0;
    temp=f;
    while(temp!=NULL)
    {
      ct++;
      temp=temp->link;
    }
    return ct;
  }

  struct node * createnodes()
  {
    struct node *first,*t,*temp;
    char ch='Y';
    int ct=0;
    while(ch=='Y')
    {
      if(ct==0)
      {
       first=getnode();
       ct++;
       temp=first;

      }
      else
      {
       temp=first;
       while(temp->link!=NULL)
       {
            temp=temp->link;
       }
       t=getnode();
       temp->link=t;
       temp=t;
      }
      printf("do you want to continue:");
      fflush(stdin);
      ch=getchar();
     }
     return first;
   }

   void displaylist (struct node *f)
   {
     struct node *temp;
     temp=f;
     while(temp!=NULL)
     {
      printf("%d-->",temp->data);
      temp=temp->link;
     }
     printf("NULL\n");
  }

/* struct node * deletefirstnode(struct node *first)
  {
     struct node *temp;
     temp=first;
     first=temp->link;
     free(temp);

   return first;
  }
  struct node * deletelastnode(struct node *first)
   {
    struct node *temp,*temp1;
    temp=first;
    temp1=first;
    while(temp->link!=NULL)
    {
     temp=temp->link;
    }
    while(temp1->link!=temp)
    {
     temp1=temp1->link;
    }
     temp1->link=NULL;
     free(temp);
     return first;

   }

   struct node *deletemiddlenode(struct node *first, int pos)
   {
     struct node *temp,*temp1;
     int i;
     temp=first;
     temp1=first;
     for(i=1;i<pos;i++)
     {
      temp=temp->link;
     }
     for(i=1;i<pos-1;i++)
     {
      temp1=temp1->link;
     }
     temp1->link=temp->link;
     free(temp);
     return first;
    }    */

    struct node * deletenodes(struct node *f,int pos)
    {
     struct node *t,*t1;
     int ct,i;
     ct=countnodes(f);
     t=f;
     t1=f;
     if(ct<pos)
     {
      printf("please cheak number of nodes & position to deleted\n");


     }

     else if(pos==1)
     {
      f=f->link;
      free(t);
     }
     else if(pos==ct)
     {
      for(i=1;i<pos;i++)
      {
       t=t->link;
      }
      for(i=1;i<pos-1;i++)
      {
       t1=t1->link;
      }
      t1->link=NULL;
      free(t);
     }
     else
     {
      for(i=1;i<pos;i++)
      {
       t=t->link;
      }
      for(i=1;i<pos-1;i++)
      {
       t1=t1->link;
      }
      t1->link=t->link;
      free(t);
     }
     return f;
   }

   void main()
   {
     int ct,n;
     struct node *first;
     clrscr();
     first=createnodes();
     displaylist(first);
     ct=countnodes(first);
     printf("number of nodes=%d\n",ct);
    /* first=deletefirstnode(first);
     displaylist(first);
     first=deletelastnode(first);
     displaylist(first);
     first=deletemiddlenode(first,4);*/
     printf("enter which node to be deleted\n");
     scanf("%d",&n);
     first=deletenodes(first,n);
     displaylist(first);
     getch();
  }

No comments:

Post a Comment