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; } 
  • one
    The fact is that after calling reverse(u); the program ends immediately. You do not display an inverted list. - VTT
  • ... And not only "do not display", but even do not save the result of turning over. The reverse function 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
  • github.com/samrrr/LABS4/blob/master/L2/code.cpp Class SET2 list. The code that I see from c ++ has only cin cout. But in c ++ there are classes. - Nikita Samoukov

1 answer 1

It can not work in any way because you take your own output cycle

 while(u) { cout << u->da << endl; u = u->next; } 

"spoiled" the value of u . After this loop, u contains a null pointer. Access to the top of your list you lost. Call

 reverse(u); 

receives this null pointer as an input and, of course, does nothing at all. On this you download the program.

First, do not port u value to your output cycle. u is a pointer to the beginning of your list. You need to take care of it like the apple of your eye. Do not use the variable u for extraneous purposes.

If you want to display a list - get a separate variable to go through the list.

 for (const List *p = u; p != NULL; p = p->next) cout << p->da << endl; 

Secondly, the reverse function reverses your list and returns you a new pointer to its new beginning. Do not forget to save this value. This is where it is reasonable to save it in u

 u = reverse(u); 

Thirdly, if you want to print an inverted list - then print it. You don't have the slightest attempt to print an inverted list in your code. Not surprisingly, nothing is printed. Again

 for (const List *p = u; p != NULL; p = p->next) cout << p->da << endl; 

Of course, to print your list, it makes sense to start a separate function, and not to blurt the same cycle over and over again in the program text.

  • Excuse me) it is possible to ask a question out of place) but what is your experience in programming, what do you know so well? I would also like to have such knowledge once) - Den Simachov
  • @DenSimachov specifically on this issue is enough a couple of months of diligent learning to freely understand what is happening. I mean the List container, pointers, loops, output streams and the ability to read someone else's code - Mishakov Maxim