summeraaaaaaaa
summeraaaaaaaa
2017-12-07 11:20

找出链表中年龄最大的学生

5
  • 链表

#include
#include
#include
struct student
{
int number;
char name[30];
char sex[3];
int age;
student *next;
};

student* fileInput(char* fileName);
void traversal(student* phead);
int main()
{
student *phead;
char *fileName;
strcpy(fileName,"C:\Users\88450\桌面\周四\students.dat");
phead=fileInput(fileName);
traversal(phead);

return 0;

}

student* fileInput(char* fileName)
{
student s,*pnew,*phead,*ptail;
FILE fp;
fp=fopen("students.dat","rb");
int i=0;
while(fread(&s,sizeof(student),1,fp)!=0)

{
pnew=(student
)malloc(sizeof(student));
pnew->number=s.number;
strcpy(pnew->name,s.name);
strcpy(pnew->sex,s.sex);
pnew->age=s.age;
pnew->next=NULL;

if(i==0)
  {
    phead=pnew;
    ptail=pnew;
  }
else
  {
    ptail->next=pnew;
    ptail=ptail->next; 
  }
i++;
}   
fclose(fp);
return phead;

}

void traversal(student* phead)
{
student *p;
p=phead;
while(p!=NULL)

{
printf ("学号:%d 姓名:%s 性别:%s 年龄:%d\n",p->number,p->name,p->sex,p->age);
p=p->next;

}

}
如何编一个自定义函数,查找出年龄最大的学生(要可实现可找出多个最大年龄的学生),谢谢各位大佬了!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

14条回答