There is a function for finding the minimum and maximum values. It is necessary to swap the maximum and minimum element in the bidirectional list through the redirection of links.

void Swap(Spis2 **b) { Spis2 *t = *b; Spis2 *max = *b; Spis2 *min = *b; int x; while (t->next != NULL) { if (t->next->info > max->info) { max = t->next; x = 0; } else if (t->next->info < min->info) { min = t->next; x = 1; } else t = t->next; } cout << "max = " << max->info << endl; cout << "min = " << min->info << endl; } 
  • And the question is what? - Akina
  • Where is the problem? .. - MihailPw
  • question in the assignment, I do not quite understand how to swap these elements - hope_op
  • @hope_op As far as I understand, you provided us with only functions for replacing elements. And where are the declared functions for finding the minimum and maximum elements? - Roman Podymov

2 answers 2

 #include <fstream> #include <iostream> struct Node { int data; Node* prev; Node* next; Node(int d) : data(d), prev(nullptr), next(nullptr) {}; }; class List { private: Node* _beg; Node* _end; public: List() : _beg(nullptr), _end(nullptr) {}; void push_back(int d); Node& max() const; Node& min() const; Node* begin() const {return _beg;}; friend std::ostream& operator<< (std::ostream& os, const List& list); }; std::ostream& operator<< (std::ostream& os, const List& list) { Node* cur = list.begin(); while(cur != nullptr) { os << cur->data << " "; cur = cur->next; } return os; } void List::push_back(int d) { Node* node = new Node(d); if(_beg == nullptr) { _beg = _end = node; } else { node->prev = _end; _end->next = node; _end = node; } } Node& List::max() const { Node* beg = _beg; Node* next = _beg->next; Node* res = nullptr; while(next != nullptr) { if(beg->data < next->data) { res = next; beg = next; } next = next->next; } return *res; } Node& List::min() const { Node* beg = _beg; Node* next = _beg->next; Node* res = nullptr; while(next != nullptr) { if(beg->data > next->data) { res = next; beg = next; } next = next->next; } return *res; } void swapNode(Node& max, Node& min) { Node* tmpPrev = min.prev; Node* tmpNext = min.next; min.prev->next = &max; min.next->prev = &max; min.next = max.next; min.prev = max.prev; max.prev->next = &min; max.next->prev = &min; max.prev = tmpPrev; max.next = tmpNext; } int main() { List list; list.push_back(3); list.push_back(9); list.push_back(1); list.push_back(2); list.push_back(4); list.push_back(100); list.push_back(2); list.push_back(5); Node& max = list.max(); Node& min = list.min(); std::cout << list << std::endl; swapNode(max, min); std::cout << list; return 0; } 

    I did this: I took not a pointer to an element but a pointer to a pointer to an element. Something like this for unidirectional:

     void Swap(Spis2 **b) { Spis2 *t = *b; Spis2 **max = b; Spis2 **min = b; int x; while (t->next != NULL) { if (t->next->info > (*max)->info) { max = &t->next; x = 0; } else if (t->next->info < (*min)->info) { min = &t->next; x = 1; } else t = t->next; } cout << "max = " << (*max)->info << endl; cout << "min = " << (*min)->info << endl; swap((*max)->next,(*min)->next); swap((*max),(*min)); } 

    For bidirectional, prev must be corrected.