0
我试图创建链接列表,但节点未正确链接。我错过了我的代码&我不知道它是什么。链接列表节点链接问题
typedef struct sllist{
int x;
struct sllist *next;
} slilist;
slilist *head=NULL, *tail=NULL;
void add_to_List(int val){
slilist *temp = malloc(sizeof(slilist));
temp->x = val;
temp->next = NULL;
if(!head){
head = temp;
tail = temp;
}else{
tail->next = temp;
}
}
int main()
{
int y;
for(y = 0; y<10; y++)
add_to_List(y);
while(head){
printf("%d\n",head->x);
head=head->next;
}
return 0;
}
和我的输出是:
0
9