第一范文网 - 专业文章范例文档资料分享平台

C语言典型例题(二级考试) - 图文

来源:用户分享 时间:2025/5/16 7:45:53 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

例25 删除指定的节点

struct student *del(struct student *head, long n){structstudent*p1,*p2;/*↑n:要删除学号*/p1=head;

if(p1->num==n)head=p1->next;/*删除首节点*/else

{do{p2=p1;p1=p1->next;

}while(p1!=NULL&&p1->num!=n);if(p1->num==n)p2->next=p1->next;/*找到*/elseprintf(\befound!\\n\/*未找到*/}

free(p1);/*释放被删除节点的存储区*/return(head);/*返回头指针*/}

例26 在链表中插入节点的函数:

structstudent*insert(structstudent*head)

{structstudent*p0,*p1,*p2;longn;intlen;

len=sizeof(structstudent);

p0=(structstudent*)malloc(len);/*申请新节点*/printf(\

scanf(\&p0->num,&p0->score);n=p0->num;/*产生学号副本n*/p1=head;/*从首节点开始查找*/

┇p1=head;/*↓插入在头部*/if(nnum) { p0->next=head; head=p0; }else

{ do /*查找插入位置*/{ p2=p1;

p1=p1->next;

} while(p1!=NULL && n>p1->num);

p0->next=p2->next; /*插入在其余位置*/p2->next=p0;}

return(head);

} /*insert*/

搜索更多关于: C语言典型例题(二级考试) - 图文 的文档
C语言典型例题(二级考试) - 图文.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1351s3xd0t4i6jp0x0ei_11.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top