Mephistopheles_

miniList

Mar 3rd, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. #include<algorithm>
  5. #include<random>
  6. using namespace std;
  7. random_device r;
  8. class L {
  9.     L* p = nullptr;
  10.     int inf;
  11. public:
  12.     L(int q) {
  13.         inf = q;
  14.     }
  15.     L* soz(int );
  16.     double sr();
  17.     void zm(int , int );
  18.     void men();
  19.     void vuv() {
  20.         cout << this->inf << ' ';
  21.         L* w = this->p;
  22.         while (w != nullptr) {
  23.             cout << w->inf << ' ';
  24.             w = w->p;
  25.         }
  26.         cout << '\n';
  27.     }
  28.    
  29. };
  30.  
  31. int main() {
  32.     L*u(0);
  33.     u = u->soz(5);
  34.     u->vuv();
  35.     u->zm(4,0);
  36.     u->vuv();
  37.     u->men();
  38.     u->vuv();
  39.  
  40.     system("pause");
  41.  
  42. }
  43. L* L::soz(int n) {
  44.     L* o = new L(r() % 5);
  45.     L* q = o;
  46.     for (int i = 1; i < n; ++i) {
  47.         L* v = new L(r() % 5);
  48.         o->p = v;
  49.         o = v;
  50.     }
  51.     return q;
  52. }
  53. double L:: sr() {
  54.         int s = this->inf;
  55.         int k = 1;
  56.         L* w = this->p;
  57.         while (w != nullptr) {
  58.             s += w->inf;
  59.             ++k;
  60.             w = w->p;
  61.         }
  62.         return s * 1.0 / k;
  63. }
  64. void L:: zm(int x, int y) {
  65.     if (this->inf == x) {
  66.          this->inf = y;
  67.     }
  68.     L* w = this->p;
  69.     while (w != nullptr) {
  70.         if (w->inf == x) {
  71.             w->inf = y;
  72.         }
  73.         w = w->p;
  74.     }
  75. }
  76. void L:: men() {
  77.     L* u = this;
  78.     L* w = this->p;
  79.     if (w != nullptr) {
  80.         while (w->p != nullptr) {
  81.             w = w->p;
  82.         }
  83.         swap(u->inf, w->inf);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment