问答题
以下程序的功能是;利用单向链表统计一段英文单词或数字串中单词或数字串出现的频率。假设每行长度不超过80个字符,单词、数字串之间由空格、/t或/n分隔,一个单词或数字串的长度不超过20个字符。从键盘上输入任意一段英文单词或数字串,并以单词stop结束输入。函数create()依次读入一个单词或数字串后,首先与链表各节点上的数据(单词或数字串)进行比较,若已存在,则将该数据出现的次数加1;否则,构建一个新的节点,将该数据作为该新节点的数据域,并插入已建链表的尾部。当读取到stop时,结束链表的建立。函数print()输出链表各节点的值,函数dele_list()依次删除链表上的节点。例如,执行以下程序时,从键盘上输入下面一段英文单词或数字串;
abs 345 happy good yes no 345
it we are yes no 45 stop
程序统计后的输出为;
abs(1)345(2)happy(I)good(1)yes(2)no(2)it(1)we(1)are(1)45(1)
[程序](4分)
#include
#include
typedef struct node
{
int count;
char data[20];
struct node *next;
}word;
word *create(void)
{
char str[20];
word*h=NULL,*r,*s,*p; //h指向第一个节点,r指向最后一个节点
while(1){
cin>>str;
if(strcmp(str,"stop")==0)
break;
p=h;
while(p!=NULL&&strcmp(p->data,str)!=0)
p=p->next;
if(p!=NULL)
___(27)___;
else{
s=new word;
s->count=1;
strcpy(s->data,str);
s->next=NULL;
if(h==NULL){
h=s;
___(28)___;
}
else{
___(29)___;
r=s;
}
}
}
return h;
}
void print(word *p)
{
if(p==NULL)
cout<<"空表"<data<<"("<count<<")";
p=p->next;
}
cout<