测试代码:
#include <stdio.h>
#include <stdlib.h>
int main() {
struct student {
char *name;
int score;
struct student *next;
};
struct student *p, *q, *r, *current;
p = (struct student *)malloc(sizeof (struct student));
q = (struct student *)malloc(sizeof (struct student));
r = (struct student *)malloc(sizeof (struct student));
p->name = "Bright";
p->score = 98;
q->name = "Linda";
q->score = 95;
r->name = "Jenny";
r->score = 95;
p->next = q;
q->next = r;
r->next = NULL;
current = p;
printf("Students:\n");
while (current != NULL) {
printf("%10s %10d\n", current->name, current->score);
current = current->next;
}
free(p);
free(q);
free(r);
return 0;
}
=== 测试结果:
Students:
Bright 98
Linda 95
Jenny 95