C++循环链表:删除元素

C++循环链表:删除元素,c++,pointers,linked-list,circular-list,C++,Pointers,Linked List,Circular List,我完成了插入,搜索循环链表,但删除我得到编译器错误 下面是我的节点结构 struct node { int p_data; struct node* p_next; node(node* head, int data) { p_next = head; p_data = data; } explicit node(int data) {

我完成了插入,搜索循环链表,但删除我得到编译器错误

下面是我的节点结构

 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);
这是为了移除头部元件和temp指向的另一个元件。 但是我有错误

有人想从循环列表中删除一个元素吗

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

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

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

< P>这是循环链表如何工作的:

每个节点指向行中的下一个节点,列表的尾部指向header节点。这就是循环链表和常规链表的区别,在上面的例子中,循环链表将37点指向一个终止符null

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

所以,正如您所看到的,任何地方都没有指向null的对象,但是它发生在您的代码上,并且您的显式构造函数将在我编写node n=node12时运行


我建议您看一看,以便更好地了解您的算法应该是什么样子。

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

每个节点指向行中的下一个节点,列表的尾部指向header节点。这就是循环链表和常规链表的区别,在上面的例子中,循环链表将37点指向一个终止符null

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

所以,正如您所看到的,任何地方都没有指向null的对象,但是它发生在您的代码上,并且您的显式构造函数将在我编写node n=node12时运行


<>我建议你看一下,看看你的算法应该是什么样子。

你需要考虑几件事。

一,。空列表的情况

  if(head == nullptr){//Empty list case
      return nullptr;
  }
二,。要删除的目标是head节点,这是列表中唯一的节点

  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
  }
三,。创建一个循环,循环遍历整个列表。如果您有一个两项列表,并且目标是第二项,那么只有删除下一个节点的内容才有效

  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

五,。在循环中添加else,这意味着目标位于列表中的任意位置。既然我给了你剩下的,我就让你去拿这个零件。它并不难,只是比上面的语句长几行。

你需要考虑几件事。

一,。空列表的情况

  if(head == nullptr){//Empty list case
      return nullptr;
  }
二,。要删除的目标是head节点,这是列表中唯一的节点

  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
  }
三,。创建一个循环,循环遍历整个列表。如果您有一个两项列表,并且目标是第二项,那么只有删除下一个节点的内容才有效

  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

五,。在循环中添加else,这意味着目标位于列表中的任意位置。既然我给了你剩下的,我就让你去拿这个零件。只比上面的语句长几行并不难。

错误消息是什么?看起来像是删除p_下一行;在remove_中,循环可能无法编译。我同意quamrana的观点。在remove_circular的范围内不存在p_next这样的东西。如果您认为编译器错误是有问题的,只需等待,直到您尝试运行它。错误是clang llvm 1.0错误退出代码254。我在谷歌上搜索过它,它是由我的代码生成的一种bug,我确信这个错误是由remove_循环函数引起的。隐藏该函数,错误就会消失。这可能是由于quamrana所说的。在remove_循环范围内没有名为p_next的变量错误消息是什么?看起来像是delete p_next行;在remove_中,循环可能无法编译。我同意quamrana的观点。在remove_circular的范围内不存在p_next这样的东西。如果您认为编译器错误是有问题的,只需等待,直到您尝试运行它。错误是clang llvm 1.0错误退出代码254。我在谷歌上搜索过它,它是由我的代码生成的一种bug,我确信这个错误是由remove_循环函数引起的。隐藏该函数,错误就会消失。这可能是由于quamrana所说的。没有变量ca
lled p_next in remove_circular scope我试图保持对初始算法的真实性,尽管您尝试的很多内容都没有意义。我建议使用对节点的引用而不是指针,我会返回bool或node而不是node*。我同意Derek的观点。我相信你应该从头开始写你的算法。谢谢。我想我明白了!!我试图保持你最初算法的真实性,即使你尝试的很多东西都没有意义。我建议使用对节点的引用而不是指针,我会返回bool或node而不是node*。我同意Derek的观点。我相信你应该从头开始写你的算法。谢谢。我想我明白了!!