![](https://i-blog.csdnimg.cn/blog_migrate/37904561ff971678e8a7b88da4a37f70.png)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode {
ElemType data;
LinkNode* next;
}LinkNode, * LinkList;
//尾插法建立单链表
void creatLinkList(LinkList& L) {
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL;
LinkNode* pTail = L;
int num;
while (scanf("%d", &num) && num != -1) {
LinkNode* pnode = (LinkNode*)malloc(sizeof(LinkNode));
pnode->data = num;
pnode->next = pTail->next;
pTail->next = pnode;
pTail = pnode;
}
}
//打印输出
void printLinkList(LinkList L) {
LinkNode* pCur = L->next;
while (pCur != NULL) {
printf("%d ", pCur->data);
pCur = pCur->next;
}
}
//统计次数
int countNum(LinkList L, int num) {
LinkNode* pCur = L->next;
int count = 0;
while (pCur != NULL) {
if (pCur->data == num) {
count++;
}
pCur = pCur->next;
}
return count;
}
int main() {
LinkList L;
creatLinkList(L);
int num;
scanf("%d", &num);
//printLinkList(L);
printf("%d在单链表中出现了%d次", num, countNum(L,num));
return 0;
}