Sunday, March 10, 2013

Write C program to create and print Single linked list without using function


Write C program to create and print linked list without using function

struct node
{
   int data;
   struct node *link;
};

main()
{
  struct node *first,*temp;
  char ch=’y’;
  int ct=0;
//Creates Linked List 
  while (ch==’y’)
  {
        if (ct==0)
        {
           first=(struct node *)malloc(sizeof(struct node));
           printf("Enter data into the node: ");
           scanf("%d", &first->data);
           first->link=NULL;
           temp=first;
           ct++;
        }
        else
        {
           temp=first;
              while(temp->link!=NULL)
              {
                 temp=temp->link;
              }
             t=(struct node *)malloc(sizeof(struct node));
             printf("Enter data into the node: ");
             scanf("%d", &t->data);
             t->link=NULL;
             temp->link=t;
        }
      printf("Do you want to continue to create another node: ");
      fflush(stdin);
     ch=getchar();
   }

// Displaying Linked List
  temp=first;
    while (temp->link!=NULL)
    {
      printf("%d --> ",temp->data);
      temp=temp->link;
    }
    printf("%d -->NULL",temp->data);
 
 getch();
}

No comments:

Post a Comment