fork download
  1. #include<stdio.h>
  2. struct node
  3. {
  4. int item;
  5. struct node *next
  6. };
  7. int main()
  8. {
  9. struct node *start,*list,*temp;
  10. int i;
  11.  
  12. start = (struct node *)malloc(sizeof(struct node));
  13. list = start;
  14. start->next = NULL;
  15. for(i=1;i<5 ;i++)
  16. {
  17. list->item = i;
  18. list->next = (struct node *)malloc(sizeof(struct node));
  19. list = list->next;
  20. }
  21. list->next = NULL;
  22. temp = start;
  23. int length =0;
  24. while(temp!=NULL)
  25. {
  26.  
  27. if (temp->next == NULL)
  28. {
  29. break;
  30. }
  31. else
  32. {
  33. length++;
  34. temp=temp->next;
  35. }
  36. }
  37. printf("Length of Linked List : %d \n",length);
  38. printf("Elements of linked lists are :\n");
  39. while(start != NULL)
  40. {
  41. if (start->next == NULL)
  42. {
  43. break;
  44. }
  45. printf("%d\n",start->item);
  46. start = start->next;
  47. }
  48. return 0;
  49. }
  50.  
  51.  
  52.  
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
Length of Linked List : 4 
Elements of linked lists are :
1
2
3
4