// 定义带头结点的单链表结点结构
struct ListNode {
int data;
struct ListNode *next;
};
// 计算所有数据域为x的结点个数的函数
int count_X(ListNode *head, int x) {
ListNode *p = head->next; // 从第一个结点开始遍历
int count = 0;
while (p != NULL) {
if (p->data == x) {
count++; // 数据域为x,计数加一
}
p = p->next; // 移动到下一个结点
}
return count;
}