中易网

C语言达人帮忙解读一下程序

答案:2  悬赏:0  
解决时间 2021-04-20 22:23

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student
{
long stuID;
char name[20];
char course[10];
float grade;
struct student *next;
};
typedef struct student stu;

stu *creatlist();
stu *insertlist(stu *p,stu *head);
void display(stu *head);

int main()
{
int num;
stu *head1,*head2,*head3;
stu *p;
char *course1="math",*course2="english",*course3="physics";
printf("pls enter the total number of students:\n");
scanf("%d",&num);


head1=creatlist();
head2=creatlist();
head3=creatlist();

while(num--)
{
p=(stu *)malloc(sizeof(stu));
printf("pls enter your student ID,then choose the course name:\n");
printf("name student ID course\n");
scanf("%s%ld%s",p->name,&p->stuID,p->course);
p->next=NULL;

if(!strcmp(p->course,course1))
head1=insertlist(p,head1);
if(!strcmp(p->course,course2))
head2=insertlist(p,head2);
if(!strcmp(p->course,course3))
head3=insertlist(p,head3);
}
if(head1->stuID!=0)
display(head1);
else free(head1);
if(head2->stuID!=0)
display(head2);
else free(head2);
if(head3->stuID!=0)
display(head3);
else free(head3);

return 0;
}

stu *creatlist()
{
stu *head;
head=(stu *)malloc(sizeof(stu));
if(head!=NULL)
{
head->next=NULL;
head->stuID=0;
}
else
{
printf("creat list head failed!\n");
exit(1);
}
return head;
}

stu *insertlist(stu *p,stu *head)
{
stu *pf,*pb,*t;
pb=head;
pf=head;
if(pb->next==NULL&&pb->stuID==0)
{
head=p;
head->next=NULL;
}
else
{
t=pf;
while(pb!=NULL&&p->stuID>=pb->stuID)
{
pf=t;
t=pb;
pb=pb->next;
}
if(t==head&&p->stuID<=head->stuID)
{
p->next=head;
head=p;


}
else
if(t->next==NULL&&p->stuID>=t->stuID)
{
pf->next=p;
p->next=NULL;
}
else
{
pf->next=p;
p->next=pb;
}
}

return head;
}

void display(stu *head)
{
while(head!=NULL)
{
printf("\n%s\t%ld %s",head->name,head->stuID,head->course);
head=head->next;
}
}

最佳答案

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student
{
long stuID;
char name[20];
char course[10];
float grade;
struct student *next;
};
typedef struct student stu;

stu *creatlist();
stu *insertlist(stu *p,stu *head);
void display(stu *head);

int main()
{
int num;
stu *head1,*head2,*head3;
stu *p;
char *course1="math",*course2="english",*course3="physics";
printf("pls enter the total number of students:\n");
scanf("%d",&num);



head1=creatlist();
head2=creatlist();
head3=creatlist();



while(num--)
{
p=(stu *)malloc(sizeof(stu));
printf("pls enter your student ID,then choose the course name:\n");
printf("name student ID course\n");
scanf("%s%ld%s",p->name,&p->stuID,p->course);
p->next=NULL;

if(!strcmp(p->course,course1))
head1=insertlist(p,head1);
if(!strcmp(p->course,course2))
head2=insertlist(p,head2);
if(!strcmp(p->course,course3))
head3=insertlist(p,head3);
}

if(head1->stuID!=0)
display(head1);
else free(head1);
if(head2->stuID!=0)
display(head2);
else free(head2);
if(head3->stuID!=0)
display(head3);
else free(head3);


return 0;
}

stu *creatlist()
{
stu *head;
head=(stu *)malloc(sizeof(stu));
if(head!=NULL)
{
head->next=NULL;
head->stuID=0;
}
else
{
printf("creat list head failed!\n");
exit(1);
}
return head;
}

stu *insertlist(stu *p,stu *head)
{
stu *pf,*pb,*t;
pb=head;
pf=head;
if(pb->next==NULL&&pb->stuID==0)
{
head=p;
head->next=NULL;
}
else
{
t=pf;
while(pb!=NULL&&p->stuID>=pb->stuID)
{
pf=t;
t=pb;
pb=pb->next;
}
if(t==head&&p->stuID<=head->stuID)
{
p->next=head;
head=p;



}
else
if(t->next==NULL&&p->stuID>=t->stuID)
{
pf->next=p;
p->next=NULL;
}
else
{
pf->next=p;
p->next=pb;
}
}


return head;
}

void display(stu *head)
{
while(head!=NULL)
{
printf("\n%s\t%ld %s",head->name,head->stuID,head->course);
head=head->next;
}
}

全部回答

这是个成绩处理系统。

首先定义了五个变量,学号,名字,课程,分数和一个指向下一个学生的指针。

然后定义了三个函数:创建链表,插入链表和显示学生数据,这三个函数都是通过指针来进行操作的。

哎呀,太长了,你哪里不懂,你就一点点问吧。这要是一句一解释,打字会累死的。

我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
作业太多怎么办啊!
紫茗阁怎么去啊,有知道地址的么
绵阳哪些厂在招人底薪多少具体地址?
成龙合作过的男明星有个光头
在浙江国际海运技术学院需要自带被子吗
楚雄哪个县离昆明最近
秋田犬白色刚满4个月体重32斤。
所有的人都有两面吗?善良与邪恶.
求问一个牌匾上的字
抽烟喝酒感觉如何?
一个企业要想有发展,必须要提高员工的素质,
为什么酷狗创建不了号?
初二人教版数学上册P16第九题答案是...
时尚公寓(群英中路)地址在哪,我要去那里办事
宝宝拉肚子能吃鸡蛋吗,宝宝才有8个月大的,
推荐资讯
帮忙策划一个闲鱼集市能和社区街道搞的活动,
怎么查手机是什么时候激活的
电磁炉中的电源芯片a12能否用a22的代换
富锦二手车奥驰2000有卖的吗
电脑用HDMI线连接显示器开机后黑屏,用ps3连
15款傲虎led改透镜需要解码么
接触器的工作原理(简化示意图)
天猫分期第一期下单收费收多少
中华英才网找工作可靠吗
为什么登陆不了DNF
请问装修小孩房间什么颜色搭配最好,求具体搭
怎么样才能让孩子做事情动作变快,听话
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?