节点在双向链表中删除

时间:2013-09-25 15:29:28

标签: c++ doubly-linked-list

我正在开发一个删除双向链表节点的函数。这是我的头文件:

class LinkedList
{
private:
      struct Node
      {
         int data;
         Node *next;
         Node *previous;
      };

      int count;
      Node *head;
      Node *tail;

public:
      LinkedList() {head = NULL; tail = NULL; count = 0;} //Constructor

      void insert(const int );
      bool remove(const int );
      bool contains(const int );

      size_t lenght() {return count;}
};

我的其他功能正常,但是我的删除功能在运行时断开。 当我运行我的代码时,我得到了一个分段错误,经过2天的尝试找出我逻辑中的缺陷后,我转向社区寻求帮助。我很感激此时的任何反馈,谢谢。这是我的删除功能:

bool LinkedList::remove(const int item)
{//if the list is empty returns false
if(head == NULL) {return false;}

Node *hptr = head;
Node *tptr = tail;

if((hptr -> data) == item)
{//if the node is at the head of the list
  hptr = hptr -> next;
  delete head;
  hptr -> previous = NULL;
  head = hptr;
  --count;
  return true;

} else if((tptr -> data) == item) {
 //if the node is at the tail of the list
  tptr = tptr -> previous;
  delete tail;
  tail = tptr;
  tptr -> next = NULL;
  --count;
  return true;

} else {//if the node is in he middle of the list
  Node *ptr_head = head;   Node *ptr_headp = NULL;
  Node *ptr_tail = tail;   Node *ptr_tailp = NULL;

  while((ptr_head -> data) != item || (ptr_tail -> data) != item)
  {//pointers pass each other then data was not found
     if((ptr_tail -> data) < (ptr_head -> data)) {return false;}
   //traversing the list from the head and tail simultaniously
     ptr_headp = ptr_head;
     ptr_head = ptr_head -> next;

     ptr_tailp = ptr_tail;
     ptr_tail = ptr_tail -> previous;
  }

  if((ptr_head == ptr_tail) && ((ptr_tail -> data) == (ptr_head -> data)))
  {//the item is at the intersection of both head and tail pointers
     ptr_headp -> next = ptr_tailp;
     ptr_tailp -> previous = ptr_headp;
     delete ptr_head;
     delete ptr_tail;
     --count;
     return true;
  }

  if((ptr_head -> data) == item)
  {//the item is before middle node
     ptr_headp -> next = ptr_head -> next;
    (ptr_head -> next) -> previous = ptr_headp;
     delete ptr_head;
     --count;
     return true;
  }

  if((ptr_tail -> data) == item)
  {//the item is after the middle node
     ptr_tailp -> previous = ptr_tail -> previous;
    (ptr_tail -> previous) -> next = ptr_tailp;
     delete ptr_tail;
     --count;
     return true;
  }
}

return false;
}

2 个答案:

答案 0 :(得分:2)

这是一个常见的例子,当改变数据结构时,通过统一看起来不同的情况 * ,可以使逻辑变得非常简单。

逻辑的主要问题是你有很多条件需要检查:

  • 删除其后有其他节点的第一个节点
  • 删除前面有其他节点的最后一个节点
  • 删除唯一的节点
  • 删除中间的节点

通过确保左侧始终有节点,任何节点右侧有节点,您可以使这四个条件与最后一个条件相同。以下是如何做到这一点:

class LinkedList
{
private:
      struct Node
      {
         int data;
         Node *next;
         Node *previous;
      };

      int count;
      // The change begins here
      Node headTail;
      // End of the change

public:
      LinkedList() {head = NULL; tail = NULL; count = 0;} //Constructor

      void insert(const int );
      bool remove(const int );
      bool contains(const int );

      size_t lenght() {return count;}
};

head指针为headTail的{​​{1}}; next指针是tail。下一个和前一个点都在一个空列表中返回。

这有点低效,因为previous的{​​{1}}未使用。该列表变为循环,始终存在一个节点。使用此节点,您可以安全地删除中间的任何节点,并更新前一个和下一个指针,就像它们属于不同的对象一样。

<小时/> * 以下是a link一个与手头问题没有直接关系的优秀读物,但对于理解这种方法的哲学非常有用。

答案 1 :(得分:1)

// Locate the item to remove
Node* to_remove = head;
while(to_remove && to_remove->data != item)
  to_remove = to_remove->next;

// Do the removal if we found it
if(to_remove)
{
  // If it was at the head, advance the head to the next item
  if(to_remove == head)
    head = head->next;
  // If it was at the tail, advance the tail to the previous item
  if(to_remove == tail)
    tail = tail->previous;

  // Remove from the list
  if(to_remove->next)
    to_remove->next->previous = to_remove->previous;
  if(to_remove->previous)
    to_remove->previous->next = to_remove->next;

  // Free the removed node
  delete to_remove;
  count--;
  return true;
}

return false;