目录
老规矩,先看结果:
代码分析:
第一步:声明
第二步:输入函数
第三步:添加函数
第四步:输出函数
第五步:主函数
完整的代码:
老规矩,先看结果:
代码分析:
第一步:声明
#include
#include
struct student //声明结构体类型
{
int num;
float score;
struct student *next;
};
int n; //全局变量
第二步:输入函数
struct student *creat()
{
struct student *head;
struct student *p1,*p2; //声明结构体类型变量
p1=(struct student *)malloc(sizeof(struct student)); //开辟新的空间
p2=p1; //赋值
printf("请输入学生学号、成绩 :");
scanf("%d%f",&p1->num,&p1->score); //输入数据
head=NULL; //赋初始值
n=0; //赋初始值
while(p1->num!=0) //循环输入
{
n++; //记录结点数
if(n==1) //头结点
{
head=p1; //赋值
}
else
{
p2->next=p1; //p2指针指向p1
}
p2=p1; //赋值
p1=(struct student *)malloc(sizeof(struct student)); //又开辟新的空间
printf("请输入学生学号、成绩:");
scanf("%d%f",&p1->num,&p1->score); //输入
}
p2->next=NULL; //结尾为NULL
return head; //返回头指针
}
第三步:添加函数
struct student *add(struct student *h,int num,float score)
{
struct student *p1,*p2,*New; //声明结构体变量
p1=h; //p1开始指向头指针h
p2=NULL; //p2不动,初始化为NULL
while(p1!=NULL&&p1->numnext;
}
New=(struct student *)malloc(sizeof(struct student)); //开辟新的空间
New->num=num; //输入的数据给相应的结点
New->score=score; //输入的数据给相应的结点
New->next=p1; //由于结点已找到,所以New的尾部可以指向p1
if(p2==NULL) //p2未动,说明在头部
{
h=New; //将New的头部赋值给h
}
else
{
p2->next=New; //否则为中间或结尾,将New的头部赋值给p2的尾部
}
return h; //返回新的链表
}
第四步:输出函数
void print(struct student *h)
{
struct student *p;
p=h;
if(h!=NULL) //判断是否为空指针
{
printf("n结果是:");
do{
printf("nt%dt%.2f",p->num,p->score); //循环输出
p=p->next;
}while(p!=NULL);
}
}
第五步:主函数
int main()
{
struct student *creat(); //输入函数声明
struct student *add(struct student *h,int num,float score); //插入函数声明
void print(struct student *h); //输出函数声明
struct student *h;
int num;
float score;
int ch;
h=creat(); //调用输入函数
print(h); //调用输出函数,打印结果
while(1) //外循环,判断是否要添加数据
{
printf("n你是否需要添加数据:");
do{
ch=getchar();
}while(ch!='Y'&&ch!='N'); //用do——while循环判断,如果输入错误,再次输入
if(ch=='Y')
{
printf("n请你输入要加入的学生学号:");
scanf("%d",&num); //输入要添加的数据
printf("请你输入要加入的学生成绩:");
scanf("%f",&score); //输入要添加的数据
h=add(h,num,score); //调用添加函数
print(h);
}
else
{
break; //如果不添加,则跳出
}
}
printf("添加完毕!");
printf("最终数据为:n");
print(h);
return 0;
}
#include#include struct student //声明结构体类型 { int num; float score; struct student *next; }; int n; //全局变量
第二步:输入函数
struct student *creat()
{
struct student *head;
struct student *p1,*p2; //声明结构体类型变量
p1=(struct student *)malloc(sizeof(struct student)); //开辟新的空间
p2=p1; //赋值
printf("请输入学生学号、成绩 :");
scanf("%d%f",&p1->num,&p1->score); //输入数据
head=NULL; //赋初始值
n=0; //赋初始值
while(p1->num!=0) //循环输入
{
n++; //记录结点数
if(n==1) //头结点
{
head=p1; //赋值
}
else
{
p2->next=p1; //p2指针指向p1
}
p2=p1; //赋值
p1=(struct student *)malloc(sizeof(struct student)); //又开辟新的空间
printf("请输入学生学号、成绩:");
scanf("%d%f",&p1->num,&p1->score); //输入
}
p2->next=NULL; //结尾为NULL
return head; //返回头指针
}
第三步:添加函数
struct student *add(struct student *h,int num,float score)
{
struct student *p1,*p2,*New; //声明结构体变量
p1=h; //p1开始指向头指针h
p2=NULL; //p2不动,初始化为NULL
while(p1!=NULL&&p1->numnext;
}
New=(struct student *)malloc(sizeof(struct student)); //开辟新的空间
New->num=num; //输入的数据给相应的结点
New->score=score; //输入的数据给相应的结点
New->next=p1; //由于结点已找到,所以New的尾部可以指向p1
if(p2==NULL) //p2未动,说明在头部
{
h=New; //将New的头部赋值给h
}
else
{
p2->next=New; //否则为中间或结尾,将New的头部赋值给p2的尾部
}
return h; //返回新的链表
}
第四步:输出函数
void print(struct student *h)
{
struct student *p;
p=h;
if(h!=NULL) //判断是否为空指针
{
printf("n结果是:");
do{
printf("nt%dt%.2f",p->num,p->score); //循环输出
p=p->next;
}while(p!=NULL);
}
}
第五步:主函数
int main()
{
struct student *creat(); //输入函数声明
struct student *add(struct student *h,int num,float score); //插入函数声明
void print(struct student *h); //输出函数声明
struct student *h;
int num;
float score;
int ch;
h=creat(); //调用输入函数
print(h); //调用输出函数,打印结果
while(1) //外循环,判断是否要添加数据
{
printf("n你是否需要添加数据:");
do{
ch=getchar();
}while(ch!='Y'&&ch!='N'); //用do——while循环判断,如果输入错误,再次输入
if(ch=='Y')
{
printf("n请你输入要加入的学生学号:");
scanf("%d",&num); //输入要添加的数据
printf("请你输入要加入的学生成绩:");
scanf("%f",&score); //输入要添加的数据
h=add(h,num,score); //调用添加函数
print(h);
}
else
{
break; //如果不添加,则跳出
}
}
printf("添加完毕!");
printf("最终数据为:n");
print(h);
return 0;
}
struct student *creat()
{
struct student *head;
struct student *p1,*p2; //声明结构体类型变量
p1=(struct student *)malloc(sizeof(struct student)); //开辟新的空间
p2=p1; //赋值
printf("请输入学生学号、成绩 :");
scanf("%d%f",&p1->num,&p1->score); //输入数据
head=NULL; //赋初始值
n=0; //赋初始值
while(p1->num!=0) //循环输入
{
n++; //记录结点数
if(n==1) //头结点
{
head=p1; //赋值
}
else
{
p2->next=p1; //p2指针指向p1
}
p2=p1; //赋值
p1=(struct student *)malloc(sizeof(struct student)); //又开辟新的空间
printf("请输入学生学号、成绩:");
scanf("%d%f",&p1->num,&p1->score); //输入
}
p2->next=NULL; //结尾为NULL
return head; //返回头指针
}
struct student *add(struct student *h,int num,float score)
{
struct student *p1,*p2,*New; //声明结构体变量
p1=h; //p1开始指向头指针h
p2=NULL; //p2不动,初始化为NULL
while(p1!=NULL&&p1->numnext;
}
New=(struct student *)malloc(sizeof(struct student)); //开辟新的空间
New->num=num; //输入的数据给相应的结点
New->score=score; //输入的数据给相应的结点
New->next=p1; //由于结点已找到,所以New的尾部可以指向p1
if(p2==NULL) //p2未动,说明在头部
{
h=New; //将New的头部赋值给h
}
else
{
p2->next=New; //否则为中间或结尾,将New的头部赋值给p2的尾部
}
return h; //返回新的链表
}
第四步:输出函数
void print(struct student *h)
{
struct student *p;
p=h;
if(h!=NULL) //判断是否为空指针
{
printf("n结果是:");
do{
printf("nt%dt%.2f",p->num,p->score); //循环输出
p=p->next;
}while(p!=NULL);
}
}
第五步:主函数
int main()
{
struct student *creat(); //输入函数声明
struct student *add(struct student *h,int num,float score); //插入函数声明
void print(struct student *h); //输出函数声明
struct student *h;
int num;
float score;
int ch;
h=creat(); //调用输入函数
print(h); //调用输出函数,打印结果
while(1) //外循环,判断是否要添加数据
{
printf("n你是否需要添加数据:");
do{
ch=getchar();
}while(ch!='Y'&&ch!='N'); //用do——while循环判断,如果输入错误,再次输入
if(ch=='Y')
{
printf("n请你输入要加入的学生学号:");
scanf("%d",&num); //输入要添加的数据
printf("请你输入要加入的学生成绩:");
scanf("%f",&score); //输入要添加的数据
h=add(h,num,score); //调用添加函数
print(h);
}
else
{
break; //如果不添加,则跳出
}
}
printf("添加完毕!");
printf("最终数据为:n");
print(h);
return 0;
}
void print(struct student *h)
{
struct student *p;
p=h;
if(h!=NULL) //判断是否为空指针
{
printf("n结果是:");
do{
printf("nt%dt%.2f",p->num,p->score); //循环输出
p=p->next;
}while(p!=NULL);
}
}
int main()
{
struct student *creat(); //输入函数声明
struct student *add(struct student *h,int num,float score); //插入函数声明
void print(struct student *h); //输出函数声明
struct student *h;
int num;
float score;
int ch;
h=creat(); //调用输入函数
print(h); //调用输出函数,打印结果
while(1) //外循环,判断是否要添加数据
{
printf("n你是否需要添加数据:");
do{
ch=getchar();
}while(ch!='Y'&&ch!='N'); //用do——while循环判断,如果输入错误,再次输入
if(ch=='Y')
{
printf("n请你输入要加入的学生学号:");
scanf("%d",&num); //输入要添加的数据
printf("请你输入要加入的学生成绩:");
scanf("%f",&score); //输入要添加的数据
h=add(h,num,score); //调用添加函数
print(h);
}
else
{
break; //如果不添加,则跳出
}
}
printf("添加完毕!");
printf("最终数据为:n");
print(h);
return 0;
}
完整的代码:
#include#include struct student { int num; float score; struct student *next; }; int n; int main() { struct student *creat(); struct student *add(struct student *h,int num,float score); void print(struct student *h); struct student *h; int num; float score; int ch; h=creat(); print(h); while(1) { printf("n你是否需要添加数据:"); do{ ch=getchar(); }while(ch!='Y'&&ch!='N'); if(ch=='Y') { printf("n请你输入要加入的学生学号:"); scanf("%d",&num); printf("请你输入要加入的学生成绩:"); scanf("%f",&score); h=add(h,num,score); print(h); } else { break; } } printf("添加完毕!"); printf("最终数据为:n"); print(h); return 0; } struct student *creat() { struct student *head; struct student *p1,*p2; p1=(struct student *)malloc(sizeof(struct student)); p2=p1; printf("请输入学生学号、成绩 :"); scanf("%d%f",&p1->num,&p1->score); head=NULL; n=0; while(p1->num!=0) { n++; if(n==1) { head=p1; } else { p2->next=p1; } p2=p1; p1=(struct student *)malloc(sizeof(struct student)); printf("请输入学生学号、成绩:"); scanf("%d%f",&p1->num,&p1->score); } p2->next=NULL; return head; } struct student *add(struct student *h,int num,float score) { struct student *p1,*p2,*New; p1=h; p2=NULL; while(p1!=NULL&&p1->num next; } New=(struct student *)malloc(sizeof(struct student)); New->num=num; New->score=score; New->next=p1; if(p2==NULL) { h=New; } else { p2->next=New; } return h; } void print(struct student *h) { struct student *p; p=h; if(h!=NULL) { printf("n结果是:"); do{ printf("nt%dt%.2f",p->num,p->score); p=p->next; }while(p!=NULL); } }



