Sunday, March 10, 2013

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


Write C program to Create, insert 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 * AddnodeFirst(struct node *f)
   {
     struct node *t;
     t=getnode();
     t->link=f;
     f=t;
     return f;

   }
   struct node * addnodeLast(struct node *f)
   {
     struct node *t,*temp;
     t=getnode();
     temp=f;
     while(temp->link!=NULL)
     {
       temp=temp->link;
     }
     temp->link=t;

     return f;
    }

   struct node * addnodemiddle(struct node *f,int pos)
   {
     struct node *temp,*t;
     int i;
     temp=f;
     for(i=1;i<pos-1; i++)
     {
       temp=temp->link;
     }
     t=getnode();
     t->link=temp->link;
     temp->link=t;
     return f;
   }


  void main()
  {
     int ct;
     struct node *first;
     clrscr();
     first=createnodes();
     displaylist(first);
     ct=countnodes(first);
     printf("number of nodes=%d\n",ct);
     first=AddnodeFirst(first);
     displaylist(first);
     first=addnodeLast(first);
     displaylist(first);
     first=addnodemiddle(first,3);
     displaylist(first);
     getch();
  }


Note: Instead of Addnodefirst, addnodelast and addnodemiddle function use InsertNodes function which as the functionality of above three functions

struct node * InsertNodes(struct node *f,int pos)
   {
     struct node *temp,*t;
     int i,ct;
     ct=countnodes(f);
     if (pos==1)
     {
       t=getnode();
       t->link=f;
       f=t;
     }
     else if (pos>ct)
     {
       temp=f;
       while(temp->link!=NULL)
       {
             temp=temp->link;
       }
       t=getnode();
       temp->link=t;
     }
     else
     {
       for(i=1;i<pos-1; i++)
       {
             temp=temp->link;
       }
       t=getnode();
       t->link=temp->link;
       temp->link=t;
     }
     return f;
   }

No comments:

Post a Comment