#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int sno
;
char name
[20];
char gender
;
int age
;
student
* next
;
};
student
creatalist(student
*phead
,int n
);
student
change(student
* phead
);
void numberOfquery(student
*,char*);
void traverseList(student
* phead
);
int main()
{
student
* phead
=NULL;
phead
=(student
*)malloc(sizeof(student
));
student
* pnew
;
int n
;
printf("学生人数:");
scanf("%d",&n
);
creatalist(phead
,n
);
change(phead
);
char sname
[20];
printf("\n待查的学生姓名:");
fflush(stdin);
gets(sname
);
numberOfquery(phead
,sname
);
traverseList(phead
);
phead
=NULL;
free(phead
);
return 0;
}
student
creatalist(student
*phead
,int n
)
{
student
* pnew
;
phead
->next
=NULL;
for(int i
=0;i
<n
;i
++)
{
pnew
=(student
*)malloc(sizeof(student
));
printf("\n学号:");
scanf("%d",&(pnew
->sno
));
printf("姓名:");
fflush(stdin);
gets(pnew
->name
);
printf("性别:");
pnew
->gender
=getchar();
printf("年龄:");
scanf("%d",&(pnew
->age
));
pnew
->next
=NULL;
pnew
->next
=phead
->next
;
phead
->next
=pnew
;
}
return *phead
;
}
student
change(student
* phead
)
{
student
*pnew
,*pwork
;
pnew
= phead
->next
;
phead
->next
= NULL;
while(pnew
!= NULL)
{
pwork
=pnew
;
pnew
= pnew
->next
;
pwork
->next
= phead
->next
;
phead
->next
= pwork
;
}
return *phead
;
}
void traverseList(student
* phead
)
{
int i
= 0;
student
* p
= NULL;
p
= phead
->next
;
printf("\n 学号 姓名 性别 年龄 \n");
if(NULL == p
)
{
printf("链表为空!\n");
}
else
{
while(p
)
{
printf("\n第%d个学生 ",i
+1);
printf(" %d %s %c %d",p
->sno
,p
->name
,p
->gender
,p
->age
);
p
= p
->next
;
i
++;
}
}
return;
}
void numberOfquery(student
* phead
,char* sname
)
{
int sum
=0;
student
* p
=phead
;
while(p
->next
!=NULL)
{
p
=p
->next
;
if(strcmp(p
->name
,sname
)==0) sum
=sum
+1;
}
printf("\n叫%s名字的人有%d位",sname
,sum
);
return;
}
转载请注明原文地址: https://mac.8miu.com/read-508066.html