我似乎无法弄清楚为什么会收到此错误。我运行 valgrind,它说 newNode = (NodeType *)malloc(sizeof(NodeType)); 正在创建错误,但我不知道为什么...基本上我要做的是初始化传入的大小为 n 的链表并将头部设置为第一个节点。
typedef struct {
int number;
AnotherNodeType *anotherLinkedList;
} Type;
typedef struct Node{
Type *data;
struct Node *next;
} NodeType;
int main(){
NodeType *nodePointer = NULL;
initLinkedList(&nodePointer, 10);
return 0;
}
void initLinkedList(NodeType **nodePointer, int n){
int i;
NodeType *prevNode, *newNode;
prevNode = NULL;
for (i = 0; i < n; i++){
newNode = (NodeType *)malloc(sizeof(NodeType));
newNode->data = (Type *)malloc(sizeof(Type));
newNode->data->number = i;
newNode->data->anotherLinkedList = NULL;
if (prevNode == NULL){
*nodePointer = newNode;
}
else{
prevNode->next = newNode;
}
prevNode = newNode;
}
}