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(); }
nextfield 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