c ++通过更改指针替换链表中的值

c++ replace values in linked list by changing pointers

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

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

我敢肯定,存在更简单的方法,如何去做,但我不完全理解它是如何工作的... 在此先感谢您的帮助。

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

一些问题:

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

  • 节点 before temporaryOne 需要它的 next 指针来改变,所以你应该在为了访问该节点并执行此操作。

  • 在某些情况下head可能需要改变,但肯定不是总是这样,所以做head = temporaryOnehead = temporaryTwo肯定是不对的。在大多数情况下,您需要从 preceding 节点 link 到交换节点(参见上一点)。

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

正如评论中已经提到的那样,建议将任务分为删除和插入,因为当您尝试涵盖所有可能的情况时,使用 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;
}