C ++循环链表:删除元素 [英] C++ Circular Linked List : remove element

查看:241
本文介绍了C ++循环链表:删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经完成了插入,搜索循环链表,但为了删除我得到编译错误...下面是我的结构的节点。

  struct node 
{
int p_data;
struct node * p_next;

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

显式节点(int数据)
{
p_next = nullptr;
p_data = data;
}
};




node * remove_circular(node * head,node * target)
{
if(head == target-> p_next)
{
删除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指向的另一个元素。
但我收到错误



任何人都有想从循环列表中删除一个元素




我改变它删除target-> p_next;
,但现在它删除列表中的所有内容。
任何想法

解决方案

您需要考虑几件事。



1。)空列表的情况

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

2。)要删除的目标是头节点,这是

  if(head == target&& target-> p_next == head){ 
创建一个带有目标数据值的临时节点
target = nullptr; //因为没有任何东西指向目标,所以它是从列表中删除的所有意图和目的,但数据仍然存在,所以你可以做一些事情。我认为这是必要的,因为你返回一个节点*。
返回临时节点
}

3。)创建一个循环通过整个列表。如果您有两个项目列表,而目标是第二个项目,则您只有删除下一个节点。

  auto next_pointer = head-> p_next; //不能为target-> p_next,因为假设为
while(next_pointer-> p_next!= target){//此while循环遍历列表,下一个条目。

4.)在循环中添加一个检查以查看列表是否已遍历,

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

5。)在循环中添加else case目标位于列表中的任意位置。因为我给你的休息,我会让你得到这部分。这不难只是比上面的语句更长几行。


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. But I am getting errors

Anybody has any idea to remove one element from circular list??

I changed it to delete target->p_next; but now it deletes everything in the list. Any idea???

解决方案

You need to consider several things.

1.) the case of an empty list

  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.

  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. 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.

   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. 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.

这篇关于C ++循环链表:删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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