Question description Admission for the current Academic year is happening in Most of the Universities across the Country. Once the Students got admitted they are assigned a unique Registration Number. Admission in charges used to assign give these details in some order. But during enrolment of the student there is a specific entrance test for admitted students to get scholarship. now admission cell conducting a test. one of the question was , a singly linked list and a key, count number of occurrences of given key in linked list. For example, if given linked list is 1->2->1->2->1->3->1 and given key is 1, then output should be 4.
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;

}