Skip to main content
typo
Source Link

I believe these two lines are causing all the trouble.

 List* temp1 = head;
 List* temp2 = temp1->next;

temp2 never gets the head node because it is initialized as head->next to begin with.

I prefer double pointers since I find them rather cleaner.

void remove(ListNode<ItemType> * & head, double value) {    
    Node<ItemType> **pp = &head;
    Node<ItemType> *curr = head;
    while (curr) {
        if (curr->item == value) {
            *pp = curr->next;
            delete curr;
            curr = *pp;
        }
        else {
            pp = &curr->next;
            curr = curr->next;
        }
    }
}

I believe these two lines are causing all the trouble.

 List* temp1 = head;
 List* temp2 = temp1->next;

temp2 never gets the head node because it is initialized as head->next to begin with.

I prefer double pointers since I find them rather cleaner.

void remove(List * & head, double value) {    
    Node<ItemType> **pp = &head;
    Node<ItemType> *curr = head;
    while (curr) {
        if (curr->item == value) {
            *pp = curr->next;
            delete curr;
            curr = *pp;
        }
        else {
            pp = &curr->next;
            curr = curr->next;
        }
    }
}

I believe these two lines are causing all the trouble.

 List* temp1 = head;
 List* temp2 = temp1->next;

temp2 never gets the head node because it is initialized as head->next to begin with.

I prefer double pointers since I find them rather cleaner.

void remove(Node<ItemType> * & head, double value) {    
    Node<ItemType> **pp = &head;
    Node<ItemType> *curr = head;
    while (curr) {
        if (curr->item == value) {
            *pp = curr->next;
            delete curr;
            curr = *pp;
        }
        else {
            pp = &curr->next;
            curr = curr->next;
        }
    }
}
improved formatting
Source Link

I believe these two lines are causing all the trouble.

 List* temp1 = head;
 List* temp2 = temp1->next;

temp2 never gets the head node because it is initialized as head->next to begin with.

I prefer double pointers since I find them rather cleaner.

void remove(List * & head, double value) {    
    Node<ItemType> **prev**pp = &head;
    Node<ItemType> *curr = head;
    while (curr) {
        if (curr->item == value) {
            *pp = curr->next;
            delete curr;
            curr = *pp;
        }
        else {
            pp = &curr->next;
            curr = curr->next;
        }
    }
}

I believe these two lines are causing all the trouble.

 List* temp1 = head;
 List* temp2 = temp1->next;

temp2 never gets the head node because it is initialized as head->next to begin with.

I prefer double pointers since I find them rather cleaner.

void remove(List * & head, double value) {    
    Node<ItemType> **prev = &head;
    Node<ItemType> *curr = head;
    while (curr) {
        if (curr->item == value) {
            *pp = curr->next;
            delete curr;
            curr = *pp;
        }
        else {
            pp = &curr->next;
            curr = curr->next;
        }
    }
}

I believe these two lines are causing all the trouble.

 List* temp1 = head;
 List* temp2 = temp1->next;

temp2 never gets the head node because it is initialized as head->next to begin with.

I prefer double pointers since I find them rather cleaner.

void remove(List * & head, double value) {    
    Node<ItemType> **pp = &head;
    Node<ItemType> *curr = head;
    while (curr) {
        if (curr->item == value) {
            *pp = curr->next;
            delete curr;
            curr = *pp;
        }
        else {
            pp = &curr->next;
            curr = curr->next;
        }
    }
}
Source Link

I believe these two lines are causing all the trouble.

 List* temp1 = head;
 List* temp2 = temp1->next;

temp2 never gets the head node because it is initialized as head->next to begin with.

I prefer double pointers since I find them rather cleaner.

void remove(List * & head, double value) {    
    Node<ItemType> **prev = &head;
    Node<ItemType> *curr = head;
    while (curr) {
        if (curr->item == value) {
            *pp = curr->next;
            delete curr;
            curr = *pp;
        }
        else {
            pp = &curr->next;
            curr = curr->next;
        }
    }
}