建立简单的静态链表,它由3个学生数据的结点组成,要求输出各结点的数据。

解:将第1个结点的起始地址赋给头指针head,将第2个结点的起始地址赋给第1个结点的next成员,将第3个结点的起始地址赋给第2个结点的next成员。第3个结点的next成员赋予NULL,这就形成了链表。为了建立链表,使head指向a结点,a.next指向b结点,b.next指向c结点,c.next=NULL的作用是使c.next不指向任何有用的存储单元。

程序:

#include<stdio.h>

struct Student

{

int num;

float score;

struct Student *next;

};

int main()

{

struct Student a, b, c, *head, *p;

a.num = 10101; a.score = 89.5;

b.num = 10103; b.score = 90;

c.num = 10107; c.score = 85;

head = &a;

a.next = &b;

b.next = &c;

c.next = NULL;

p = head;

do

{

printf("num=%ld   score=%5.1f\n", p->num, p->score);

p = p->next;

} while (p != NULL);

return 0;

}

结果:

num=10101   score= 89.5

num=10103   score= 90.0

num=10107   score= 85.0

请按任意键继续. . .