从C ++队列中间删除节点

时间:2009-05-27 17:54:43

标签: c++ linked-list

我有一个带有c风格ctordtor的链接列表。

当这个if声明决定不测试时,我太沮丧了,把我放进去了 无限循环。我不明白为什么它永远不会真实。

我正在尝试从LinkedList删除节点(类对象的地址)。

也许有人可以帮助我?

Node *Current = first_; // I want this to be my only Node Ptr Varaible Declaration.
if ( NULL == first_ )
std::cout << "Cannot delete from an empty list: \n";


while ( Current != NULL )
{
  if ( first_->data_ == node->data_ ) 
  {
    //check to see if we are deleteing the head.
    first_ = first_->next_;
    --listLen_;
    delete Current;
    std::cout << "Head Deleted!\n";
  }
  if ( Current->data_ == node->data_ ) // FOR SOME REASON this is never true?
  {
    --listLen_;
    node->data_ = NULL;
    Current     = Current->next_;
    node->data_ = Current;
  }
  else  // we must not of found it.  // else should match previous i
  {
    Current->prev_ = Current;// since we are not deleting the first node we are OK here.
    Current        = first_->next_;

    if ( Current->next_ == NULL ) // see if we are at the end of the list.
    {
      first_ = NULL;  
      last_  = Current->prev_;

    }
  }
}
return;

7 个答案:

答案 0 :(得分:2)

这应该被重写,因为它有太多的问题......为什么不使用STL容器呢?我认为这是一个家庭作业问题。

无限循环的答案是增加到下一个节点的else情况:

Current        = first_->next_;

如果在前两个节点中找不到数据,这将使您永远循环...因为您将始终将下一个测试设置为第一个下一个节点,并且它将永远不会将当前设置为NULL列表中的2个节点。

答案 1 :(得分:1)

我不完全确定你想要完成什么,但我确定你做错了。如果你只是想从一个与node-&gt; data_匹配的双向链表中删除一个元素,那么就像这样简单:

Node *Current = first_;

while (Current != NULL)
{
  if (Current->data_ == node->_data)
  {
    //If Current isn't the head of the list, set prev to next
    if (Current != first_)
      Current->prev_->next_ = Current->next_
    else
    {
      first_ = Current->next_;
      if (first_ != NULL)
        first_->prev_ = NULL;
    }

    //If Current isn't the tail of the list, set next to prev
    if (Current->next_ != NULL)
      Current->next_->prev_ = Current->prev_
    else if (Current->prev_ != NULL)
      Current->prev_->next_ = NULL;


    delete Current;
    Current = NULL;
  }
  else
  {
    Current = Current->next_;
  }
}
return;

答案 2 :(得分:0)

您没有显示node来自何处或data_如何定义,但如果它是指针类型,您可能需要比较内容,而不是地址。

假设data_是一个指向某事物的指针,它指向operator==定义的内容,或者是内置类型,它具有您正在寻找的值你可以这样做:

if ( *Current->data_ == *node->data_ )

答案 3 :(得分:0)

如果first_->data_ == node->data_的计算结果为true,则第二个if语句将始终计算为true,则第二个if条件将始终计算为false Current->data_ == node->data_,因为在第一次迭代first_ == Current和您删除Current而不更新

要从链接列表中删除节点,您似乎做了太多工作。

答案 4 :(得分:0)

对于实际问题,这里不是一个真正的答案,而是一个建议。

我永远不会遍历链表来删除列表中的条目。每个条目都应该有一个有效的下一个和上一个指针,当你从列表中删除一个条目时,你只需将前一个记录指向下一个条目,反之亦然,从列表中删除自己。空列表应该有一个头记录和一个尾记录,它们只是指向彼此,所有有效的条目都插入其间。

答案 5 :(得分:0)

保持你的循环小,更容易弄清楚出了什么问题。假设您的数据比较有意义,请查看以下内容:

curr = first_;
while( curr && (curr->data_ != node->data_) ) { 
  curr = curr->next_; 
}
if (!curr) return   // didnt find it, nothing to remove
if ( curr == first_ )   
  first_ = curr->next_  
else            
  curr->prev_->next_ = curr->next_
curr->next_->prev_ = curr->prev_ // always fix next's prev
delete curr

答案 6 :(得分:0)

删除传递值的节点。

void deleteBegin()
{
 Node* temp =Head;
 if(temp==NULL)
     return;
 Head=Head->next;
 free(temp);
}
void deleteMiddle(int _data)
{
 Node* curr = Head;
 Node* prev = Head;

if(curr==NULL)
    return;

if(curr->next==NULL)
  {
    deleteBegin();
    return;
 }

 while(curr->next!=NULL && curr->data!=_data)
 {
    prev=curr;
    curr=curr->next;
 }

 if(curr->data == _data)
 {
     if(prev==curr)
     {
        deleteBegin();
        return;
      }
    prev->next = curr->next;
    free(curr);
 }  
 else
 {
    cout<<"Element Not Found\n";
    return;
 }

}