这里我没按小甲鱼的方法,把那几个函数放在一个main里了做了遍新的,以下的代码,亲测可以实现
/*************************************/
//创建一个单向表单,里面包含学号、成绩
//通过一个个输入学号和成绩存在各节点
//如果输入学号为0表示退出建立新节点
//创建完表单后,将对应学号的对应成绩打印
/*************************************/#include<stdio.h>
#include<stdlib.h>
struct stu {
int num;
int grade;
struct stu*next;
};
void main() {
struct stu *p1, *p2, *head=NULL,*temp;
p1 = (struct stu*)malloc(sizeof(struct stu));
printf("Please enter the num:");
scanf("%d", &p1->num);
if (p1->num != 0) {
printf("PLease enter the grade:");
scanf("%d", &p1->grade);
}
p2 = p1;
int n = 0;
while (p1->num != 0) {
n++;
if (n == 1) {
head = p1;
}
p1 = (struct stu*)malloc(sizeof(struct stu));
printf("Please enter the num:");
scanf("%d", &p1->num);
p2->next = &p1->num;
if (p1->num==0) {
break;
}
else {
printf("PLease enter the grade:");
scanf("%d", &p1->grade);
}
p2 = p1;
}
p2->next = NULL;
int i;
if (n == 0) {
printf("无学号输入\n");
}
else {
printf("一共有%d个学生\n", n);
for (i = 1, temp = head; temp != NULL; i++) {
printf("num:%d grade:%d\n", temp->num, temp->grade);
temp=temp->next;
}
}
}
|