我正在处理单个链表并且无法解决问题(我认为问题是在添加函数时使用NULL指针),问题是它只添加第一个数字来列表并跳过休息呼叫添加功能。无法理解c中的空指针
#include<stdlib.h>
#include<stdio.h>
struct node
{
int i;
struct node* next;
};
struct node* head = NULL;
int add(int val)
{
if(head == NULL)
{
head = (struct node*)malloc(sizeof(struct node));
head->i = val;
}
else
{
struct node* current = head;
while(current != NULL)
{
current = current->next;
}
current = (struct node*)malloc(sizeof(struct node));
current->i = val;
}
}
int print(struct node* first)
{
while(first != NULL)
{
printf("%d\n",first->i);
first = first->next;
}
}
int main()
{
add(36);
add(46);
add(97);
print(head);
return 0;
}
@underscore_d这就是所谓的C++,如果你想试着阻止人们询问关联链表的问题,那么这就是无关紧要的。 – immibis