There was a problem with the implementation of the algorithm, which inverts the elements of the list between the maximum and minimum numbers. Simply put, if there is a list (2 1 4 5 12 5), then eventually it should be converted to (2 12 5 4 1 5), since the maximum element = 12, and the minimum = 1. Creating, filling and searching the maximum and the minimum element I implemented, but how to invert what is between them ... this is a problem for me. I would be very grateful for the help. Here is my code:
#include "stdafx.h" #include "iostream" #include "conio.h" #include "math.h" using namespace std; typedef float Tdata; struct Tnode { Tdata info; Tnode *next; }; typedef Tnode* Tlist; Tlist L=0; void output(Tlist L); void create_L(Tlist &L, int n); Tlist min(Tlist L); Tlist max(Tlist L); void change(Tlist &L); // Π€ΡΠ½ΠΊΡΠΈΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ ΠΏΠΎΡΡΠ΄ΠΊΠ° ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΌΠ΅ΠΆΠ΄Ρ max ΠΈ min int main () { setlocale(0,"Rus"); int n; cout<<"ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠ·Π»ΠΎΠ² "; cin>>n; create_L(L,n); cout<<"ΠΠ²Π΅Π΄ΡΠ½Π½ΡΠ΅ ΡΠΈΡΠ»Π°: "; output(L); //Tlist L_min=min(L); //cout<<"ΠΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅: "<<L_min->info<<endl; //Tlist L_max=max(L); //cout<<"ΠΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅: "<<L_max->info<<endl; change(L); cout<<"Π§ΠΈΡΠ»Π° ΠΏΠΎΡΠ»Π΅ ΡΠΎΡΡΠΈΡΠΎΠ²ΠΊΠΈ: "; output(L); _getch(); return 0; } void output(Tlist L) // ΠΡΠ²ΠΎΠ΄ ΡΠΏΠΈΡΠΊΠ° { Tlist q=L; while (q) { cout<<(q->info)<<" "; q=q->next; } cout<<endl; } void create_L(Tlist &L, int n) // ΠΠ°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ ΡΠΏΠΈΡΠΊΠ° { L=new Tnode; cout<<"ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠΈΡΠ»ΠΎ "; cin>>(L->info); L->next=0; Tnode *q1, *q2; q1=L; for(int i=1; i<n; i++) { q2 = new Tnode; cout<<"ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠΈΡΠ»ΠΎ "; cin>>(q2->info); q2->next=0; q1->next=q2; q1=q2; } } Tlist min(Tlist L) { Tlist q=L; Tlist q_min; q_min=q; q=q->next; while (q) { if ((q_min->info)>(q->info)) q_min=q; q=q->next; } return q_min; // ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ ΡΠ·Π΅Π» ΡΠΏΠΈΡΠΊΠ° } Tlist max(Tlist L) { Tlist q=L; Tlist q_max; q_max=q; q=q->next; while (q) { if ((q_max->info)<(q->info)) q_max=q; q=q->next; } return q_max; // ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ ΡΠ·Π΅Π» ΡΠΏΠΈΡΠΊΠ° } void change(Tlist &L) { Tlist q=L; float temp1; float L_min=min(L)->info; // ΠΡΠ·ΠΎΠ² ΡΡΠ½ΠΊΡΠΈΠΈ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΡΠΈΡΠ»Π° float L_max=max(L)->info; // ΠΡΠ·ΠΎΠ² ΡΡΠ½ΠΊΡΠΈΠΈ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΡΠΈΡΠ»Π° while (q) { if ((q->info)==(L_min)) q->info=L_max; else if ((q->info)==(L_max)) q->info=L_min; q=q->next; } }