面向对象程序设计(二).ppt
上传人:天马****23 上传时间:2024-09-11 格式:PPT 页数:30 大小:247KB 金币:10 举报 版权申诉
预览加载中,请您耐心等待几秒...

面向对象程序设计(二).ppt

面向对象程序设计(二).ppt

预览

免费试读已结束,剩余 20 页请下载文档后查看

10 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

补充:链表2.什么叫链表3.链表的种类带头结点的单向链表4.链表的描述注意:5.链表的基本操作创建链表的方法:程序:Student*Create()//创建链表函数{Student*ps;//当前要插入的结点Student*pEnd;//链尾指针ps=newStudent;//为当前要插入的结点分配空间cin>>ps->number>>ps->score;//给结点赋值head=NULL;//初始化链表,开始时链表为空pEnd=ps;while(ps->number!=0)//以输入的学生学号为0作为结束条件{if(head==NULL)//当前插入的结点为链表的第一个结点head=ps;elsepEnd->next=ps;pEnd=ps;ps=newStudent;cin>>ps->number>>ps->score;}pEnd->next=NULL;//完成链表创建后,链尾结点的指针域赋值为NULLdeleteps;//释放结点成员number为0的结点所占的堆内存return(head);}voidmain(){head=Create();}(2)遍历链表(3)删除链表结点例如:从前面已建立的学生链表中删除学号为number的学生结点。for(Student*pGuard=head;pGuard->next;pGuard=pGuard->next){if(pGuard->next->number==number)//找到要删除的结点{p=pGuard->next;//p指向待删除结点pGuard->next=p->next;deletep;cout<<number<<"havebeendeleted\n";return(head);}}cout<<number<<"notfound!\n";//未找到要删除的结点return(head);}(4)插入链表结点if(head->number>stud->number)//结点的插入位置在链首{stud->next=head;head=stud;return(head);}Student*pGuard=head;While//查找插入位置(pGuard->next&&pGuard->next->number<stud->number)pGuard=pGuard->next;//插入结点stud->next=pGuard->next;pGuard->next=stud;return(head);}