以下の交差関数でプッシュの最初の呼び出しが行われると、tail-> nextの値はNULLになります。私の理解は、& tail-> nextは、次にポインタを保持するスタック上のダミーオブジェクトの最後の4バイトを指しているということです。今、head_refがpush関数の中で変更されたとき、ダミーオブジェクトの次の変数の中に格納されているアドレスを変更していませんか?だから、ダミーは常にリストの最後の要素を指しているように見えますが、これは実行時にリストの先頭を正しく出力します。誰かがプッシュコールごとに何が起こっているのかを説明できますか?ありがとう。リンクされたリストメモリ
この問題は、ダミーの次のポインタを変更するんpush
にhere
struct node* sortedIntersect(struct node* a, struct node* b)
{
struct node dummy;
struct node* tail = &dummy;
dummy.next = NULL;
while (a != NULL && b != NULL)
{
if (a->data == b->data)
{
push((&tail->next), a->data);
tail = tail->next;
a = a->next;
b = b->next;
}
else if (a->data < b->data) /* advance the smaller list */
a = a->next;
else
b = b->next;
}
return(dummy.next);
}
void push(struct node** head_ref, int new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
'、 '&tail-> nextはNULLを指すノードまたは空のノードのアドレスを保持しますか?したがって、 '* head_ref = new_node'が呼び出されたとき、tailの次のノードを新しく作成されたノードに設定します。あなたのプッシュの最後の行はそれを自分自身に戻しませんか? 'old_head'と' *head_ref'が実際にどのような値を格納しているのか分かりません。 – exozt912