How to move the minimum element after the maximum in the list.

Given a list of sequences of natural numbers. Add to the list after the maximum element of the minimum.

I have half the program but I do not know how to do it.

#include <iostream> #include <stdlib.h> #include <conio.h> #include <stdio.h> using namespace std; struct List { int n; List *next;} *start, *last; void min1(void) { int min; List *smin,*g; min=start->n; g=start->next; while(g!=0){ //Поиск ΠΌΠΈΠ½ if(min>g->n) { min=g->n; smin=g; } g=g->next; } printf("\n"); printf ("Min=%d",min); } void max1(void) { int max; List *smin,*smax,*ssmin,*ssmax,*g; max=start->n; g=start->next; while(g!=0){ if(max<g->n) { max=g->n; smax=g; //Поиск макс } g=g->next; } printf("\n"); printf ("Max=%d",max); } void change( void ) { List *s, *l; s = start; l = last; int h; h = s -> n; s -> n = l -> n; // МСняСм мСстами ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ ΠΈ послСдний l -> n = h; } void fill_list( int N ) { List *p; int i; if ( start == NULL ) { start = new List; start -> n = 1; start -> next = NULL; last = start; // ЗаполняСм список } for( i = 2; i <= N; i++ ) { p = new List; p -> n = i; p -> next = NULL; last -> next = p; last = p; } } int main() { start = NULL; last = NULL; List *p1, *p2, *p,*g; printf( "VVedite razmer spiska\n" ); int M,max,min; scanf( "%d", &M ); //Π’Π²ΠΎΠ΄ΠΈΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€ списка fill_list( M ); //ЗаполняСм p = start; while( p != NULL ) { printf("| %2d |", p -> n ); //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ p = p -> next; } max1(); min1(); change(); p = start; printf("\n"); while( p != NULL ) { printf("| %2d |", p -> n ); //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ смСну послСднСго ΠΈ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ элСмСнт p = p -> next; } p = start; p1 = start; p2 = NULL; while (p != NULL) { p1 = p -> next; delete p; p = p1; } getch(); } 
  • To bypass all elements of the list, remembering pointers to the element with the minimum and the element with the maximum value. Then in the next field of the element with the maximum value, write the previously stored pointer to the element with the minimum value. Now you know how to do it. - Sergey Gornostaev
  • Can't you write a snippet how to do it? I’m just learning lists, and I’m not good at it - Yura
  • one
    There is already an answer to the same question. - avp

0