1

二重ポインタを使用している連結リストに関する C コードの意味がわかりません。これが私が読んでいるコードです

    struct list
{
    int value;
    struct list *next;
};
//Insert an element at the begining of the linked list
void insertBegin(struct list **L, int val)
{
    //What does **L mean?
    //Memory allocation for the new element temp
    struct list *temp;
    temp = (struct list *)malloc(sizeof(temp));
    //The new element temp points towards the begining of the linked list L
    temp->next = *L;
    //Set the beginning of the linked list
    *L = temp;
    (*L)->value = val;
}
void loop(struct list *L)
{
    printf("Loop\n");
    //Run through all elements of the list and print them
    while( L != NULL )
    {
        printf("%d\n", L->value);
        L = L->next;
    }
}
struct list* searchElement(struct list *L,int elem)
{
    while(L != NULL)
    {
        if(L->value == elem)
        {
            printf("Yes\n");
            return L->next;
        }
        L = L->next;
    }
    printf("No\n");
    return NULL;
}

int main()
{
    struct list *L = NULL;
    insertBegin(&L,10); // Why do I need 
    return 0;
}

**LはどういうinsertElement意味ですか? 主にが宣言されているときに、単純ではなく引数を使用して関数を呼び出す必要があるのはなぜですか?**L*Lloopstruct list *L = NULLinsertBegin&LL

*Lリンクされたリストの最初のノードへのポインターであると思いますが**L、リストの任意の要素を指す場合があります。ただし、これが正しいかどうかはわかりません。

ご協力ありがとうございました!

4

5 に答える 5

1

L は、リストの最初のリンクのアドレスを格納します。したがって、*L はリストの最初のリンクの内容であり、&L はリストの最初のリンクのアドレスを格納する変数のアドレスです。

つまり、関数に引数を渡してリストにメモリを割り当てて初期化する唯一の方法は、&L を引数として指定することです。L を引数として渡すと、関数は最初のリンクのアドレスを受け取りますが、代わりに、最初のリンクのアドレスを格納する場所が必要です。

于 2015-03-12T16:35:44.613 に答える