Advertisement
cacodemon665

Лаба 14 Вариант 7

Oct 25th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include "pch.h" //для версий вижлы старше последних версий 2017 здесь должно быть #include "stdafx.h"
  2. #include <iostream>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. struct QueueNode
  8. {
  9.     QueueNode * next;
  10.     int value;
  11. };
  12.  
  13. void Add(QueueNode **front, QueueNode **back, int value)
  14. {
  15.     QueueNode * temp = new QueueNode;
  16.     temp->value = value;
  17.     temp->next = nullptr;
  18.  
  19.     if (*front && *back)
  20.     {
  21.         (*back)->next = temp;
  22.         (*back) = temp;
  23.     }
  24.     else
  25.     {
  26.         *front = *back = temp;
  27.     }
  28. }
  29.  
  30. void Print(QueueNode * node)
  31. {
  32.     if (!node)
  33.         return;
  34.  
  35.     cout << node->value << " ";
  36.  
  37.     Print(node->next);
  38. }
  39.  
  40. void DeleteAll(QueueNode *front)
  41. {
  42.     if (front)
  43.     {
  44.         DeleteAll(front->next);
  45.         delete front;
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51.     QueueNode *front = nullptr, *back = nullptr;
  52.     const int queue_size = 20;
  53.  
  54.     srand(time(0));
  55.  
  56.     for (int i = 0; i < queue_size; i++)
  57.     {
  58.         Add(&front, &back, rand() % 101 - 50);
  59.     }
  60.  
  61.     Print(front);
  62.     cout << endl;
  63.  
  64.     int max = -INT_MAX, min = INT_MAX;
  65.     QueueNode *max_node = nullptr,
  66.               *min_node = nullptr,
  67.               *temp = front;
  68.  
  69.     while (temp)
  70.     {
  71.         int v = temp->value;
  72.  
  73.         if (v < 0 && v > max)
  74.         {
  75.             max = v;
  76.             max_node = temp;
  77.         }
  78.  
  79.         if (v > 0 && v < min)
  80.         {
  81.             min = v;
  82.             min_node = temp;
  83.         }
  84.  
  85.         temp = temp->next;
  86.     }
  87.  
  88.    
  89.     if (min_node && max_node)
  90.     {
  91.         min_node->value = max;
  92.         max_node->value = min;
  93.     }
  94.     else
  95.     {
  96.         cout << "Error!";
  97.     }
  98.  
  99.     Print(front);
  100.     DeleteAll(front);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement