程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Menu
{
int num;
char name[50];
struct Menu *next;
}Book,*List;
struct name
{
int num;
char name[50];
};
List createlist(int n);
void outputlist(List L);
int main()
{
int n;
List h;
printf("请输入数据个数:");
scanf("%d",&n);
h=createlist(n);
outputlist(h);
return 0;
}
List createlist(int n)
{
List L=(Book*)malloc(sizeof(Book));
L->next=NULL;
Book *r=L;
int i;
for(i=0;i<n;i++)
{
Book* p=(Book*)malloc(sizeof(Book));
scanf("%d%s",&p->num,&p->name[0]);
p->next=NULL;
r->next=p;
r=p;
}
return L;
}
void outputlist(List L)
{
struct name t,num[26]={0};
Book *p;
p=L->next;
while(p)
{
num[p->name[0]-65].num++;
strcpy(num[p->name[0]-65].name,p->name);
//printf("num=%d,name=%s\n",p->num,p->name);
p=p->next;
}
for(int i=0;i<25;i++)
{
for(int j=0;j<25-i;j++)
{
if(num[j].num<num[j+1].num)//比较大小
{
t=num[j];
num[j]=num[j+1];
num[j+1]=t;
}
}
}
for(int i=0;i<3;i++)
{
printf("%s\n",num[i].name);
}
}