C ++ Удаление и удаление узла - PullRequest
1 голос
/ 01 мая 2020

Я впервые использую указатели и пытаюсь написать функцию, которая удаляет узел, и функцию, которая удаляет узел. Мой код распознает, когда я вставляю ключ и данные (например, key = 123, data = 123). Однако, когда я пытаюсь удалить ключ 123, он не распознает, что он существует. Вот мой LList. cpp, при необходимости я могу включить другой код:

#include <iostream>
#include "LList.h"

//----------------------------------------------------
//             constructor & destructor
//----------------------------------------------------
LList::LList() {
    head = NULL;
} // constructor (default)
LList::~LList() {
    if (head) delete head;
} // destructor


//----------------------------------------------------
//                  print
//----------------------------------------------------
// prints each node in the list, or EMPTY when there
// are no nodes to print
//----------------------------------------------------
void LList::print() {
    if (head == NULL)
        cout << "EMPTY\n\n";
    else {
        node* p;
        p = head;
        while (p != NULL) {
            p->print();
            p = p->next;
        }
        cout << endl;
    }
} // print()

//----------------------------------------------------
//                       search
//----------------------------------------------------
// Given: key to search for
// Returns: pointer to a node in the list found to have
//          the given search key, or NULL for not found
//----------------------------------------------------
node* LList::search(int srchKey) {
    if (head == NULL)
        return NULL;
    node* itr = head;
    while (itr != NULL) {
        if (itr->key == srchKey)
            itr = itr->next;
    }
    return itr;
} // search()

//----------------------------------------------------
//                 findNode
//----------------------------------------------------
// Given: a key to search for
// Searches for a node with the given key, and if 
// found, invokes the print() method in the found node.
// Otherwise, prints "not found"
//----------------------------------------------------
void LList::findNode(int srchkey) {
    if (head == NULL) {
        cout << "not found\n";
    }
    node* itr = head;
    while (itr != NULL) {
        if (itr->key == srchkey) {
            print();
        }
        itr = itr->next;
    }
    cout << "not found\n";
} // findNode()

//----------------------------------------------------
//                  getb4
//----------------------------------------------------
// Given: a pointer to a node in the list
// Returns: a pointer to the node in the list BEFORE
//               the one pointed to by r, OR
//          NULL when r is the head or not found in
//               the list
//----------------------------------------------------
node* LList::getb4(node* r) {
    node* tmp = head;
    while (tmp->next != NULL) {
        if (tmp->next == r)
            return tmp;
        tmp = tmp->next;
    }
    return NULL;
} // getb4()

//----------------------------------------------------
//                     insert
//----------------------------------------------------
// Given: key and data for a new node
// Allocates/populates a new node
// When a node with the same key already exists:
//     the current/old node is replaced by the new one
//     and the old one is placed on the new one's 
//     duplicate list.
// Otherwise, the new node is prepended to the head
//     of the list.
//----------------------------------------------------
void LList::insert(int k, string d) {
    node* newNode = new node();
    node* currentNode, *prevNode;
    newNode->key = k;
    newNode->data = d;
    if (head == NULL) {
        head = newNode;
    }
    else {
        currentNode = search(k);
        if (currentNode == NULL) {
            newNode->next = head;
            head = newNode;
        }
        if (currentNode == head) {
            newNode->next = head->next;
            head->next = NULL;
            newNode->dup = head;
            head = newNode;
        }
        prevNode = getb4(currentNode);
        if (prevNode == NULL)
            return;
        prevNode->next = newNode;
        //newNode->next = currentNode->next;
        currentNode->next = NULL;
        newNode->dup = currentNode;
    }
} // insert()

//----------------------------------------------------
//                     remove
//----------------------------------------------------
// Given: a pointer to a node in the list to be removed
//        BUT NOT DELETED/DESTROYED
// Returns: TRUE - when the node was successfully removed
//          FALSE - when the given node is NULL or the node
//                  is not actually in the list.
// Simply removes the node from the linked list.
// (including setting the NEXT pointer in the node to NULL)
//----------------------------------------------------
bool LList::remove(node* r) {
    if (r == NULL || search(r->key) == NULL) {
        return false;
    }
    if (r == head) {
        head = head->next;
    }
    else {
        node* prev = getb4(r);
        prev->next = r->next;
    }
    r->next = NULL;
    return true;
} // remove()

//----------------------------------------------------
//                     drop
//----------------------------------------------------
// Given: key of a node to drop
// Returns: TRUE when a node was found and deleted
//          FALSE when a node with the given key not found,
//                or the remove() fails.
// Searches for a node in the list with the given key:
// When found, removes and deletes the node
//----------------------------------------------------
bool LList::drop(int k) {
    node* currentNode = search(k);
    if (currentNode == NULL || !remove(currentNode))
        return false;
    node* tmp = currentNode;
    while (tmp != NULL) {
        currentNode = currentNode->dup;
        remove(tmp);
        tmp = currentNode;
    }
    return true;
} // drop()

//----------------------------------------------------
//                      max
//----------------------------------------------------
// Returns: a pointer to the node with the highest key
//          or NULL when there list is empty.
node* LList::max() {
    if (head == NULL)
        return NULL;
    node* max = head;
    node* tmp = head->next;
    while (tmp != NULL) {
        if ((tmp->key) > (max->key)) {
            max = tmp;
        }
        tmp = tmp->next;
    }
    return max;
} // max()

1 Ответ

3 голосов
/ 01 мая 2020

Это l oop:

 while (itr != NULL) {
        if (itr->key == srchKey)
            itr = itr->next;
    }
 return itr;

неверно. Вы или итерируете, если ключ найден, или не повторяетесь вообще. Вам нужно перебирать список, и искать ключ при этом:

 while (itr != NULL) {
        if (itr->key == srchKey)
            break;
        itr = itr->next;
    }
 return itr;

Кроме того, ваш деструктор неверен; Вы удаляете только первый узел, а остальные - утечка. Вам необходимо удалить все узлы, просматривая связанный список:

LList::~LList() {
    while (head) 
        delete std::exchange(head, head->next);
}

Кроме того, предпочитайте использовать nullptr вместо NULL.

...