Write a function/program to counts the number of times a given int/number occurs in a Linked List

function/program to counts the number of times a given int/number occurs in a Linked List

1. Initialize count as zero.
 2. Loop through each element of linked list:
    a) If element data is equal to the passed number then increment the count(n->data==a).
 3. Return count.

#include<stdio.h>
#include<stdlib.h>

struct node{

int data;
struct node *next;
};

void insert(struct node **head,int new_data)
{
struct node *new_node;
new_node=(struct node*)malloc(sizeof(struct node));

new_node->data=new_data;
new_node->next=*head;
*head=new_node;

}
int count(struct node *n,int a)
{
int count=0;
while(n!=0)
{
if(n->data==a)
{
count++;
}
n=n->next;
}
return count;
}

main()
{

struct node *head;
head=(struct node*)malloc(sizeof(struct node));

insert(&head,1);
insert(&head,1);
insert(&head,2);
insert(&head,3);
insert(&head,1);
insert(&head,1);
insert(&head,4);

int x=count(head,1);

printf("Number of 1 is:%d\n",x);

}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics