【发布时间】:2020-07-19 13:33:14
【问题描述】:
我创建了一个带有一些基本链表函数的头文件,其中之一是 append(),它以一个结构节点*指针和值作为参数。 所以当我调用这个函数时,由于某种原因,根值被改变了,所以我不得不不断地从被调用和 main 传递根指针。 他们有办法纠正这个问题吗?
**我试过的:**
**主要源代码:**
#include"singly_linked_list0.h"
struct node* root = NULL;
int main(){
int n = 5;
while(n > 0){
root = append(root, n);
n--;
}
print_all(root);
return 0;
}
** 头函数:**
/*appends the value passed at the end, and returns the ROOT*/
struct node* append(struct node* ROOT, int d){
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = d;
temp->link = NULL;
if(ROOT == NULL) ROOT = temp;
else{
struct node *p;
p = ROOT;
while(p->link != NULL){
p = p->link;
}
p->link = temp;
}
return ROOT;
}
【问题讨论】:
-
头文件不应包含任何代码。
-
这是“不断传递根指针”的代码吗?