【问题标题】:C++ Circular Linked List : remove elementC++ 循环链表:删除元素
【发布时间】:2013-08-21 18:02:41
【问题描述】:

我已完成插入,在循环链表中搜索,但要删除我收到编译器错误...

以下是我的节点结构。

 struct node
 {
     int               p_data;
     struct node*   p_next;

     node(node* head, int data)
     {
           p_next = head;
           p_data = data;
     }

     explicit node(int data)
      {
           p_next = nullptr;
           p_data = data;
      }
 };




 node* remove_circular(node* head, node* target)
 {
      if (head == target->p_next)
      {
           delete head;
           return nullptr;
      }

      auto next_pointer = target->p_next;
      target->p_data = next_pointer->p_data;
      target->p_next = next_pointer->p_next;

      delete target->p_next;
      return target;
 }

在主函数中我调用

 head = remove_circular(head, head);
 head = remove_circular(head, temp);

这是删除 head 元素和 temp 指向的另一个元素。 但是我遇到了错误

有人知道从循环列表中删除一个元素吗?

我将其更改为删除目标-> p_next; 但现在它会删除列表中的所有内容。 有什么想法???

【问题讨论】:

  • 错误信息是什么?
  • 看起来remove_circular() 中的delete p_next; 行可能无法编译。
  • 我同意 quamrana。在remove_circular() 的范围内没有p_next 这样的东西。如果您认为编译器错误有问题,请等到您尝试运行 this。
  • 错误是 clang llvm 1.0 错误退出代码 254。我用谷歌搜索了它,这是我的“CODE”产生的一种错误,我确信错误是由 remove_circular 函数引起的。隐藏该功能,错误就消失了。
  • 这可能是由于 quamrana 所说的。在remove_circular() 范围内没有名为p_next 的变量

标签: c++ pointers linked-list circular-list


【解决方案1】:

这就是循环链表的工作原理:



每个节点都指向下一行,链表的尾部指向头节点。这就是 circular linked listregular linked list 的区别(在上面的例子中,这会使 37 指向终止符 null)。

如果您的列表只有一个对象,那么它应该如下所示:



因此,如您所见,在任何地方都没有指向null 的对象,但它发生在您的代码中,使用explicit 构造函数(如果我编写node n = node(12) 将运行)。

我建议您查看this link 以更好地了解您的算法应该是什么样子。

【讨论】:

    【解决方案2】:

    一旦您解决了编译器错误,您仍然会遇到算法问题。我建议你在纸上画一个循环列表,并考虑删除元素所需的步骤。考虑所有情况,例如:空列表、1 个项目的列表、不在列表中的元素等。

    【讨论】:

      【解决方案3】:

      你需要考虑几件事情。

      1.) 空列表的情况

        if(head == nullptr){//Empty list case
            return nullptr;
        }
      

      2.) 要移除的目标是头节点,这是列表中唯一的节点。

        if (head == target && target->p_next == head){
             create a temp node with the data value of target
             target = nullptr;//Since nothing points to target now it is for all intents and purposes deleted from the list but the data is still there so you can do something with it. I assume this is necessary because you return a node *.
             return the temp node
        }
      

      3.) 创建一个遍历整个列表的循环。如果您有一个包含两个项目的列表并且目标是第二个项目,那么您的某些东西只会删除下一个有效的节点。

        auto next_pointer = head->p_next;//could not be target->p_next as this assumed 
        while (next_pointer->p_next != target){//This while loop traverses the list rather than just deleting the next entry.
      

      4.) 在你的循环中添加一个检查以查看列表是否已被遍历并且目标是否从未找到。

         if (next_pointer->p_next == head){
            return nullptr;
         }//end IF
      

      5.) 在循环内添加 else 情况,这意味着目标位于列表中的任意位置。既然我给了你剩下的,我就让你去拿这部分。比上面的语句长几行并不难。

      【讨论】:

      • 我试图忠实于你的初始算法,即使你尝试的很多东西没有意义。我建议使用对节点的引用而不是指针,我会返回布尔或节点而不是节点 *。
      • 我同意德里克的观点。我相信你应该从头开始编写算法。
      • 谢谢。我想我明白了!
      猜你喜欢
      • 1970-01-01
      • 2020-12-03
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多