69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
# define len sizeof(struct stu)
struct stu
{
char num[20];
char name[20];
int scr;
struct stu *next;
};
int n;
struct stu *lib()
{
struct stu *head;
struct stu *p1,*p2;
n=0;
p1=p2=(struct stu *)malloc(len);
scanf("%s %s %d",p1->num,p1->name,&p1->scr);
p1->next = NULL;
head=NULL;
//while(p1->num!="0"||p1->name!="0"||p1->scr!=0) //字符串比较不能用“==”和“!=”直接比较
while(0 != strcmp(p1->num,"0") || 0 != strcmp(p1->name,"0") || 0 != p1->scr)
{
n+=1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct stu *)malloc(len);
p1->next = NULL; //设置为节点的指针域为空
scanf("%s %s %d",p1->num,p1->name,&p1->scr);
getchar();
}
p2->next=NULL;
return (head);
}
void prt(struct stu *head)
{
struct stu *p;
p=head;
if(head!=NULL)
{
while(p!=NULL)
{
printf("%s %s %d\n",p->num,p->name,p->scr);
p=p->next;
}
}
}
void main()
{
struct stu *head=NULL;
head=lib();
prt(head);
return ;
}