简体   繁体   English

C ++循环链表:remove元素

[英]C++ Circular Linked List : remove element

I am done with insertion, search in circular linked list but for removal I am getting compiler errors... 我已经完成插入,在循环链表中搜索,但要删除却遇到了编译器错误...

Following is my structure for nodes. 以下是我的节点结构。

 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;
 }

and in main function I call 在主函数中

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

this is to remove head element and another element that temp points to. 这是为了删除head元素和temp指向的另一个元素。 But I am getting errors 但是我遇到了错误

Anybody has any idea to remove one element from circular list?? 任何人有任何想法从循环列表中删除一个元素?

I changed it to delete target->p_next; 我将其更改为删除target-> p_next; but now it deletes everything in the list. 但现在它将删除列表中的所有内容。 Any idea??? 任何想法???

This is how a circular linked list works: 循环链表的工作方式如下:


链表示例


Each node points to the next in line, and the tail of the list points to the header node. 每个节点都指向行中的下一个节点,列表的尾部指向标头节点。 That's the difference from a circular linked list to a regular linked list (which, in the case above, would make 37 point to a terminator null ). 这是从circular linked listregular linked list的区别(在上述情况下,这会使37指向终止符null )。

In the case of your list having only one object, then it should look something like this: 如果您的列表只有一个对象,那么它应该看起来像这样:


单节点情况


So, as you can see, there is no object pointing to null anywhere, yet it happens on your code with your explicit constructor (which will run if I write node n = node(12) ). 因此,如您所见,在任何地方都没有指向null对象,但是它在使用explicit构造函数的代码中发生(如果我编写node n = node(12) ,它将运行)。

I suggest you take a look at this link to have a better understanding of how your algorithm should look like. 我建议您看一下此链接 ,以更好地了解算法的外观。

Once you resolve your compiler error, you are still going to have algorithmic issues. 解决编译器错误后,您仍然会遇到算法问题。 I suggest you draw a circular list on paper and think about the steps required to remove an element. 我建议您在纸上画一个圆形清单,并考虑删除元素所需的步骤。 Consider all the cases, for example: empty list, list of 1 item, element not in the list, etc. 考虑所有情况,例如:空列表,1个项目的列表,不在列表中的元素等。

You need to consider several things. 您需要考虑几件事。

1.) the case of an empty list 1.)空列表的情况

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

2.) The target to be removed is the head node and this is the only node in the list. 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.) Create a loop that iterates through the entire list. 3.)创建一个循环遍历整个列表的循环。 You have something that would only delete the next node which works if you have a two item list and target was the second item. 如果只有两个项目列表,而目标是第二个项目,那么您将只能删除下一个起作用的下一个节点。

  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.)Inside you loop add a check to see if the list has been traversed and target was never found. 4)在循环中添加检查以查看列表是否已遍历并且从未找到目标。

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

5.) Inside the loop add the else case which means target was in an arbitrary location in the list. 5.)在循环中添加else情况,这意味着目标位于列表中的任意位置。 Since I gave you the rest I'll leave you to get this part. 自从我把剩下的都给了我以后,我就离开你去做这部分。 It's not hard just a few lines longer than the statements above. 仅比上面的语句多几行并不难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM