自星期一以来,我一直在做作业,但我仍然无法找到我的反向List功能没有返回反向节点的原因。我检查,双重检查甚至三重检查但仍然。任何帮助将不胜感激。我在vista上使用dec-C ++。这是我的功能:
List::ListNode *List::Reverse_List(ListNode *head)
{
ListNode* result = NULL;
ListNode* cur = head;
ListNode* next;
while (cur != NULL)
{
next = cur->next;
cur->next = result; //move the node into result
result = cur;
cur = next;
}
head = result;
return result;
}
答案 0 :(得分:1)
尝试在纸上用铅笔绘制堆内发生的事情。然后,您将了解如何改进代码。
以更加C ++的方式,您可以使用模板化的集合,例如std::list<>
或std::vector<>