【发布时间】:2019-05-22 20:49:42
【问题描述】:
在下面给出的代码中,我将 head-pointer 从 main 函数传递给 addNode 函数,以便我保留 head-pointer 的位置(也将它传递给其他linkedList相关函数以执行其他操作)但下面的代码不起作用正如预期的那样,每次我调用函数 addNode 时,我都会得到Head node Created,我没有正确地将指针传递给 addNode 吗?如何实现将头指针保留到列表,并将其从 main() 发送到 addNode 函数的目标?
using namespace std;
struct stud {
string stud_name;
string stud_roll:
stud *next_node;
};
void addNode(stud* head);
int main()
{ stud *head = nullptr;
addNode(head);
addNode(head);
addNode(head);
addNode(head);
addNode(head);
}
void addNode(stud* head)
{
stud *new_node = new stud;
new_node->next_node = NULL;
if (head == NULL)
{
head = new_node;
cout << "Head node Created" << endl;
}
else
{
stud *temp_head = NULL;
temp_head = head;
while (temp_head->next_node != NULL)
{
temp_head = temp_head->next_node;
cout << "Moving temp pointer" << endl;
}
temp_head->next_node = new_node;
cout << "Body node created" << endl;
}
}
【问题讨论】:
标签: c++ struct linked-list