结构体.ppt

上传人:本田雅阁 文档编号:3167472 上传时间:2019-07-19 格式:PPT 页数:48 大小:478.02KB
返回 下载 相关 举报
结构体.ppt_第1页
第1页 / 共48页
结构体.ppt_第2页
第2页 / 共48页
结构体.ppt_第3页
第3页 / 共48页
结构体.ppt_第4页
第4页 / 共48页
结构体.ppt_第5页
第5页 / 共48页
点击查看更多>>
资源描述

《结构体.ppt》由会员分享,可在线阅读,更多相关《结构体.ppt(48页珍藏版)》请在三一文库上搜索。

1、1,第12章 结构体和共用体,2,定义:,将不同种类型的数据有序地组合在一起,构造出一个新的数据类型,这种形式称为结构体。,结构体是多种类型组合的数据类型。,3,struct 结构体名 成员列表 ;,struct student int num; char name20; char sex; char addr30; ;,结构体名,关键字,不同数据类型组成的成员,分号不能少,4,定义结构体类型变量的方法,一、先定义结构体类型再定义变量名,struct student int num; char name20; char sex; int age; float score; char addr3

2、0; ;,struct student student1, student2;,结构体类型名,变量1,变量2,结构体类型只是一种数据类型,不占内存空间,只有定义结构体类型变量时才开辟内存空间。,5,# define STUDENT struct student STUDENT int num; char name20; char sex; int age; float score; char addr30; ; STUDENT student1,student2;,凡是STUDENT的地方都用struct student 机械替换。,6,二、在定义类型的同时定义变量,struct studen

3、t int num; char name20; char sex; int age; float score; char addr30; student1, student2;,struct 结构体名 成员列表 变量名列表;,紧接着定义变量,7,三、直接定义结构体类型变量,struct int num; char name20; char sex; int age; float score; char addr30; student1, student2;,struct 成员列表 变量名列表;,不出现结构体名。,8,2、在编译时,仅对变量分配空间,不对类型分配空间。,1、结构体类型的变量在内存

4、依照其成员的顺序顺序排列,所占内存空间的大小是其全体成员所占空间的总和。,3、对结构体中各个成员可以单独引用、赋值,其作用与变量等同。,格式:变量名 . 成员名 student1 . num,9,4、结构体的成员可以是另一个结构体类型。,struct date int month; int day; int year; ;,struct student int num; char name20; struct date birthday; ;,成员类型,成员名,5、成员名可以与程序中的变量名相同,二者分占不同的内存单元,互不干扰。例如,在程序中仍可以定义变量 int num;,10,结构体类型

5、变量的引用,1、不能对结构体变量整体赋值或输出,只能分别对各个成员引用。,cinstudent1;,cinstudent1.num; student1.num=100;,可以将一个结构体变量整体赋给另外一个相同类型的结构体变量。 student2=student1;,2、嵌套的结构体变量必须逐层引用。,student1.birthday.day=25;,3、结构体变量中的成员可以同一般变量一样进行运算。,student1.birthday.day+; student1.score+=60;,错误,必须用成员名引用,11,对局部变量类型的结构体变量初始化,void main(void) stru

6、ct student long int num; char name20; char sex; char addr30; student1=901031, “Li Lin”, M, “123 Beijing Road”; coutstudent1.nameendl;,输出: LiLin,对变量初始化,一一赋值,12,关于结构类型变量的使用,说明以下几点: 1、同类型的结构体变量之间可以直接赋值。这种赋值等同于各个成员的依次赋值。 2、结构体变量不能直接进行输入输出,它的每一个成员能否直接进行输入输出,取决于其成员的类型,若是基本类型或是字符数组,则可以直接输入输出。 3、结构体变量可以作为函数

7、的参数,函数也可以返回结构体的值。当函数的形参与实参为结构体类型的变量时,这种结合方式属于值调用方式,即属于值传递。(举例说明),13,结构体数组,结构体数组中的每个元素都是一个结构体类型的变量,其中包括该类型的各个成员。数组各元素在内存中连续存放。,14,一、结构体数组的定义,struct student int num; char name20; char sex; int age; float score; char addr30; ; struct student stu30;,struct student int num; char name20; char sex; int age

8、; float score; char addr30; stu30;,直接定义,15,二、结构体数组的初始化,struct student int num; char name20; char sex; stu3= 1011, “Li Lin“,M, 1012,“Wang Lan“,F, 1013,“Liu Fang“,F;,16,struct student int num; char name20; char sex; stu = 1011,“Li Lin“,M, 1012,“Wang Lan“,F, 1013,“Liu Fang“,F;,17,以下程序的结果是:,void main(vo

9、id) struct date int year, month, day; today; coutsizeof(struct date)endl; ,12,18,根据下面的定义,能打印出字母M的语句是:,struct person char name9; int age; ; struct person class10= “Jone”,17, “Paul”,19, “Mary”,18, “Adam”,16 ; coutclass3.nameendl; coutclass3.name1endl; coutclass2.name1endl; coutclass2.name0endl;,输出:Ada

