【问题标题】:Deleting Head of Linked List when it is the only Node in the List. Singly Linked当链表是链表中唯一的节点时,删除链表的头。单链接
【发布时间】:2017-10-22 05:23:25
【问题描述】:

你好,我写了一个链表类的成员函数来复制偶数节点并删除奇数节点。

我的测试用例中的一切都是成功的,直到我尝试删除仅包含 1 个元素的列表的头部。

我的测试程序指出,无论出于何种原因,列表的长度都大于零,这是不可能的,因为我明确地将 headPtr 的值设置为 NULL。

void RemOddDupEven(Node*& headPtr)
{
Node *cur = headPtr;   // current node is set to head
Node *pred = 0;        // predecessor is NULL

if(headPtr == 0)       // check for empty list.
return;

// ensures will only run 1 time if there is 1 item in list.

while(cur != 0 && headPtr -> link != 0) // ensure there is a next link and more than 1 node in list.
{

if(cur -> data % 2 == 0)            // If the value is even
{
    Node *newNode = new Node;         // Create a new node
    newNode -> data = cur -> data;    // Set new Nodes data field
    newNode -> link = 0;              // set newNode link field to NULL

    if(cur == headPtr)                // if the current node is the head of the list
    {
     newNode -> link = headPtr;       // link field updated to head
     headPtr = newNode;               // newNode becomes the new Head of the list
    }
      else                            // current node is not the head of the list
      {
        pred -> link = newNode;       // update pred node to point to newNode
        newNode -> link = cur;        // update newNode to point to current
      }

    pred = cur;                       // update the pred node
    cur = cur -> link;                // update the current node
  }


     if(cur -> data % 2 == 1)              // check if this is odd
     {
     Node* nextNode = 0;                // Declare Next Node and set equal to

     if(cur -> link == 0)               // if there is no next Node then we are at the end of the list
     {
       delete cur;                      // delete the current Node
       cur = nextNode;
     }
      else{                             // else there is a next node defined
        nextNode = cur -> link;         // set the nextNode to point to next in list
        delete cur;                     // delete the current Node
        cur = nextNode;                 // assign the current Node to the next Node
      }

    if(pred)                            // if the pred is defined
     pred -> link = cur;                // previous node point to current node
      else
        headPtr = cur;                  // else we're at the head of the list
    }
  }    // end while
 }     // end method

这是我的列表函数代码

我的检查列表长度的功能如下

int listLength(Node* headPtr){ // pass by value
   int length = 0;

   while(headPtr !=0){
     length++;
     headPtr=headPtr->link;
   }

   return length;
}

【问题讨论】:

  • 您需要使用调试器来查找此问题的原因。

标签: c++ linked-list


【解决方案1】:
while(cur != 0 && headPtr -> link != 0)

如果 headPtr 是列表中的唯一节点,那么 headPtr -> link 实际上确实指向 null,因此检查它并不是解决这种情况的最佳方法。您可以使用简单的else if() 来应用这样的逻辑

 if(headPtr == 0)       // check for empty list.
    return;

    else if(headPtr -> link == NULL){
         // Do some stuff to delete only this node
    } 

    else { // handle as usual
    // ensures will only run 1 time if there is 1 item in list.

    while(cur != 0 && headPtr -> link != NULL) // ensure there is a next link and more than 1 node in list.
    {
      // The rest of your code
    }
   }

【讨论】:

  • 谢谢!这解决了我的问题!
  • 如果它解决了您的问题,请点击绿色复选标记接受答案,它会关闭问题
猜你喜欢
  • 1970-01-01
  • 2020-08-11
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
相关资源
最近更新 更多