第9章用户建立的数据类型2.ppt
《第9章用户建立的数据类型2.ppt》由会员分享,可在线阅读,更多相关《第9章用户建立的数据类型2.ppt(15页珍藏版)》请在三一文库上搜索。
1、,第9章,用户建立的数据类型,9.4 用指针处理链表,9.4.1 链表概述 链表是一种常见的重要的数据结构,是动 态地进行存储分配的一种结构。 链表的组成: 头指针:存放一个地址,该地址指向一个元素 结点:用户需要的实际数据和链接节点的指针,图11-10,用结构体建立链表的结点(例): struct student int num; float score; struct student *next ;; 其中成员num和score用来存放结点中的有用数据(用 户需要用到的数据),next是指针类型的成员,它指 向struct student类型数据。,图9-11,9.4.2 建立简单的静态链
2、表,例 9.7 建立有3个结点的简单链表,并输出各结点中的数据.,#include #define NULL 0 struct student int num; float score; struct student *next; ; void main(), struct student a,b,c,*head,*p;,p=head;,a. num=99101; a.score=89.5; b. num=99103; b.score=90; c. num=99107; c.score=85;,head=,do printf(“%d %5.1fn“,p-num,p-score); p=p-ne
3、xt; while(p!=NULL); ,9.4.3 建立动态链表,所谓建立动态链表是指在程序执行过程中从无 有地建立起一个链表,即一个一个地开辟结点和 输入各结点数据,并建立起前后相链的关系。 例9.8 写一函数建立一个有3名学生数据的单向动态链表。,建立链表的函数如下: #include #include #define NULL 0 /令NULL代表,用它表示“空地址 #define LEN sizeof(struct student) /令LEN代表struct /student类型数据的长度 struct student int num; float score; struct st
4、udent *next; ; int n; /n为全局变量,本文件模块中各函数均可使用它,struct student *creat( ) struct student *head,*p1,*p2; n=0; p1=p2=(struct student*)malloc(LEN); scanf(“%d%f”,head=NULL; while(p1-num!=0)/学号为0建链表结束 n=n+1; if(n=1)head=p1; else p2-next=p1;/前后结点连接起来 p2=p1; p1=(struct student*)malloc(LEN);/新结点 scanf(“%d%f”, ,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 用户 建立 数据类型
链接地址:https://www.31doc.com/p-2258238.html