fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node{
  5. int data;
  6. struct node * next;
  7. };
  8.  
  9. void newnode(struct node ** headr , int item)
  10. {
  11. struct node * newn = (struct node *)malloc(sizeof(struct node));
  12. newn->data = item;
  13. newn->next = *headr;
  14. *headr = newn;
  15. }
  16.  
  17. void printnode(struct node * head)
  18. {
  19. struct node * t = head;
  20. while(t != NULL)
  21. {
  22. printf("%d->" , t->data);
  23. t = t->next;
  24. }
  25. printf("NULL\n");
  26. }
  27.  
  28. void countnode(struct node * head , int item)
  29. {
  30. struct node * t = head;
  31. int c = 0;
  32. while(t != NULL)
  33. {
  34. if(t->data == item)
  35. c+=1;
  36. t = t->next;
  37. }
  38. printf("%d occurs %d times\n" ,item,c);
  39. }
  40.  
  41. int main()
  42. {
  43. struct node * head =NULL;
  44. newnode(&head,1);
  45. newnode(&head,2);
  46. newnode(&head,3);
  47. newnode(&head,4);
  48. newnode(&head,3);
  49. newnode(&head,6);
  50. newnode(&head,3);
  51. printnode(head);
  52. countnode(head,3);
  53. return 0;
  54. }
  55.  
  56.  
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
3->6->3->4->3->2->1->NULL
3 occurs 3 times