数据结构之链表的删除
发布日期:2022-02-02 02:58:08 浏览次数:13 分类:技术文章

本文共 1117 字,大约阅读时间需要 3 分钟。

删除结点原则:
不改变原来的排列顺序,只是从链表中分离开来,撤消原来的链接关系。
两种情况:
1、要删的结点是头指针所指的结点则直接操作;
2、不是头结点,要依次往下找。
另外要考虑:空表和找不到要删除的结点

需要由两个临时指针:

P1: 判断指向的结点是不是要删除的结点(用于寻找);
P2: 始终指向P1的前面一个结点;

代码实现:

#include 
#include
#include
using namespace std;struct student{ int num,score; student *next;};struct student *del(student *head,int num){ struct student *p1,*p2; if(head==NULL) { cout<<"NULL!"<
num&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(num==p1->num) { if(p1==head)head=head->next; else p2->next=p1->next; } return head;}int main(){ int num; struct student a,b,c,d,e,*head,*p,*scur; a.num=101;a.score=120; b.num=102;b.score=121; c.num=103;c.score=122; d.num=104;d.score=123; e.num=106;e.score=125; head=&a;a.next=&b;b.next=&c;c.next=&d;d.next=&e;e.next=NULL; p=head; cout<<"请输入要删除的学号: "; cin>>num; del(head,num); do { if(p==head) { delete head; } else { cout<
num<<" "<
score<
next; } while(p!=NULL); return 0;}

转载地址:https://blog.csdn.net/u010368749/article/details/19070425 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:动态规划---hdu1864---最大报销额
下一篇:数据结构之链表的插入

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月09日 00时29分11秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章