There is a samopisny single-linked list. Stores data and a pointer to the next item.

void insert(List **begin, List * newinsert)//передаем указатель на начало и элемент который нужно вставить { List * insert = newinsert;//записываем данные которые будем вставлять if(*begin == NULL)//если список пустой, вставляем все данные в начало. тут всё работает как надо. { *begin = insert; (*begin)->next = NULL; return; } List * temp = *begin;//если список не пустой, запишем указатель на начало в временной переменную while(temp) {//пока переменная не равна нулю, переходим к следующему элементу temp=temp->next; } if(!temp) {//когда переменная всё же ноль, я хочу записать в неё данные temp = insert; temp->next = NULL; } } 

But in this case, nothing works. When outputting to the console, only the first item is displayed. What could be a mistake. It seems everything should be simple, but not grown together.

    2 answers 2

    Because the variable temp takes the value null and then the new element is written to the temporary variable.

    Try this:

     List* current = *begin; while (current) { if (current->next) { // Не достигли конца списка. current = current->next; } else { // Конец списка, вставляем элемент. current->next = insert; insert->next = NULL; break; } } 
    • thank. got it - V. Rotenberh

    This is because no one references your item. You need to make next last element point to your new element.