简体   繁体   中英

Deleting Head of Linked List when it is the only Node in the List. Singly Linked

Hello I've written a member function of a linked list class to duplicate even Nodes and delete the odd valued Nodes.

Everything in my test cases is successful until i try to remove the head of the list containing only 1 element.

My test program states that for whatever reason the length of the list is greater than zero which can't be possible as i explicitly set the value of the headPtr to 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

Here is my code for List Function

My function for Checking the Length of the list is as follows

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

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

   return length;
}
while(cur != 0 && headPtr -> link != 0)

If headPtr is the only node in the list then headPtr -> link does in fact point to null so checking for that isn't the best way to account for that case. You can use a simple else if() to apply to logic like so

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM