题目描述:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/17e9c7952e08a32e1cc1ab26b40cc02d.png)
解题思路:
代码实现:
#include <iostream>
#include <algorithm>
using namespace std;
struct Lnode {
int data;
Lnode* next;
Lnode() {}
Lnode(int data, Lnode* next) : data(data), next(next) {}
};
int caortx(Lnode* HL, int x) {
Lnode* p = HL;
int cnt = 0;
while (p) {
if (p->data == x) {
cnt++;
}
p = p->next;
}
return cnt;
}
int main() {
Lnode a, b, c, d, e;
a.next = &b;
b.next = &c;
c.next = &d;
d.next = &e;
e.next = NULL;
a.data = 1;
b.data = 2;
c.data = 1;
d.data = 4;
e.data = 1;
cout << caortx(&a, 1);
return 0;
}