您的位置:首页 > 其它

建立和输出一个简单的链表

2014-12-12 11:56 134 查看
#include <stdio.h>

#define NULL 0

struct student

{

long num;

float score;

struct student * next

};

void main()

{

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

a.num = 1001;

a.score = 89.2;

b.num = 1002;

b.score = 90.1;

c.num = 1003;

c.score = 92.1;

head = &a;

a.next = &b;

b.next = &c;

c.next = NULL;

p = head;

do{

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

p = p->next;

}while ( p != NULL);

}

开始时使head指向a结点,a.next指向b节点,b.next指向c节点。这个就是关键的关系。c.next=NULL就是使c.next不指向任何存储单元。在输出链表时要借助p,先使p指向a节点,然后输出a节点中的数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: