#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
long num;
float score;
struct Student *next;
};
int n;
void print(struct Student *head)
{
printf("\nNow, These %d records are:\n",n);
if(head != NULL)
{
do{
printf("%ld %5.1f\n",head->num,head->score);
head = head->next;
}while(head != NULL);
}
}
以下为课本上的解法,在print函数中添加了 一局部变量, 个人认为没有必要在另加一变量也可完成输出功能,即以上解法
void print(struct Student *head) //输出链表的函数
{
struct Student *p;
printf("\nNow,These %d records are:\n",n); //records记录
p = head; //p指向第1个结点
if (head != NULL) //若不是空表
{
do
{
printf("%ld %5.1f\n", p->num, p->score);
p = p->next;
}while (p != NULL);
}
}