【问题标题】:c++ replace values in linked list by changing pointersc ++通过更改指针替换链表中的值
【发布时间】:2021-12-14 13:33:12
【问题描述】:

链表有问题。需要创建一个方法,通过更改指针而不是创建新元素来替换列表中的数据。现在我有这样的方法:

    void replaceValues(Node* head, int indexOne, int indexTwo)
 {
    Node* temporaryOne = NULL;
    Node* temporaryTwo = NULL;
    Node* temp = NULL;
    Node* current = head;
    int count = 0;
    while (current != NULL) {
        if (count == indexOne)
        {
            temporaryOne = current;
        }
        else if (count == indexTwo)
        {
            temporaryTwo = current;
        }
        count++;
        current = current->next;
    }
    current = head;
    count = 0;
    while (current != NULL) {
        if (count == indexOne)
        {
            head = temporaryTwo;
        }
        else if (count == indexTwo)
        {
            head = temporaryOne;
        }
        count++;
        current = current->next;
    }
}

我敢肯定,存在更简单的方法,如何做到这一点,但我不完全理解,它是如何工作的...... 提前感谢您的帮助。

【问题讨论】:

  • 建议:不要编写一个庞大的函数,而是编写两个小实用程序:一个从列表中删除一个条目而不删除它,另一个在列表中插入一个已经存在的条目。考虑到这些,实现你的功能会容易得多。
  • “替换”是指“交换”吗?
  • 所有任务都是这样的:创建函数将元素编号n替换为元素编号m。所以,也许交换。

标签: c++ pointers linked-list


【解决方案1】:

我假设“替换”实际上是指“交换”/“交换”。

一些问题:

  • 参数head 应该通过引用传递,因为要交换的节点之一实际上可能是那个头节点,然后head 应该在函数完成其工作后引用另一个节点。

  • 节点 before temporaryOne 需要更改其 next 指针,因此您应该提前一步停止循环,以便访问该节点并执行此操作。

  • 在某些情况下head 可能需要更改,但肯定并非总是如此,所以这样做head = temporaryOnehead = temporaryTwo 肯定是不对的。在大多数情况下,您需要从 preceding 节点链接到交换的节点(请参阅上一点)。

  • 被交换节点的next 指针也需要更改,因为它后面的节点将与以前不同。

正如在 cmets 中已经提到的,建议将任务拆分为删除和插入,因为当您尝试涵盖所有可能的情况时,摆弄 next 指针可能会让人感到困惑,特别是要区分两个节点是否相邻。

这里有一些函数将工作分为移除、插入和最终交换节点:

Node* removeNode(Node* &head, int index) {
    // If index is out of range, no node is removed, and function returns  nullptr
    // Otherwise the extracted node is returned.
    if (head == nullptr || index < 0) return nullptr;
    Node* current = head;
    if (index == 0) {
        head = head->next;
        current->next = nullptr;
        return current;
    }
    while (--index > 0) {
        current = current->next;
        if (current == nullptr) return nullptr;
    }
    Node* temp = current->next;
    if (temp != nullptr) {
        current->next = temp->next;
        temp->next = nullptr;
    }
    return temp;
}

void insertNode(Node* &head, Node* node, int index) {
    // If index is too large, node is inserted at the end of the list
    // If index is negative, node is inserted at the head of the list
    if (index <= 0 || head == nullptr) {
        node->next = head;
        head = node;
        return;
    }
    Node* current = head;
    while (--index > 0 && current->next != nullptr) {
        current = current->next;
    }
    node->next = current->next;
    current->next = node;
}

bool exchangeNodes(Node* &head, int indexOne, int indexTwo)
{
    // Returns true when successful, false when at least one index  
    // was out of range, or the two indexes were the same
    if (head == NULL || head->next == NULL || indexOne == indexTwo || indexOne < 0) return false;
    // To ensure the right order of operations, require the first index is the lesser:
    if (indexOne > indexTwo) return exchangeNodes(head, indexTwo, indexOne);
    Node* two = removeNode(head, indexTwo);
    if (two == nullptr) return false; // out of range
    Node* one = removeNode(head, indexOne);
    insertNode(head, two, indexOne);
    insertNode(head, one, indexTwo);
    return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多