リストの最後に新しいノードを追加すると、次のようにうまく機能します。
typedef struct node* edge;
struct node
{
int data;
edge next;
};
void add(edge start, int val)
{
edge n = malloc(sizeof(struct node));
n->data = val;
n->next = NULL;
while (start->next)
start = start->next;
start->next = n;
}
ただし、本体を次のように変更するadd()
と:
edge n = malloc(sizeof(struct node));
n->data = val;
n->next = start;
start = n;
何も追加されません。
新しいノードがリストの新しい開始点になり、前の開始点が 2 番目になることを期待していました。最初の実装が期待どおりに機能し、2 番目の実装が機能しないのはなぜですか? 2 番目の方法に期待していた機能を実装する最良の方法は何ですか? void 関数で実行できますか?