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

* * * * * * * * * * * * * * * * * * * * * 9-4-5链表(含头结点)操作处理 插入某个结点 //在链表的头结点后插入由ptr指向的结点 void inserth( struct student * head, struct student *ptr ) { if ( ptr == NULL ) return; ptr-next = head-next; head-next = ptr; return; } 检查 插入结点 是否存在 head 哨兵 ptr 10107 85 NULL 9-4-5链表(含头结点)操作处理 插入某个结点 //在链表的头结点后插入由ptr指向的结点 void inserth( struct student * head, struct student *ptr ) { if ( ptr == NULL ) return; ptr-next = head-next; head-next = ptr; return; } 检查 插入结点 是否存在 head 哨兵 10107 85 NULL ptr 10103 90 如何在“表尾”之后增加结点? //生成一个结点,并由用户输入结点数据 //返回指向头结点的指针 //如果分配内存不成功,返回空指针 struct student * create_node( void ) { struct student *p; p = (struct student *) malloc( LEN ); if ( p != NULL ) { printf(No.= ); scanf(%d, p-num); printf(score= ); scanf(%f,p-score); p-next= NULL; } return ( p ); } 9-4-5链表(含头结点)操作处理 生成一个结点 这是一个独立的结点,不指向其他结点。 //遍历链表,输出各个结点的数据 void list(struct student *head) { struct student *p; p = head-next; while ( p != NULL ) { print_node( p ); p = p-next; }; return; } 可以换成对链表结点数据的其他操作 9-4-5链表(含头结点)操作处理 遍历链表输出每个结点的数据 //输出指定结点的数据 void print_node( struct student * ptr ) { if ( ptr == NULL ) printf(“...NULL\n”); //考虑指针为空的情形 else { printf(num=%d, ptr-num); printf(,score=%f\n, ptr-score); } return; } 9-4-5链表(含头结点)操作处理 遍历链表输出每个结点的数据 int main(int argc, char* argv[]) { struct student *head,*pt; head = create(); pt = create_node(); inserth(head,pt); pt = create_node(); inserth(head,pt); pt = create_node(); inserth(head,pt); list(head); return 0; } 链表_例3 利用上述函数,对含头结点的链表进行有关操作 (实现例2类似功能) //查找指定的结点(该结点的某个成员数据为指定值) //返回指向第一个满足条件结点的指针 //如果没有找到,则返回空指针 struct student * find(struct student * head, int num) { struct student *p; p = head-next; while ( p != NULL ) { if ( p-num == num ) break; p = p-next; } return (p); } 找到, 跳出循环 9-4-5链表(含头结点)操作处理 查找某个结点 指定成员的值 找到,返回指向结点的指针; 否则,返回空指针。 //删除指定的结点(该结点的某个成员数据为指定值) //释放所删除结点占用的内存空间 void del_node(struct student * head, int num) { struct student *p,*pre;

文档评论(0)

文档精品 + 关注
实名认证
内容提供者

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

版权声明书
用户编号:6203200221000001

1亿VIP精品文档

相关文档