Advertisement
Dambosin

var 14

Apr 5th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include  <time.h>
  4. struct tochd
  5. {
  6.     int inf;
  7.     tochd *l;
  8.     tochd *r;
  9. };
  10. using namespace std;
  11. void NewOchd(tochd **sl, tochd **sr) {
  12.     *sl = new tochd;
  13.     *sr = new tochd;
  14.     (*sl)->l = NULL;
  15.     (*sl)->r = *sr;
  16.     (*sr)->l = *sl;
  17.     (*sr)->r = NULL;
  18. }
  19.  
  20. void AddOchdR(tochd *sp, int inf) {
  21.     tochd *spt = new tochd;
  22.     spt->inf = inf;
  23.     spt->l = sp;
  24.     spt->r = sp->r;
  25.     sp->r->l = spt;
  26.     sp->r = spt;
  27. }
  28. void AddOchdL(tochd *sp, int inf) {
  29.     tochd *spt = new tochd;
  30.     spt->inf = inf;
  31.     spt->l = sp->l;
  32.     spt->r = sp;
  33.     sp->l->r = spt;
  34.     sp->l = spt;
  35. }
  36. int ReadOchdD(tochd *sp) {
  37.     int inf = sp->inf;
  38.     sp->l->r = sp->r;
  39.     sp->r->l = sp->l;
  40.     delete sp;
  41.     return inf;
  42. }
  43. void DelOchdAll(tochd **sl, tochd **sr)
  44. {
  45.     tochd *spt = (*sl)->r;
  46.     while (spt != *sr)
  47.     {
  48.         cout << ReadOchdD(spt) << " ";
  49.         spt = (*sl)->r;
  50.     }
  51.     cout << endl; *sl = NULL;
  52.     delete *sl; *sr = NULL;
  53.     delete *sr;
  54.     return;
  55. }
  56. void div2Ochd(tochd *sl, tochd *sr, tochd **slL,
  57.     tochd **srL, tochd **slR, tochd **srR) {
  58.     NewOchd(slL, srL);
  59.     NewOchd(slR, srR);
  60.     tochd *spt = sl->r;
  61.     while (spt != sr)
  62.     {
  63.         AddOchdL(*srL, ReadOchdD(spt));
  64.         spt = sl->r;
  65.         if (spt != sr)
  66.         {
  67.             AddOchdL(*srR, ReadOchdD(spt));
  68.             spt = sl->r;
  69.         }
  70.     }
  71. }
  72.  
  73. void slipOchd(tochd **sl, tochd **sr, tochd *slL, tochd *srL, tochd
  74.     *slR, tochd *srR)
  75. {
  76.     NewOchd(sl, sr);
  77.     tochd *sptL = slL->r;
  78.     tochd *sptR = slR->r;
  79.     while ((sptL != srL) && (sptR != srR))
  80.     {
  81.         if (sptL->inf < sptR->inf)
  82.         {
  83.             AddOchdL(*sr, ReadOchdD(sptL));
  84.             sptL = slL->r;
  85.         }
  86.         else
  87.         {
  88.             AddOchdL(*sr, ReadOchdD(sptR));
  89.             sptR = slR->r;
  90.         }
  91.     }
  92.     while (sptL != srL)
  93.     {
  94.         AddOchdL(*sr, ReadOchdD(sptL));
  95.         sptL = slL->r;
  96.     }
  97.     delete slL; delete srL;
  98.     while (sptR != srR)
  99.     {
  100.         AddOchdL(*sr, ReadOchdD(sptR));
  101.         sptR = slR->r;
  102.     }
  103.     delete slR; delete srR;
  104. }
  105.  
  106. void SotrSlipOchd(tochd **sl, tochd **sr) {
  107.     tochd *slL, *srL, *slR, *srR;
  108.     if ((*sl)->r == *sr || (*sl)->r->r == *sr) return;
  109.     div2Ochd(*sl, *sr, &slL, &srL, &slR, &srR);
  110.     SotrSlipOchd(&slL, &srL);
  111.     SotrSlipOchd(&slR, &srR);
  112.     slipOchd(sl, sr, slL, srL, slR, srR);
  113. }
  114.  
  115.  
  116. int main()
  117. {
  118.     setlocale(LC_ALL, "Russian");
  119.     srand(time(NULL));
  120.     int n;
  121.     cout << "Введите количество элементов ";
  122.     cin >> n;
  123.     tochd *sl, *sr;
  124.     NewOchd(&sl, &sr);
  125.     tochd *t = new tochd;
  126.     t->inf = -51;
  127.    
  128.     for (int i = 0; i < n; i++) {
  129.         int temp = rand() % 101 - 50;
  130.         cout << temp << " ";
  131.        
  132.         AddOchdL(sr, temp);
  133.         if (temp > t->inf) t = sr->l;
  134.     }
  135.     cout << endl;
  136.     int temp = sl->r->inf;
  137.     sl->r->inf = t->inf;
  138.     t->inf = temp;
  139.     t = sl->r;
  140.     while (t != sr) {
  141.  
  142.         cout << t->inf << " ";
  143.         t = t->r;
  144.     }
  145.     cout << endl;
  146.     tochd *spt = sl->r;
  147.     int x = 14;
  148.     int pos = 1;
  149.     while (spt != sr && spt->inf != x) {
  150.         pos++;
  151.         spt = spt->r;
  152.     }
  153.     if (spt != sr) {
  154.         cout << "Элемент с моим вариантом находится на "<<pos <<" позиции"<< endl;
  155.     }
  156.     else {
  157.         cout << "Нет моего варианта" << endl;
  158.     }
  159.     SotrSlipOchd(&sl, &sr);
  160.     DelOchdAll(&sl, &sr);
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement