Я не могу понять смысл кода C о связанных списках, в которых используются двойные указатели. Вот код, который я читаю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
и *L
в функции loop
? Почему в основном, когда объявлено struct list *L = NULL
, я должен вызвать функцию insertBegin
с аргументом &L
, а не простым L
?
Я думаю, *L
является указателем на первый узел связанного списка, а **L
может указывать на любой элемент списка. Однако я не уверен, что это правильно.
Благодарим за помощь!
http://stackoverflow.com/a/897414/2318775 – avim
'temp = (struct list *) malloc (sizeof (temp));' -> 'temp = malloc (sizeof * temp);' – joop