当标头未知时,从单链表中删除节点 [英] Delete node from singly linked list when header is not known

查看:84
本文介绍了当标头未知时,从单链表中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从单链表中删除特定节点

,其中列表的标题是未知的。只有指针可用的是

到要删除的节点

解决方案



" Raj" < RA ******** @ gmail.com>在消息中写道

news:11 ********************* @ f14g2000cwb.googlegro ups.com ...

有没有办法从单链表中删除特定节点
列表标题未知。只有指针可用指向要删除的节点 blockquote>


类型* temp = ptr;

ptr = ptr-> next;

free(temp);


但我想保留列表。如果我提到wats,我会失去与

前一节点的链接




"拉吉" < RA ******** @ gmail.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...

但我想保留这份清单。如果我提到wats,我会失去与
上一节点的链接



不,你不会,因为你的指针有

要删除的节点和previous_node-> gt的值同时

(这是相同的),当你说ptr = ptr时 - >接着"你更改

previous_node->的值,然后指向previous_node-> next-> next或

ptr-> next。

我不知道你是否有与列表或单一的双向间接连接,

因为如果你有单身,那么它只能删除前面的节点

指向节点,例如ptr-> next = ptr-> next-> next。


Is there any way to delete a particular node from a singly linked list
where the header of the list is unknown.Only pointer available is the
one which points to the node to be deleted

解决方案


"Raj" <ra********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...

Is there any way to delete a particular node from a singly linked list
where the header of the list is unknown.Only pointer available is the
one which points to the node to be deleted



type *temp = ptr;
ptr = ptr->next;
free(temp);


But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned



"Raj" <ra********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...

But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned


No you would not, because the the pointer you have has the adress of the
node you want to delete and value of previous_node->next at the same time
(that''s is the same thing), and when you say "ptr = ptr->next" you change
the value of previous_node->next to point to previous_node->next->next or
ptr->next.
I don''t know if you have double indirection connection with list or single,
because if you have single, than it''s only possible to delete node in front
of pointed node, like "ptr->next = ptr->next->next".


这篇关于当标头未知时,从单链表中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