统c语言计输入的一行字符串中各个字符出现的次数
统计输入的一行字符串中各个字符出现的次数。算法:先输入一个字符串,对串中每一个字符,先到链表上逐个结点查找。若找到,给该字符频数加1:否则为该字符产生一个结点,并插入链首。
#include "stdio.h"
struct node
{char c;
int count;
struct node *next;};
#define ND struct node
#define NP struct node*
11
void print(NP h)
{while(____________________)
{printf("%c,%d\n",h->c,h->count);
h=h->next;}
}
NP search(NP h,char ch)
{NP p; p=h;
while(p!=NULL)
{
if(p->c==ch)
{p->count++; break; }
else
22
p=p->next;}
if(p==NULL)
{p=________________________________________;
p->c=ch; p->count=1;
p->next=h;
h=p;}
return h;}
main()
{char s[300],*p=s; NP h;
gets(s); h=NULL;
while(*p!='\0')
{h=search(h,*p); ____________________;}
33
print(h);} 44
相关主题