c++ - 通过指针问题

标签 c++ pointers linked-list

我正在尝试实现我自己的链表版本以供学习。我有以下代码。 reverseList 函数工作正常,如果我在该函数中打印它就很好。

但是,当我离开函数然后调用 print 方法时,我得到了第一个值,然后什么也没有(null)。我猜当我退出函数时,它会让我回到原来的第一个 ([99]) 元素,现在实际上是最后一个元素。所以我的打印方法输出元素看到 null 是下一个并结束。

或者我在想我在函数中所做的更改不知何故只在该函数的范围内,即使我传递了一个指针,但这没有意义,因为如果是这样,那么我应该仍然拥有所有原始数据.

struct ListNode
{
    int value;
    ListNode* next = NULL;
};

void insertRecList(ListNode* list, int value)
{
    if(list->next == NULL)
    {
        ListNode* end = new ListNode;
        end->value = value;
        list->next = end;
    }
    else
        insertRecList(list->next, value);
}

void printList(ListNode* list)
{
    std::cout << list->value << std::endl;
    while(list->next != NULL)
    {
        list = list->next;
        std::cout << list->value << std::endl;
    }
}

void reverseList(ListNode* list)
{
    ListNode* next;
    ListNode* prev  = NULL;
    ListNode* cur   = list;

    while(cur != NULL)
    {
        if(cur->next == NULL)
        {     
            cur->next = prev;
            break;
        }
        else
        {
            next = cur->next;
            cur->next = prev;
            prev = cur;
            cur = next;
        }
    }
    list = cur;
    std::cout << cur->value << " list:" <<  list->value << std::endl;

}

void testLinkedList()
{
    srand(time(NULL));

    ListNode nodes;
    nodes.value = 99;
    int val;

    for(int i = 0; i < 5; i++)
    {
        val = rand() % 30 + 1;
        insertRecList(&nodes, i);
        //insertList(&nodes, val);
    }
    printList(&nodes);
    reverseList(&nodes);
    printList(&nodes);
}

int main()
{
    testLinkedList();
    return 0;
}

感谢你们能给我的任何帮助,

谢谢!

最佳答案

更新: 通过将 ListNode *list 传递给 reverseList,您创建了指向与 nodes 相同地址的指针的拷贝。在函数内部,您将 list 分配给更新后的 cur 指针,但拷贝将在最后被销毁。 list 仍指向与传递给 reverseList 之前相同的地址,但它的 next 已更改。

我稍微修改了你的代码:

#include <cstdlib>
#include <iostream>

struct ListNode
{
    int value;
    ListNode* next = nullptr;
};

void insertRecList(ListNode* list, int value)
{
    if(list->next == nullptr)
    {
        ListNode* end = new ListNode;
        end->value = value;
        list->next = end;
    }
    else
        insertRecList(list->next, value);
}

void printList(ListNode* list)
{
    std::cout << list->value << std::endl;
    while(list->next != nullptr)
    {
        list = list->next;
        std::cout << list->value << std::endl;
    }
}

void reverseList(ListNode** list)
{
    ListNode* cur   = *list;
    ListNode* next  = cur->next;
    ListNode* prev  = nullptr;

    while(cur != nullptr)
    {
        next = cur->next;
        cur->next = prev;
        prev = cur;
        cur = next;
    }
    *list = prev;
}

void cleanNodes(ListNode *list) {
    // clean goes here
}

void testLinkedList()
{
    srand(time(nullptr));

    ListNode *nodes = new ListNode();
    nodes->value = 99;
    int val;

    for(int i = 0; i < 5; i++)
    {
        val = rand() % 30 + 1;
        insertRecList(nodes, i);
        //insertList(&nodes, val);
    }
    printList(nodes);
    reverseList(&nodes);
    printList(nodes);

    cleanNodes(nodes);
}

int main()
{
    testLinkedList();
    return 0;
}

尝试编译:-std=gnu++11

关于c++ - 通过指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53273390/

相关文章:

java - 非常大的 Java ArrayList 遍历时间很慢

C++:为什么我的函数返回的引用地址与取消引用的指针地址不同?

c++ - 使用 while(!infile.eof()) 好奇如何在 C++ 中的 eof() 之前打破它

C:将新项分配给链表中的 "next"指针失败

c - 如何将一个节点从一个链表添加到另一个链表

c - 为什么我的所选元素的清除功能无法正常工作?

c++ - OpenGL:案例陈述和顺序错误

不同 block 中的 C++ 变量

Java - 等效指针

C++ 在运行时崩溃,但只是在某些时候