发布网友
共1个回答
热心网友
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct info
{
int no;
char name[20];
char s;
float score;
struct info *next;
}NODE;
NODE *creat()
{
NODE *head = NULL;
NODE *p,*r =NULL;
char str[100] = {0};
NODE tmp = {0};
gets(str);
sscanf(str,"%d %s %c %f", &(tmp.no), tmp.name, &(tmp.s), &(tmp.score));
while(strlen(str)>5)
{
p = (NODE*)malloc(sizeof(NODE));
memcpy(p,&tmp,sizeof(NODE));
if (NULL==head)
{
head = p;
}else
{
r->next = p;
}
r = p;
gets(str);
sscanf(str,"%d %s %c %f", &(tmp.no), tmp.name, &(tmp.s), &(tmp.score));
}
r->next = NULL;
return head;
}
NODE *findmax(NODE *p)
{
NODE tmp = {0};
NODE *ptr = NULL;
while (p)
{
if (p->score > tmp.score)
{
memcpy(&tmp,p,sizeof(NODE));
ptr = p;
}
p = p->next;
}
return ptr;
}
void prn(NODE *p)
{
printf("no<%d> name<%s> sex<%c> score<%f>", p->no,p->name,p->s,p->score);
}
void freelst(NODE *pHead)
{
NODE *p;
while (pHead)
{
p = pHead;
pHead = pHead->next;
free(p);
p = NULL;
}
}
void main()
{
NODE *head,*p;
head=creat(); //创建list
p=findmax(head); //查找max
prn(p); //输出信息
freelst(head); //释放申请到的内存空间
}