单链表的结构定义如下:
typedefstructnode
{intdata;
structnode*next;
}Node,*LinkList;
试编写算法intCountLinklist(LinkListhead,intx)实现在结点的单链表head中计算值为
x的结点数。
单链表的结构定义如下:
typedefstructnode
{intdata;
structnode*next;
}Node,*LinkList;
试编写算法intCountLinklist(LinkListhead,intx)实现在结点的单链表head中计算值为
x的结点数。
【正确答案】:int CountLinklist(LinkList head,int x) {Node*P=head; int cnt=0; while(P一>next!=NULL) {P=P一>next; if(P一>data==x)cnt++; } retum(cnt); }
Top