建立静态链表(简单)

首先定义一下

#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语言程序设计来打出了这么一个代码,欢迎点个赞,不够好之处欢迎指出来,谢谢。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值