Filage

Lab5_3

Mar 19th, 2024
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6.     node* next;
  7.     node* prev;
  8.     int info;
  9. };
  10.  
  11. void createList(node*& Begin, node*& End, int n) {
  12.     for (int i = 0; i < n; i++) {
  13.         node* el = new node;
  14.         el->info = rand() % 101 - 50;
  15.         if (End == nullptr) {
  16.             el->next = NULL;
  17.             el->prev = NULL;
  18.             Begin = End = el;
  19.         }
  20.         else {
  21.             el->next = NULL;
  22.             el->prev = End;
  23.             End->next = el;
  24.             End = el;
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30. bool isEmpty(node* Begin) {
  31.     if (Begin == nullptr)
  32.         cout << "Очередь пуста\n";
  33.     return Begin == nullptr;
  34. }
  35.  
  36. void outList(node* Begin) {
  37.     if (isEmpty(Begin))
  38.         return;
  39.     node* temp = Begin;
  40.     while (temp != NULL) {
  41.         cout << temp->info << " ";
  42.         temp = temp->next;
  43.     }
  44. }  
  45.  
  46. node* search_min(node*& Begin, node*& End, int& min_pos)
  47. {
  48.     int i = 1;
  49.  
  50.     node* min = Begin;
  51.     node* spt = Begin;
  52.     while (spt != nullptr)
  53.     {
  54.         if (spt->info < min->info) { min = spt; min_pos = i; }
  55.         spt = spt->next;
  56.         i++;
  57.     }
  58.     return min;
  59. }
  60.  
  61. node* search_max(node*& Begin, node*& End, int& max_pos)
  62. {
  63.     int i = 1;
  64.     node* max = Begin;
  65.     node* spt = Begin;
  66.     while (spt != nullptr)
  67.     {
  68.         if (spt->info > max->info) { max = spt; max_pos = i; }
  69.         spt = spt->next;
  70.         i++;
  71.     }
  72.     return max;
  73. }
  74.  
  75. void del_between(node*& Begin, node*& End)
  76. {
  77.     int min_pos = 0, max_pos = 0;
  78.     node* min = search_min(Begin, End, min_pos);
  79.     node* max = search_max(Begin, End, max_pos);
  80.  
  81.     if (min_pos == max_pos) {
  82.         std::cout << "Пустая область между min и max" << std::endl;
  83.         return;
  84.     }
  85.  
  86.     if (min_pos > max_pos) {
  87.         node* temp = min;
  88.         min = max;
  89.         max = temp;
  90.     }
  91.  
  92.     node* saveToFree;
  93.     while (min->next != max) {
  94.         saveToFree = min->next;
  95.         min->next = min->next->next;
  96.         delete saveToFree;
  97.     }
  98.  
  99.     min_pos = 0;
  100.     max_pos = 0;
  101. }
  102.  
  103.  
  104. int main() {
  105.     srand((unsigned)(time(NULL)));
  106.     setlocale(LC_ALL, "Rus");
  107.     int n;
  108.     node* Begin = nullptr, * End = nullptr;
  109.     cout << "Введите количество узлов: ";
  110.     cin >> n;
  111.     createList(Begin, End, n);
  112.     cout << endl;
  113.     outList(Begin);
  114.     del_between(Begin, End);
  115.     cout << endl << endl;
  116.     outList(Begin);
  117.     return 0;
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment