程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了双向链表中“删除”功能的问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决双向链表中“删除”功能的问题?

开发过程中遇到双向链表中“删除”功能的问题的问题如何解决?下面主要结合日常开发的经验,给出你关于双向链表中“删除”功能的问题的解决方法建议,希望对你解决双向链表中“删除”功能的问题有所启发或帮助;

所以我一直在尝试创建一个函数,在另一个节点之后从双向链表中删除一个节点,但我一直收到段错误错误。

这是我目前得到的:

voID _delete2(struct Node * curr,struct Node * pred)  
 {   //takes the Node to delete and the Node before it
     struct Node * temp = curr->next;
     if(curr->next == NulL)
     {
         return;
     }
     else if(temp->next->next == NulL)
     {
         curr->next = NulL;
     }
     else
     {  //this is the part that I'm having trouble with
         temp = curr->next;
         curr->next = temp->next;
         temp->next->prev = pred;
         free(temp);
     }
  }

解决方法

我根本没有测试过这段代码,我已经把它写在了我的脑海里,但我认为它会起作用。此代码应从双向链表中删除 curr 节点:

void _delete2(struct Node * curr)  
{   
     if(curr->next == NULL)
     {
         curr->prev->next = NULL; // <-- there is no node after current one,//     so node after previous one will be null
     }
     else
     {
         curr->prev->next = curr->next; // Node after before current one is the
                                        // node after current one
         curr->next->prev = curr->prev; // Node before next node will be 
                                        // node before current node after deleting.
     }
     free(curr);
}

另外,我假设 curr 不是 NULL,这不是不合逻辑的假设。

更新: 似乎我读错了问题,您想删除当前节点之后。您可以通过小检查使用以前的方法:

void _delete_after(struct Node * curr)  
{
    struct Node *tmp = curr->next;
    if (tmp != NULL)
        _delete2(tmp);
}

大佬总结

以上是大佬教程为你收集整理的双向链表中“删除”功能的问题全部内容,希望文章能够帮你解决双向链表中“删除”功能的问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。