Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Anything with a leading underscore is a reserved name in C++ (and in C). See this questionthis question for details.

Modern C++ uses nullptr rather than NULL. See this answerthis answer for why and how it's useful.

Anything with a leading underscore is a reserved name in C++ (and in C). See this question for details.

Modern C++ uses nullptr rather than NULL. See this answer for why and how it's useful.

Anything with a leading underscore is a reserved name in C++ (and in C). See this question for details.

Modern C++ uses nullptr rather than NULL. See this answer for why and how it's useful.

fixed bug
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
void deleteDuplicateEntries() {
    for (ListNode<Type> *curr = _head; curr && curr->next; curr = curr->next) {
        ListNode<Type> *n = curr;
        for (  ; n && n->next; n = n->next) {
            while (n->next && curr->value == n->next->value) {
                // delete matching node
                auto victim = n->next;
                n->next = victim->next;
                --_size;
                if (_tail == victim) {
                    _tail = n;
                }
                delete victim;
            }
        }
        // does last node match?
        if (n && n->value == curr->value) {
            delete n;
            --_size;
            curr->next = nullptr;
            _tail = curr;
        }
    }
}
void deleteDuplicateEntries() {
    for (ListNode<Type> *curr = _head; curr && curr->next; curr = curr->next) {
        ListNode<Type> *n = curr;
        for (  ; n && n->next; n = n->next) {
            while (n->next && curr->value == n->next->value) {
                // delete matching node
                auto victim = n->next;
                n->next = victim->next;
                --_size;
                delete victim;
            }
        }
        // does last node match?
        if (n && n->value == curr->value) {
            delete n;
            --_size;
            curr->next = nullptr;
            _tail = curr;
        }
    }
}
void deleteDuplicateEntries() {
    for (ListNode<Type> *curr = _head; curr && curr->next; curr = curr->next) {
        ListNode<Type> *n = curr;
        for (  ; n && n->next; n = n->next) {
            while (n->next && curr->value == n->next->value) {
                // delete matching node
                auto victim = n->next;
                n->next = victim->next;
                --_size;
                if (_tail == victim) {
                    _tail = n;
                }
                delete victim;
            }
        }
        // does last node match?
        if (n && n->value == curr->value) {
            delete n;
            --_size;
            curr->next = nullptr;
            _tail = curr;
        }
    }
}
removed spurious diagnostic line
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284

Use a more efficient algorithm

The deleteDuplicateEntries() routine is not very efficient. There's no need to traverse the entire list for each value. If we proceed from the head to the tail, we know that there cannot be any duplicates in the part of the list we've already processed, so all that's necessary is to search the remainder of the list. Here's how I'd do it:

void deleteDuplicateEntries() {
    for (ListNode<Type> *curr = _head; curr && curr->next; curr = curr->next) {
        ListNode<Type> *n = curr;
        for (  ; n && n->next; n = n->next) {
            while (n->next && curr->value == n->next->value) {
                // delete matching node
                auto victim = n->next;
                n->next = victim->next;
                --_size;
                delete victim;
            }
        }
        // does last node match?
        if (n && n->value == curr->value) {
            delete n;
            --_size;
            curr->next = nullptr;
            _tail = curr;
        }
    }
}

Use nullptr rather than NULL

Use nullptr rather than NULL

Use a more efficient algorithm

The deleteDuplicateEntries() routine is not very efficient. There's no need to traverse the entire list for each value. If we proceed from the head to the tail, we know that there cannot be any duplicates in the part of the list we've already processed, so all that's necessary is to search the remainder of the list. Here's how I'd do it:

void deleteDuplicateEntries() {
    for (ListNode<Type> *curr = _head; curr && curr->next; curr = curr->next) {
        ListNode<Type> *n = curr;
        for (  ; n && n->next; n = n->next) {
            while (n->next && curr->value == n->next->value) {
                // delete matching node
                auto victim = n->next;
                n->next = victim->next;
                --_size;
                delete victim;
            }
        }
        // does last node match?
        if (n && n->value == curr->value) {
            delete n;
            --_size;
            curr->next = nullptr;
            _tail = curr;
        }
    }
}

Use nullptr rather than NULL

added correction per Rakete1111
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading