我正在尝试用C ++构建链表。我的理解是,我创建的代码应该创建一个节点,然后逐步将4个链接到最后。不幸的是,虽然我希望看到cout结果为" 12 123 1234 12345"我看到" 12 12 12 12"在我的主要内容中,我无法遍历列表 - 它只是崩溃了。
我有以下代码:
struct listNode {
int val;
listNode* next;
};
int nodeCount = 0;
listNode* addToEnd(listNode* node) {
listNode* newNode = new listNode;
newNode->val = ++nodeCount;
newNode->next = NULL;
if (node == NULL) {
return newNode;
}
listNode* current = node;
cout<<"\n\n";
do {
if (current->next == NULL) {
current->next = newNode;
}
cout<<current->val<<"\n";
current = current->next;
} while (current->next != NULL);
cout<<current->val<<endl;
}
int main()
{
listNode* first = addToEnd(NULL);
addToEnd(first);
addToEnd(first);
addToEnd(first);
addToEnd(first);
cout<<"Third: "<<first->next->next->val;
}
任何帮助都表示赞赏,因为我已经结束了!
答案 0 :(得分:5)
很明显,函数addToEnd
是错误的
listNode* addToEnd(listNode* node) {
listNode* newNode = new listNode;
newNode->val = ++nodeCount;
newNode->next = NULL;
if (node == NULL) {
return newNode;
}
listNode* current = node;
cout<<"\n\n";
do {
if (current->next == NULL) {
current->next = newNode;
}
cout<<current->val<<"\n";
current = current->next;
} while (current->next != NULL);
cout<<current->val<<endl;
}
假设列表已经包含两个节点,并考虑函数内部的do-while循环。首先current_next
!= null所以执行以下语句
current = current->next;
现在当前指向第二个节点。其数据成员next
等于NULL。所以循环的条件
} while (current->next != NULL);
将为false,不会重复迭代。所以我们没有添加任何东西。
如果node不等于NULL,函数也不会返回任何内容。
按以下方式重写该功能
listNode* addToEnd( listNode* node )
{
listNode* newNode = new listNode { ++nodeCount, NULL };
if ( node == NULL) return newNode;
listNode* current = node;
while ( current->next != NULL ) current = current->next;
current->next = newNode;
return newNode;
// or
//return node;
}
考虑到这个陈述
cout<<"Third: "<<first->next->next->val;
仅输出第三个节点的值。 如果要输出所有列表,则应编写
for ( listNode *current = first; current; current = current->next )
{
std::cout << current->val << ' ';
}
std::cout << std::endl;
顺便使用我的函数你可以用main编写,例如以下方式:)
listNode* first;
addToEnd( addToEnd( addToEnd( addToEnd( first = addToEnd( NULL ) ) ) ) );
答案 1 :(得分:3)
使用for循环来到最后一个节点而不是一会儿,然后分配循环的新节点OUTSIDE。尝试在内部执行将导致无限循环(并使代码更难阅读):
listNode* current;
for(current = node; current->next != NULL; current = current->next) ;
current->next = newNode;
您还忘记在功能结束时返回newNode
。
答案 2 :(得分:2)
您使用非void
返回类型的函数结束。你没有使用返回值的事实并没有那么好。
离开函数末尾相当于没有值的
return
;这会导致值返回函数中的未定义行为。
答案 3 :(得分:0)
如果检查if(node==null)
的if条件失败,则没有return语句。
答案 4 :(得分:0)
在你的问题中使用递归函数是否违反规则?
为什么不......
void addToEnd(listNode* node){
if(node == NULL){
*node = new listNode;
node->next = NULL;
node->val = ++nodeCount;
}else{
addToEnd(node->next);
}
return;
}
int main(){
listNode* first = NULL;
addToEnd(first); // 1
addToEnd(first); // 2
addToEnd(first); // 3
addToEnd(first); // 4
addToEnd(first); // Linked list is now 5 long
}
答案 5 :(得分:0)
这就是我将编码添加到包含节点计数的链表的五个节点的方法。如果有人有建议,欢迎。
#include <iostream>
#include <cstdlib>
using namespace std;
struct listNode{
int val;
listNode* next;
};
listNode* addToEnd(listNode*, int);
int main()
{
listNode* first = NULL;
listNode* temp;
int nodeCount = 1;
for(int i = 0; i < 5; i++){
first = addToEnd(first, nodeCount);
nodeCount++;
}
temp = first;
while(temp){
cout << temp->val << ' ';
temp = temp->next;
}
temp = first;
//Deallocate memory
while(temp){ //could do memory deallocation while displaying
nodeToDelete = temp; //the value of nodeCount but wanted to illustrate
//both methods individually
temp = temp->next;
delete nodeToDelete;
}
first = NULL; //eliminate hanging pointer
return 0;
}
listNode* addToEnd(listNode* node, int nodeCount)
{
listNode* newNode = new (nothrow) listNode;
listNode* current = node;
if(newNode){
newNode->val = nodeCount;
newNode->next = NULL;
if (node == NULL)
node = newNode;
else{
while (current->next != NULL)
current = current->next;
current->next = newNode;
}
}
else
cout << "error allocationg memory" << endl;
return node;
}