【文件属性】:
文件名称:结构体排序查找以及链表的使用
文件大小:1KB
文件格式:C
更新时间:2014-07-19 03:24:02
排序查找以及链表
学习结构体排序查找以及链表的使用#include
#include
#include
struct student
{
int num;
double score;
char name[100];
struct student *next;
};
int cmp(const void *a,const void *b)
{
struct student *p1=(struct student*)a;
struct student *p2=(struct student*)b;
if(p1->score!=p2->score)
return p1->score-p2->score?1:-1;
else if(p1->num!=p2->num)
return p1->num-p2->num;
else return strcmp(p1->name,p2->name);
}
void main()
{
struct student s[100],*q,key,*p,*head;
int n,i;
while(scanf("%d",&n)!=EOF)
{
for(i=0;inum,p->score,p->name);
p=p->next;
}
qsort(s,n,sizeof(s[0]),cmp);
head=&s[0];
for(i=1;inum,p->score,p->name);
p=p->next;
}
scanf("%d %lf %s",&key.num,&key.score,key.name);
q=(struct student*)bsearch(&key,s,n,sizeof(s[0]),cmp);
if(q)
printf("%d %.2lf %s",q->num,q->score,q->name);
else printf("No\n");
}
}