1

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

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

我的其他功能工作正常,但我的删除功能在运行时中断。当我运行我的代码时,我遇到了分段错误,在尝试找出我的逻辑缺陷两天后,我正在向社区寻求帮助。在这一点上,我将不胜感激任何反馈,谢谢。这是我的删除功能:

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

2 回答 2

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指针是' headTails ; 指针是它next的. next 和 previous 都指向一个空列表中的自身。tailprevious

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


*这是一个与手头的问题没有直接关系的优秀读物的链接,但对于理解这种方法的哲学非常有用。

于 2013-09-25T15:48:29.337 回答
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;
于 2013-09-25T15:41:50.957 回答