首先定义一下
#include<stdio.h>
#include<string.h>
struct link /*声明结构体类型sruxct link*/
{
int num;
char name[20];
float score;
struct link *next; /*定义struct link类型的指针变量next*/
};
然后主函数里赋值
struct link a,b,c; /*定义3个结构体变量作为链表的结点*/
struct link *head,*p; /*定义两个struct link 类型的指针变量*/
a.num=10001;strcpy(a.name,"zhang");a.score=91;
b.num=10002;strcpy(b.name,"wang");b.score=92;
c.num=10003;strcpy(c.name,"guo");c.score=93;
接着指向的地址赋值;
head=&a; /*将结点a的起始地址赋给头指针head*/
a.next=&b; /*将结点b的起始地址赋给a结点的next成员*/
b.next=&c; /*将结点c的起始地址赋给b结点的next成员*/
c.next=NULL; /*c结点的next成员不存在其他结点地址*/
p=head; /*使p指向第一个结点*/
最后循环输出
do
{
printf("%5d %-10s %-.1f\n",p->num,p->name,p->score);
/*输出p指向的结点的数据*/
p=p->next; /*使p指向下一个结点*/
}while(p!=NULL); /*输出完c结点后的p的值为NULL,循环终止*/
完整代码如下;
#include<stdio.h>
#include<string.h>
struct link /*声明结构体类型sruxct link*/
{
int num;
char name[20];
float score;
struct link *next; /*定义struct link类型的指针变量next*/
};
int main()
{
struct link a,b,c; /*定义3个结构体变量作为链表的结点*/
struct link *head,*p; /*定义两个struct link 类型的指针变量*/
a.num=10001;strcpy(a.name,"zhang");a.score=91;
b.num=10002;strcpy(b.name,"wang");b.score=92;
c.num=10003;strcpy(c.name,"sun");c.score=85;
head=&a; /*将结点a的起始地址赋给头指针head*/
a.next=&b; /*将结点b的起始地址赋给a结点的next成员*/
b.next=&c; /*将结点c的起始地址赋给b结点的next成员*/
c.next=NULL; /*c结点的next成员不存在其他结点地址*/
p=head; /*使p指向第一个结点*/
do
{
printf("%5d %-10s %-.1f\n",p->num,p->name,p->score);
/*输出p指向的结点的数据*/
p=p->next; /*使p指向下一个结点*/
}while(p!=NULL); /*输出完c结点后的p的值为NULL,循环终止*/
return 0;
}
这是程序运行结果
10001 zhang 91.0
10002 wang 92.0
10003 sun 85.0
小白是借鉴C语言程序设计来打出了这么一个代码,欢迎点个赞,不够好之处欢迎指出来,谢谢。