10、m,输出:d,输出:a,输出:M,19,用指针处理链表,一、链表概述,链表是由一个个结点组成,每一个结点是一个结构体类型的变量,各个结点的类型相同,但其地址不一定连续。具体结点的个数根据需要动态开辟。,每个结点由两部分组成,第一部分放若干数据,第二部分是指针变量,放下一结点的地址。链表头是一指针变量,放第一个结点的地址,若结点的第二部分的值为NULL,表示此链表结束。,20,二、如何处理链表,struct student int num; float score; struct student *next; ;,#define STU struct student STU int num; f

11、loat score; STU *next; ;,链表结点的结构:,指向同一结构体类型的指针变量,指向同一结构体类型的指针变量,1、建立链表,21,struct student int num; float score; struct student *next; ;,struct student *p; /定义了结构体类型的指针 p=new student; /用new开辟一结构体空间,将地址赋给p p-num=10; /为新开辟的结构体空间中的num成员赋值 p-score=90;,10,90,用指针引用结构体内的成员,(*p).num,22,1、首先定义两个结构体类型的指针 STU *p

12、1, *p2;,2、用new在内存中开辟一个结构体变量的空间,将地址赋给p1。,p1=new student; /* STU struct student */,3、将数据赋给刚开辟的变量空间。,cinp1-nump1-score;,4、若输入的数据有效,将首地址作为链表头,head=p1; 令p2=p1,p1继续用new开辟新的内存空间。,5、将下一个数据赋给新开辟的变量空间。,p1=new student; /* STU struct student */,cinp1-nump1-score;,6、若输入的数据有效,将p2与p1连接起来,p2-next=p1 再令p2=p1,p1继续用ne

13、w开辟新的内存空间。做5。若输入的数据无效,p2就是链表的尾,则p2-next=NULL。,0,A,B,C,23,0,A,B,C,24,STU *p1, *p2, *head; head=NULL; p1=p2=new student; cinp1-nump1-score; if (p1-num!=0) head=p1;,p1=new student; cinp1-nump1-score; if (p1-num!=0) p2-next=p1; p2=p1;,p1=new student; cinp1-nump1-score; if (p1-num!=0) p2-next=p1; p2=p1;

14、,p1=new student; cinp1-nump1-score; if (p1-num= =0) p2-next=NULL; return (head);,第一结点,第二结点,第三结点,最后结点,返回链表头,25,STU *creat( ) STU *head, *p1,*p2; n=0; head=NULL; p1=p2=new student; cinp1-nump1-score; while (p1-num!=0) n=n+1; if (n= =1) head=p1; else p2-next=p1; p2=p1; p1=new student; cinp1-nump1-score

15、; p2-next=NULL; return(head); ,n为全局变量,表示结点数,开辟新结点,向新结点输入数据,不满足输入条件,结束,26,2、输出链表,void print(STU * head) STU *p; p=head; while(p!=NULL) coutnumscorenext; ,2000H,3000H,3050H,6000H,2090H,输出数据,p指向下一结点,27,3、删除链表,2000H,3000H,6000H,2090H,head,1、首先定义两个结构体类型的指针 STU *p1, *p2;,2、将链表的表头赋给p1, p1=head;,3、判断p1所指向的结

16、点是否是要删除的结点 p1-num a1。,4、若p1-num!=a1, p2=p1; p1指向下一个结点p1=p1-next,继续判断下一个结点是否是要删除的结点。继续做3。,5、若p1-num= =a1,则p1当前指向的结点就是要删除的结点,将p2的指针成员指向p1所指的下一个结点。,p2-next=p1-next;,这样就删除了一个结点。,28,特殊情况:,1、若链表为空链表,返回空指针。,2、删除的结点为头结点时,head指向下一个结点,3、链表内没有要删除的结点,返回提示信息。,29,struct student *del(struct student * head, int num

17、) struct student *p1,*p2; if (head= =NULL) coutnum ,链表为空,未找到结点,循环,结点为第一个,循环结束,没有要找的结点,找到结点,30,4、插入结点:要插入结点的链表是排序的链表。插入10。,2000H,3000H,6000H,2090H,head,1、定义三个结构体指针变量 STU *p1,*p2,*p0; p0指向要插入的结点。p1=head;,7000H,p0,2、比较p1-num 与p0-num,若p1-numnum,p2=p1; p1=p1-next; 继续比较。,3、若p1-num =p0-num,p0应插在p1与p2之间,则p2

