C Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int count(struct node* head,int search_for)
{
int c=0;
while(head!=NULL)
{
if(head->data==search_for)
c++;
head=head->next;
}
return c;
}
int main()
{
int n,x,i,val;
struct node *head=NULL,*newnode,*temp;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&val);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=head;
head=newnode;
}
printf("Linked list:-->");
temp=head;
while(temp)
{
printf("%d",temp->data);
if(temp->next!=NULL)
printf("-->");
temp=temp->next;
}
scanf("%d",&x);
printf("\nCount of %d:%d",x,count(head,x));
return 0;
}
0 Comments