2011-11-1结构体与链表课件

上传人:我*** 文档编号:141021677 上传时间:2020-08-03 格式:PPT 页数:54 大小:868KB
返回 下载 相关 举报
2011-11-1结构体与链表课件_第1页
第1页 / 共54页
2011-11-1结构体与链表课件_第2页
第2页 / 共54页
2011-11-1结构体与链表课件_第3页
第3页 / 共54页
2011-11-1结构体与链表课件_第4页
第4页 / 共54页
2011-11-1结构体与链表课件_第5页
第5页 / 共54页
点击查看更多>>
资源描述

《2011-11-1结构体与链表课件》由会员分享,可在线阅读,更多相关《2011-11-1结构体与链表课件(54页珍藏版)》请在金锄头文库上搜索。

1、结构体是C语言中的另外一种构造型数据,它 是由不同类型的数据项组成的复合类型。结构体 是建造动态数据结构非常有用的工具。 如,由结构体类型的数据所构成的链表等。 本章将介绍结构体类型的定义、引用和结构 体数组、结构体指针以及由结构体所构成的链表,第11章 结构体与链表,第11章 结构体与共用体 11.1 定义结构体类型和定义结构体变量 结构体变量的引用 结构体变量的初始化 11.2 结构体数组 11.3 指针与结构体 * 11.4 用指针处理链表 11.5 共用体 11.6 枚举类型 11.7 用typedef定义类型,学号 姓名 性别 年龄 成绩 电 话,结构体类型的基本概念,int,cha

2、r,float,struct stu int num; char name20; int age; float score; long int tele; a;,结 构 体,long int,一、结构体类型变量的定义,struct 类型名 成员表列; 变量名;,格式:,struct stu int num; char name10; float score; a;,结构体类型所占内存的字节数=所有成员的字节总和,16,11.1 结构体类型的基本操作,num name score,2000,2002,200C,a,二、结构体类型变量的引用,变量名.成员名,struct stu int num;

