标签:inf ring null pre def int mamicode for lib
/*下列程序的功能:创建一个链表并实现数据统计。函数WORD *create(char *a[][20],int n) 创建一个包含n个结点的单向链表,结点数据来自a所指向的数组中存储的n个单词(字符串)。 函数void count(WORD *h)统计h指向的单向链表中不同单词各自出现的次数,将统计结果保存到 局部数组c中并输出。*/ #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;i<n;i++) { p1=(WORD *)malloc(sizeof(WORD)); strcpy(p1->word,a[i]); if(h==0) h=p2=p1; else { p2->next=p1;p2=p1; } } p2->next=NULL; return h; } void count(WORD *h) { struct { char word[20]; int num; }c[10]={0}; int m=0,i; while(h) { if(m==0) { strcpy(c[0].word,h->word); c[0].num=1;m++; } else { for(i=0;i<m;i++) if(strcmp(c[i].word,h->word)==0) { c[i].num++; break; } if(i>=m) { strcpy(c[m].word,h->word); c[m].num++; m++; } } h=h->next; } for(i=0;i<m;i++) printf("%s:%d ",c[i].word,c[i].num); } int main() { char words[10][20]={"red","green","blue","blue","green","blue","black","white","red","pink"}; WORD *head=0; head=create(words,10); count(head); }
运行结果:
标签:inf ring null pre def int mamicode for lib
原文地址:https://www.cnblogs.com/yanglike111/p/13175571.html