18、-next=p0 ; p0-next=p1;,31,特殊情况:,1、若链表为空链表,将插入结点作为唯一的结点,head=p0;返回。,2、若插入结点中的数据最小,则插入的结点作为头结点。,3、插入到链尾,插入结点为最后一个结点。,p2-next=p0; p0-next=NULL;,p0-next=head; head=p0;,32,STU *insert(STU * head, STU * stud) STU *p0,*p1,*p2; p1=head; p0=stud; if (head= =NULL) head=p0; p0-next=NULL; else while(p0-nump1-nu

19、m) ,链表为空,未找到结点,循环,找到结点,插入在第一个结点前,插入在最后一个后,33,void main(void) STU *head, stu; int del_num; head=creat( ); print(head); coutdel_num; head=del(head,del_num); print(head); coutstu.numstu.score; head=insert(head, ,建立链表,打印链表,删除结点,插入结点,coutstu.numstu.score; head=insert(head, ,变量,固定空间,34,2000H,3000H,6000H,2

20、090H,head,7000H,stu,7000,6000H,35,void main(void) STU *head,*stu; int num; coutstu-numstu-score; while (stu-num!=0) head=insert(head, stu); print(head); coutstu-numstu-score; ,如果要插入结点,动态开辟新结点,指针,可以赋值,重新开辟空间,36,共用体,C语言中,允许不同的数据类型使用同一存储区域,即同一存储区域由不同类型的变量共同表示。这种数据类型就是共用体。,union 共用体名 成员表列; 变量表列;,union d

21、ata int i; char ch; float f; a, b, c;,union data a, b, c;,这几个成员在共用体变量中存放在同一地址,相互覆盖,其长度为最长的成员的长度。,37,共用体变量的引用,不能整体引用共用体变量,只能引用变量中的成员。,a.i 表示为整型 a.ch 表示为字符型 a.f 表示为符点型,38,共用体变量的特点,1、共用体的空间在某一时刻只有一个成员在起作用。,2、共用体变量中的成员是最后一次放入的成员。,3、共用体变量不能在定义时赋初值。,4、共用体变量不能作为函数的参数或函数值,但可使用指向共用体的指针变量。,5、共用体可以作为结构的成员,结构体也

22、可以作为共用体的成员。,39,union un int i; double y; ; struct st char a10; union un b; ; coutsizeof(struct st)endl;,18,40,union un short int a; char c2; w; w.c0=A; w.c1=a; coutoctw.aendl;,低字节低地址 高字节高地址,a,A,w.c1,w.c0,2000H,2001H,输出:060501,a,65 ?,56 ?,41,void main(void) union EXAMPLE struct int x, int y; in; int

23、a,b; e; e.a=1; e.b=2; e.in.x=e.a*e.a; e.in.y=e.b+e.b; coute.in.xte.in.yendl; ,in,x,y,a,b,b,1,2,4,8,输出:4 8,42,枚举类型,如果一个变量只有几种可能的值,可以定义为枚举类型。,枚举类型就是将变量的值一一列举出来,变量的值仅限于列举出来的值的范围内。,43,enum weekday sun, mon, tue, wed, thu, fri, sat;,enum weekday workday, weekend ;,workday 和 weekend 值只能是sun 到 sat 其中之一。,en

24、um sun, mon, tue, wed, thu, fri, sat workday, weekend ;,其中sun, mon,sat称为枚举元素或枚举常量,为用户定义的标识符,所代表的意义由用户决定,在程序中体现出来。,数据类型,可能取的值,变量,另一种定义变量的方法,44,1、枚举元素为常量,不可赋值运算。 sun=0; mon=1;,2、在定义枚举类型的同时,编译程序按顺序给每个枚举元素一个对应的序号,序号从0开始,后续元素依次加1。,enum weekday sun, mon, tue, wed, thu, fri, sat; 0 , 1, 2, 3, 4, 5, 6,3、可以在

25、定义时人为指定枚举元素的序号值。,enum weekday sun=9, mon=2, tue, wed, thu, fri, sat; 9 , 2, 3, 4, 5, 6 , 7,4、只能给枚举变量赋枚举值,若赋序号值必须进行强制类型转换。,day=mon ; day=1; day=(enum weekday)1;,45,5、枚举元素可以用来进行比较判断。,if (workday= = mon) if (workdaysun),6、枚举值可以进行加减一个整数n的运算,得到其前后第n个元素的值。,workday=sun; workday=(week)(workday+2);,workday=

26、= tue,7、枚举值可以按整型输出其序号值。,workday=tue; coutworkday;,2,46,void main(void) enum team qiaut, cubs=4, pick, dodger=qiaut-2; coutqiauttcubst; coutpicktdodgerendl; ,输出:0,4,5,-2,47,用typedef定义类型,typedef定义新的类型来代替已有的类型。,typedef 已定义的类型 新的类型,typedef float REAL,REAL x, y;,1、typedef可以定义类型,但不能定义变量。,2、typedef只能对已经存在的

27、类型名重新定义一个类型名,而不能创建一个新的类型名。,typedef struct student int i; int *p; REC;,REC x, y, *pt;,struct student x, y, *pt;,float x, y;,48,typedef char *CHARP; CHARP p1,p2;,char *p1, *p2;,typedef char STRING81; STRING s1, s2, s3;,char s181, s281,s381;,1、先按定义变量的方法写出定义体,char s81;,2、把变量名换成新类型名,char STRING81;,3、在前面加typedef,typedef char STRING81;,4、再用新类型名定义变量,STRING s;,#define REAL float,编译前简单替换,typedef:编译时处理,定义一个类型替代原有的类型。,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1