设计一个算法通过遍历确定单链表中数据域值为key的节点个数


#include 
typedef struct node1
{
    int data;
    struct node1 *next;
}node;

int count(node *head,int key)
{
node *p=head;
int n=0;
while(p)
{
if(p->data==key)
{
n++;
}
p=p->next;
return n;
}
}

还缺什么

return n;写在while循环外面

还需知道,链表带不带头结点的?供参考:

#include<stdio.h> 
typedef struct node1
{
    int    data;
    struct node1* next;
}node;
int count(node* head, int key)
{
    node* p = head;
    int n = 0;
    while (p)
    {
        if (p->data == key)
        {
            n++;
        }
        p = p->next;
        //return n;修改
    }
    return n; //移动到这
}

不缺