我在添加到动态链接列表时遇到了一些问题。基本上,我的第一个节点似乎被覆盖。下面是代码:添加到链接列表中C
struct palNode {
int number;
int pointer_index;
char* solving_array;
int solved;
struct palNode* next;
}
,这里是我的加入方法:
struct palNode* add_palNode_from_keyboard(struct palNode* head, int num, int pos){
struct palNode* newnode = (struct palNode*) malloc(1 * sizeof(struct palNode));
struct palNode* current_node = head;
if (current_node == NULL)
{
head = newnode;
}
else
{
while ((*current_node).next != NULL)
{
current_node = (*current_node).next;
}
(*current_node).next = newnode;
}
(*newnode).number = num;
(*newnode).pointer_index = pos;
(*newnode).next = NULL;
printf("Operation completed\n");
return newnode;
}
这里是我的问题:什么是我做错了什么?有没有更正确的做法?我见过其他类似的问题,但我还是不明白他们
[请参阅为什么不投的malloc'的返回值这个讨论()'和'C'中的家庭。](http://stackoverflow.com/q/605845/2173917)。 –
另外,'1 * sizeof(struct palNode)'确实是不必要的。请注意,您也可以写* sin(x)^ 2 + cos(x)^ 2 *或* exp(0)*,但仅仅因为您可以这并不意味着您必须这样做。 ''(* newnode).number'你不需要解除'newnode',只需'newnode-> number'就可以了。 –
'回报newnode;' - >'返回头;',在callerside'头= add_palNode_from_keyboard(...' – BLUEPIXY