Linked List Program in C

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. #include<malloc.h>  
  4. void main()  
  5. {  
  6. struct node  
  7. {  
  8. int num;  
  9. struct node *ptr;  
  10. };  
  11. typedef struct node NODE;  
  12. NODE *head, *first, *temp;  
  13. int count=0;  
  14. int choice=1;  
  15. first=NULL;  
  16. while(choice)  
  17. {  
  18.     head=(NODE *)malloc(sizeof(NODE));  
  19.     printf("Enter the data item\n");  
  20.     scanf("%d",&head->num);  
  21.       if(first!=NULL)  
  22.         {  
  23.           temp->ptr=head;  
  24.           temp=head;  
  25.         }  
  26. else  
  27.         {  
  28.           first=temp=head;  
  29.         }  
  30. fflush(stdin);  
  31. printf("Do you want to continue(type 0 or 1)?\n");  
  32. scanf("%d",&choice);  
  33. }  
  34.   
  35. temp->ptr=NULL;  
  36. temp=first;  
  37. printf("Status of the linked list is\n");  
  38. while(temp!=NULL)  
  39.     {  
  40.         printf("%d",temp->num);  
  41.         count++;  
  42.         temp=temp->ptr;  
  43.     }  
  44. printf("NULL");  
  45. printf("NO of nodes in the list =%d\n",count);  
  46. getch();