C Program to Count the Occurrences of Elements in a Linked List

«
»
This C Program find the number of occurrences of all elements in a linked list.

Here is source code of the C Program to find the number of occurrences of all elements in a linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find Number of Occurences of All Elements in a Linked List 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int num;
  10.     struct node *next;
  11. };
  12.  
  13. struct node_occur
  14. {
  15.     int num;
  16.     int times;
  17.     struct node_occur *next;
  18. };
  19.  
  20. void create(struct node **);
  21. void occur(struct node *, struct node_occur **);
  22. void release(struct node **);
  23. void release_2(struct node_occur **);
  24. void display(struct node *);
  25. void disp_occur(struct node_occur *);
  26.  
  27. int main()
  28. {
  29.     struct node *p = NULL;
  30.     struct node_occur *head = NULL;
  31.     int n;
  32.  
  33.     printf("Enter data into the list\n");
  34.     create(&p);
  35.     printf("Displaying the occurence of each node in the list:\n");
  36.     display(p);
  37.     occur(p, &head);
  38.     disp_occur(head);
  39.     release(&p);
  40.     release_2(&head);
  41.  
  42.     return 0;
  43. }
  44.  
  45. void occur(struct node *head, struct node_occur **result)
  46. {
  47.     struct node *p;
  48.     struct node_occur *temp, *prev;
  49.  
  50.     p = head;
  51.     while (p != NULL)
  52.     {
  53.         temp = *result;
  54.         while (temp != NULL && temp->num != p->num)
  55.         {
  56.             prev = temp;
  57.             temp = temp->next;
  58.         }
  59.         if (temp == NULL)
  60.         {
  61.             temp = (struct node_occur *)malloc(sizeof(struct node_occur));
  62.             temp->num = p->num;
  63.             temp->times = 1;
  64.             temp->next = NULL;
  65.             if (*result != NULL)
  66.             {
  67.                 prev->next = temp;
  68.             }
  69.             else
  70.             {
  71.                 *result = temp;
  72.             }
  73.         }
  74.         else
  75.         {
  76.             temp->times += 1;
  77.         }
  78.         p = p->next;
  79.     }
  80. }
  81.  
  82. void create(struct node **head)
  83. {
  84.     int c, ch;
  85.     struct node *temp, *rear;
  86.  
  87.     do
  88.     {
  89.         printf("Enter number: ");
  90.         scanf("%d", &c);
  91.         temp = (struct node *)malloc(sizeof(struct node));
  92.         temp->num = c;
  93.         temp->next = NULL;
  94.         if (*head == NULL)
  95.         {
  96.             *head = temp;
  97.         }
  98.         else
  99.         {
  100.             rear->next = temp;
  101.         }
  102.         rear = temp;
  103.         printf("Do you wish to continue [1/0]: ");
  104.         scanf("%d", &ch);
  105.     } while (ch != 0);
  106.     printf("\n");
  107. }
  108.  
  109. void display(struct node *p)
  110. {
  111.     while (p != NULL)
  112.     {
  113.         printf("%d\t", p->num);
  114.         p = p->next;
  115.     }
  116.     printf("\n");
  117. }
  118.  
  119. void disp_occur(struct node_occur *p)
  120. {
  121.     printf("***************************\n  Number\tOccurence\n***************************\n");
  122.     while (p != NULL)
  123.     {
  124.         printf("    %d\t\t%d\n", p->num, p->times);
  125.         p = p->next;
  126.     }
  127. }
  128.  
  129. void release(struct node **head)
  130. {
  131.     struct node *temp = *head;
  132.     *head = (*head)->next;
  133.     while ((*head) != NULL)
  134.     {
  135.         free(temp);
  136.         temp = *head;
  137.         (*head) = (*head)->next;
  138.     }
  139. }
  140.  
  141. void release_2(struct node_occur **head)
  142. {
  143.     struct node_occur *temp = *head;
  144.     *head = (*head)->next;
  145.     while ((*head) != NULL)
  146.     {
  147.         free(temp);
  148.         temp = *head;
  149.         (*head) = (*head)->next;
  150.     }
  151. }

$ cc occurence.c 
$ ./a.out 
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 6
Do you wish to continue [1/0]: 1
Enter number: 1
Do you wish to continue [1/0]: 0
 
Displaying the occurence of each node in the list:
1	2	3	2	4	2	6	1	
***************************
  Number	Occurence
***************************
    1		2
    2		3
    3		1
    4		1
    6		1

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.