Linked list Implement on C

  Inserttion At end of List

#include<stdlib.h>
#include<stdio.h>

struct node
{
 int info;
 struct node *link;
};
struct node *STR = 0;
struct node* creatnode()
{
  struct node *n;
  n=(struct node*)malloc(sizeof(struct node));
  return(n);
}

struct node insertnode()
{
  struct node *temp,*t;
  temp=creatnode();
  printf("Enter a node : ");
  scanf("%d",&temp->info);
  temp->link=NULL;
  if( STR==NULL)
     STR=temp->link;
  else
  {

    t=STR;
   while( t->link != 0)
         t=t->link;

  // or for(t=STR;t->link!=0;t=t->link);
    t->link=temp;
   }
}


main()
{int i;
for(i=1;i<3;i++)
insertnode();
}
  

No comments:

Post a Comment