- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
C语言程序设计教程-链表.ppt
* * 链表 1、链表概述 可以设计这样一个结构体类型 struct student {int num; float score; struct student *next; }; 2、简单链表 例如:建立一个如图所示的简单链表,它由3个学生数据的结点组成。输出各结点中的数据。 #define null 0 struct student {long num; float score; struct student *next;}; void main( ) {struct student a,b,c,*head,*p; a.num=99101;a.score=89.5; b.num=99103;b.score=90; c.num=99107;c.score=85; head=a; a.next=b; b.next=c; c.next=null; p=head; do {coutp-nump-score; p=p-next; } while(p!=null); } 3、处理动态链表所需的函数 (1)malloc函数 其原型为:void *malloc(unsigned int size); 作用:在内存的动态存储区中分配一个长度为size 的连续空间。此函数的值(即“返回值”)是一个指向分配域起始地址的指针(类型为void)如果此函数未能成功地执行(如内存空间不足),则返回空指针null. 2、calloc函数 其原型为:void *calloc(unsigned n,unsigned size); 作用:在内存的动态区存储中分配n个长度为size的连续空间。函数返回一个指向分配域起始地址的指针;如果分配不成功,返回null. 3、free函数 其函数原型为:void free(void *p); 作用:释放由p指向的内存区,使这部分内存区能被其他变量使用。p是最近一次调用calloc或malloc函数时返回的值。free函数无返回值。 4、建立动态链表 :指在执行过程中从无到有的建立起一个链表,即一个一个的开辟结点和输入各结点数据,并建立起前后相链的关系。 例:写一个函数建立一个有3名学生数据的单向动态链表。 #include malloc.h #define null 0 #define len sizeof(struct student) struct student {long num; float score; struct student *next; }; int n; struct student *creat(void) {struct student *head; struct student *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); } 5、输出链表 void print(struct student *head) {struct student *p; p=head; if(head!=null) do {printf(“%ld%f\n”,p-num,p-score); p=p-next; }while(p!=null); } 6、对链表的删除操作 struct student *del(student *head,long num) {struct student *p1,*p2; if(head==null) cout“nlist null!”endl; p1=hend; while(num!=p1-nump1-next!=null) //p1指向的不是所要找的结点,并且后面还有结点 {p2=p1;p1=p1-next;}//p1后移一个结点 if(num==p1-num)//找到了 {if(p1==head) head=p1-next; //若p1指向的是首结点,把第二个结点地址赋予head else p2-next=p1-next; //否则将下一个结点地址赋给前一结点地址 printf(“ld”,num); n=n-1;} else cout“not been foubd!”numendl; return(head); } 7、对链表的插入操作 struct dtudent *insert(s
原创力文档


文档评论(0)