第08章结构体和共用体.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第08章结构体和共用体

第八章 主要内容 8.1 定义结构体类型变量的方法 8.2 结构体变量的引用 8.3 结构体变量的初始化 8.4 结构体数组 8.5 指向结构体类型数据的指针 8.6 用指针处理链表 8.7 共用体 8.8 枚举类型 8.9 用typedef定义类型 8.1 定义结构体类型变量的方法 (4) 成员也可以是一个结构体变量 例如:struct date { int month; int day; int year;     }; 或:struct student {int mum; char name[20]; char sex; int age; float score; struct date {int year; int month; int day; }birthday; }student1; 例:struct student {int mum; char name[20]; char sex; int age; float score; struct date {int year; int month; int day; }brithday; }stu1; 练习: struct stu {int x; int *y; }*p; int dt[3]={10,20,30,40}; struct stu a[3]={{50,dt[0]},{60,dt[1]},{70,dt[2]}}; main() {p=a; printf(“%d\n”,++(p-x)); } struct worker{ int no; char name[20]; char sex; struct birth{ int day; int month; int year; }a; }; struct worker w; main() { ……. } struct student *creat(void) {struct student *head *p1,*p2; n=0; p1=p2=(struct student*)malloc(LEN); scanf(“%ld,%f”,p1.num,p1.score); head=NULL; while(p1-num!=0) { n=n+1; if(n==1) head=p1; else p2-next=p1; p2=p1 P1=(struct student *)malloc(LEN); scanf(“%ld,%f”,p1-num,p1-score); } P2-next=NULL; return(head); } 8.6 用指针处理链表 注意: ②如果要删除的不是第一个结点, 则将p1-next赋给p2-next,p2-next原来指向p1指向的结点(图中第二个结点),现在p2-next改为指向p1-next所指向的结点(图中第三个结点)。p1所指向的结点不再是链表的一部分。   还需要考虑链表是空表(无结点)和链表中找不到要删除的结点的情况。 图11-20 8.6 用指针处理链表 图11-20 8.6 用指针处理链表 算法: 图11-21 8.6 用指针处理链表 删除结点的函数del: struct student *del(struct student *head,long num) {struct student *p1,*p2; if (head==NULL){printf(\nlist null!\n);goto end;} p1=head; while(num!=p1-num p1-next!=NULL) {p2=p1;p1=p1-next;} if(num==p1-num) {if(p1==head) head=p1-next; else p2-next=p1-next; printf(delete:%ld\n,num); n=n-1; } else printf(%ld not been found!\

文档评论(0)

l215322 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档