c code to find occurrence in linked list

  • clinked list
  • write a c code to find occurrence in linked list

    WAP to find number of occurrences of all elements in a linked list

    Code

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { int data; struct node *next; }; int init_count(struct node *r) { int count = 0; while (r) { r = r->next; count++; } return count; } void display(struct node *r) { printf("elements of list are : "); while (r) { printf("%d ---> ", r->data); r = r->next; } printf("NULL\n"); } struct node *create(struct node *head) { struct node *t, *p; int n; printf("****Enter the node in sorted way****\n"); if (head == NULL) { t = (struct node *)malloc(sizeof(struct node)); printf("enter the node : \n"); scanf("%d", &t->data); t->next = NULL; head = t; } if (head != NULL) { do { t = (struct node *)malloc(sizeof(struct node)); printf("enter the node: \n"); scanf("%d", &t->data); t->next = NULL; p = head; while (p->next != NULL) { p = p->next; } p->next = t; printf("do you want to add more node press 1 for yes, 0 for no. "); scanf("%d", &n); } while (n); } return head; } int occurrence(struct node *r,int c){ int rec=0; while (r) { if(r->data==c){ rec++; } r = r->next; } return rec; } int main() { struct node *head = NULL,*r,*t; printf("####enter the data of linked_list.####\n"); head = create(head); r=head; t=head; int data[init_count(head)],found,k=0; while(r!=NULL){ found =0; for(int i=0;i<init_count(head);i++){ if(data[i]==r->data){ found =1; } } if(found==0){ data[k]=r->data; k++; } r=r->next; } int l=0; printf("data\toccurence\n"); while(l<k){ printf(" %d\t %d\n",data[l],occurrence(head,data[l])); l++; } return 0; }