填空题
以下程序创建-个链表并实现数据统计功能。函数WORD *create(char a[][20],int n)创建-个包含n个结点的单向链表,结点数据来自a指向的数组中存储的n个单词(字符串)。函数void count(WORD *h)统计h指向的单向链表中不同单词各自出现的次数,将统计结果保存到局部数组c中并输出。程序运行时输出结果为"red:1 green:2 blue:3"试完善程序以达到要求的功能。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct w
{char word[20];
struct w *next:
}WORD;
WORD *create(char a[][20],int n)
{WORD *p1,*p2,*h=0;int i;
for(i=0;i1(27) 2,a[i]);
if(h==O)
h=p2=p1:
else
{p2->next=p1;p2=pl;}
}
p2->next= 3(28) 4;
return h;
}
void count(WORD *h)
{ struct
{char word[20];
int num;
}c[6]={0};
int m=0,i;
while(h)
{if(m==O)
{strcpy(c[0].word,h->word);
c[0].num=1;m++;
}
else
{for(i=O;iword)==0)
{ 5(29) 6 ;
break;
}
if(i>=m)
{strcpy(c[m].word,h->word);
c[m++].num=1;
}
}
7(30) 8;
}
for(i=0;i9
{char words[6][20]={"red","green","blue","blue","green","blue"};
WORD *head=0:
head=create(words,6);
count(head);
}
【正确答案】
1、(27)p1->word
(28)0或NULL
(29)c[i].min++
(30)h=h->next
【答案解析】