3、char name10; float score; a;,scanf(“%d,%s,%f”, ,printf(“%d,%s,%fn”, a.num,a.name,a.score);,三、结构体变量的初始化,struct stu int num; char name10; float score; a=101, “王一”,68.5 ;,num name score,一个结构体变量中可以存放一组数据(如一个学生的学 号、姓名、成绩等数据)。若要处理一批这样的数据时,就 要用到结构体数组。,11.2 结构体数组,a0,a0.num,struct stu int num; char name10; f

4、loat score; a3;,a0.name,a0.score,a1,a2,11.2.1 结构体数组的定义,a0,struct stu int num; char name20; int age; float score; long int tele ; a3,a1,a2,= 10010, “王一”, 20, 98.5, 8802766 , 10011, “李雨”, 19, 67.8, 8802765 , 10012, “欧杨”, 20, 88.5, 8802769 , ;,11.2.2 结构体数组变量的初始化,a0,a1,a2, for(i=0;i3;i+) scanf(“%d,%s,%d

5、,%f %ld”, ,11.2.3引用,一个结构体变量的指针就是该变量所占据内存段的 起始地址可以设一个指针变量,用来指向一个结构体变 量,此时该指针变量的值就是该结构体变量的起始地址 。指针变量也可以用来指向结构体数组中的元素。,11.3 指向结构体类型数据的指针,struct stu int num; char name10; float score; a=101, “Li”,68.5;,p=,printf(“%d, %s, %f n”,(*p).num, (*p).name, (*p).score );,p- num, p- name, p- score );,struct stu *p

6、;,a,*p,一、指向结构体变量的指针,p=a;,二、指向结构体数组的指针,a0,a1,a2,printf(“%d,%s,%f n”, (*p).num, (*p).name, (*p).score);,struct stu *p;,for(;pa+3;p+),struct stu int num; char name10; float score; a3= 101, “Li”,68.5, 102, “Wang”,67.8, 103, “Ma”,88.5;,*p,*p,*p,9.4 用指针处理链表,链表是指将若干个数据项按一定的原则连接起来的表。链 表中每一个数据称为节点。链表连接的原则是:

7、前一个节点指 向下一个节点; 而且只有通过前一个节点才能找到下一个节点。 链表是一种常见的重要的数据结构。利用它可以实现动态 地进行存储分配。,11.4 链表,1249,*h,11.4 用指针处理链表,1094,1021,头指针,单向链表结构,空地址,1094,1021,NULL,struct stu int num; float score; a,b,c;,一、简单链表的建立与输出,struct stu *next;,struct stu *h;,a.num=101;,a,b,c,101,a.score=98.5;,98.5,b.num=102;,b.score=67.8;,102,67.8

8、,c.num=103;,c.score=88.5;,103,88.5,struct stu int num; float score; a,b,c;,struct stu *next;,struct stu *h;,a.num=101;,a,b,c,101,a.score=98.5;,98.5,b.num=102;,b.score=67.8;,102,67.8,c.num=103;,c.score=88.5;,103,88.5,h=,*h,a.next=,b.next=,c.next=NULL;,NULL,struct stu *p;,a,b,c,101,98.5,102,67.8,103,8

9、8.5,*h,NULL,*p,p=h;,for(; ),p=(*p).next,printf(“%d,%fn”, (*p).num,(*p).score);,p!=NULL;,*p,*p,二、动态链表的建立与输出,所谓动态链表的建立是指在程序执行过程中从无到有 地建立一个链表,即一个一个地开辟节点和输入各节点数 据,并建立起前后相链的关系。,动态分配存储空间函数,int *p1; p1=malloc(2);,p1=malloc(int);,p1=malloc(float);,malloc( ),*p1,1、内存分配函数,格式:malloc(size),功能:请求分配连续size个存储位置的区域

10、。,说明: size 表示是申请内存的字节数量。,int *p; p=malloc(2);,07B6 07B7,*p=100;,内,存,动,态,函,数,*p,100,2、释放内存函数,格式:free(指针变量),功能:将由malloc函数分配的存储单元释放出来。,main() int *p; p=malloc(8); strcpy(p, “student”); printf(“%s”, p);,free(p); ,*p,3、计算字节数量函数,格式:sizeof (数据类型标识符),功能:其函数值为该类型变量在内存中所占的字节数。,main() int a, b10 ; float c6 ; p

11、rintf(“%dn”, );,2,sizeof(int),20,printf(“%dn”, sizeof(c); ,printf(“%dn”, sizeof(a);,2,printf(“%dn”, sizeof(b);,24,p1=(struct *)malloc( sizeof(struct stu) );,struct stu int num; float score; struct stu *next; *p1;,p1=(float *)malloc(sizeof (float);,为强制类型转换。即将由malloc 函数得到的地址值转换成一个浮点类型存储单元的地址值。,为字节数运算符

12、,在此是用来说明该指针变量将要开辟的是非字符类型的动态内存。,*p1,*h,*p1,struct stu int num; float score; struct stu *next; *h,*p1,*p2;,p1=(struct stu *)malloc(sizeof(struct stu);,h=p1;,p1=(struct stu *)malloc(sizeof(struct stu);,*p1,h.next=p1;,p2=p1;,*p2,p1=(struct stu *)malloc(sizeof(struct stu);,*p1,p2.next=p1;,p2=p1;,*p2,*h,*

13、p1,struct stu int num; float score; struct stu *next; *h,*p1,*p2;,NULL,main() int i;,p2=p1=(struct stu *)malloc(sizeof(struct stu);,p2=p1;,*p2,*p2,for(i=1;i2;i+), p2-next=p1;,scanf(“%d,%fn”, ,h=p1;,scanf(“%d,%fn”, ,p2-next=NULL;,*h,*p1,struct stu int num; float score; struct stu *next; *h,*p1,*p2;,N

14、ULL,main() ,*p2,p1=h;,printf(“%d,%f n”, p1-num,p1-score);,p1=p1-next;,*p1,while(p!=NULL),*h,NULL,三、动态链表的插入操作,动态链表的插入操作是指将一个节点插入到一个已有的链表中。,*p1,struct stu int num; float score; struct stu *next; *h,*p1,*p2;,NULL,main() ,*p2,scanf(“%d,%f”, ,p2=p1;,p0=,struct stu *p0, x;,x,*p2,*p0,while(p0-scorep1-score

15、),p1=p1-next; ,p2-next=p0;,*p0-next=p1;,*p1,*p2,*p1,*h,若有以下定义:,struct link int data; struct link *next; a,b,c,*p,*q;,a,b,c,能将节点c插入到上述链表结构中的a与b之 间,以形成新的链表结构的语句组是:,A) a.next=c; c.next=b; B) p.next=q; q.next=p.next; C) p-.next=,EXERCISES11-2:,*p,*q,若已建立下面的链表结构,指针 p、s 分别指向图中所示的 节点,则不能将 s所指的节点插入到链表末尾的语句组是:,A) s-next=NULL; p=p-next; p-next=s; B) p=p-next; s-next=p-next; p-next=s; C) p=p-next; s-next=p; p-next=s; D) p=(*p).next; (*s).next=(*p).next; (

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号