私は単独でリンクされたリストに取り組んでおり、問題を解決することができません(問題はNULLポインタを使って関数を追加していると思います)。問題はリストに最初の数値を追加して残りをスキップすることです関数の追加の呼び出しの。理解できません
#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