The code is compiled, but only one list is output, in the order in which it was created. And the reverse function, which puts elements in reverse order, is for some reason completely useless - I call it, but nothing happens. At the exit the list remains unchanged.
Maybe the problem is that I am writing this function before the list itself is created?
struct Data { int a; }; struct List { Data d; List * next; }; // Рекурсия, инверсия списка List * reverse(List *u) { if (u == 0) return 0; if (u->next == 0) return u; List * tmp = reverse(u->next); u->next->next = u; u->next = 0; return tmp; } // Находим значения списка bool Find_Item_Single_List(List * u, int DataItem) { List *ptr; ptr = u; while (ptr != NULL) { if (DataItem == ptr->da) return true; else ptr = ptr->next; } return false; } int main(int argc, char** argv) { List *u = NULL; // 1-й узел u = new List; // Объявление u->da = 3; u->next = NULL; // Указатель на следующий элемент // Для удобства создаем переменную-указатель, которая хранит адрес последнего элемента List *x; // 2-й узел x = u; x->next = new List; x = x->next; x->da = 5; x->next = NULL; // List *c; // 3-й узел c = x; c->next = new List; c = c->next; c->da = 1; c->next = NULL; // List *b; // 4-й узел b = c; b->next = new List; b = b->next; b->da = 9; b->next = NULL; // Вывод (просто идем по списку) while(u) { cout << u->da << endl; u = u->next; } reverse(u); return 0; }
reverse(u);the program ends immediately. You do not display an inverted list. - VTTreversefunction returns a pointer to the beginning of the inverted list. This pointer should be remembered and starting with it to display an inverted list. - AnT