Mephistopheles_

Bidirectional dynamic list

Mar 24th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include <functional>
  4. #include<string>
  5. #include<algorithm>
  6. #include <tuple>
  7. #include<random>
  8. #include <iomanip>
  9. #include <iterator>
  10. #include<set>
  11. #include<map>
  12. #include<string>
  13. using namespace std;
  14. random_device r;
  15. struct  List {
  16.     int number;
  17.     string date;
  18.     int code;
  19.     List* priv = nullptr;
  20.     List* next = nullptr;
  21.     List(const int&, string, const int&);
  22.     List(const int&);
  23.     List(const List&);
  24. };
  25. void outputfr(List*);
  26.  
  27. void outputbh(List*);
  28.  
  29. void insert(List* , const int&, string, const int&);
  30.  
  31. void eraze(List* , const int&);
  32.  
  33. List& moving(List* );
  34.  
  35. void quantity(List* );
  36.  
  37. int main() {
  38.  
  39.     List p(3);   // создание и ожидание ввода 3 элементов (сначала вводится Номер мед. полиса, потом  Дата обращения, после Код диагноза)
  40.     outputfr(&p); // вывод от первого к последнему
  41.     outputbh(&p); // вывод от последнего к первому
  42.     insert(&p, 2, "7 November", 6); //вставка нового узла
  43.     outputfr(&p);
  44.     eraze(&p, 1); // удаление всех узлов с заданным значением кода диагноза
  45.     outputfr(&p);
  46.     List q = moving(&p);  // перемещение всех узлов с одинаковым мед. полисом
  47.     outputfr(&q);
  48.     quantity(&p); //Определение  количества обращений в одну и туже дату содним и тем же диагнозом и вывод
  49. }
  50. List::List(const int& p, string s, const int& o) {
  51.     number = p;
  52.     date = s;
  53.     code = o;
  54. }
  55. List::List(const int& a) {
  56.     List* w = this;
  57.     List* q = nullptr;
  58.     int p, o; string s;
  59.     cin >> p >> s >> o;
  60.     this->number = p;
  61.     this->date = s;
  62.     this->code = o;
  63.     for (int i = 0; i < a - 1; ++i) {
  64.         cin >> p >> s >> o;
  65.         w->next = new List(p, s, o);
  66.         q = w;
  67.         w = w->next;
  68.         w->priv = q;
  69.     }
  70.     w->next = this;
  71.     this->priv = w;
  72.  
  73. }
  74. List::List(const List& p) {
  75.         this->next = p.next;
  76.         this->priv = p.priv;
  77.         this->code = p.code;
  78.         this->date = p.date;
  79.         this->number = p.number;
  80.         this->next->priv = this;
  81.         this->priv->next = this;
  82.         delete& p;
  83. }
  84. void outputfr(List* s) {
  85.     List* q = s;
  86.     while (1) {
  87.         cout << s->number << ' ' << s->date << ' ' << s->code << "     ";
  88.         s = s->next;
  89.         if (s == q)break;
  90.     }
  91.     cout << '\n';
  92. }
  93. void outputbh(List* s) {
  94.     s = s->priv;
  95.     List* q = s;
  96.     while (1) {
  97.         cout << s->number << ' ' << s->date << ' ' << s->code << "     ";
  98.         s = s->priv;
  99.         if (s == q)break;
  100.     }
  101.     cout << '\n';
  102. }
  103. void insert(List* s, const int& a, string u, const int& p) {
  104.     List* w = s;
  105.     List* f = s;
  106.     if (w->number == a) {
  107.         w = new List(a, u, p);
  108.         w->next = s->next;
  109.         s->next->priv = w;
  110.         s->next = w;
  111.         w->priv = s;
  112.         swap(w->code, s->code);
  113.         swap(w->date, s->date);
  114.         return;
  115.     }
  116.     s = s->next;
  117.     while (s != f) {
  118.         if (s->number == a) {
  119.             List* q = new List(a, u, p);
  120.             s->priv->next = q;
  121.             q->priv = s->priv;
  122.             q->next = s;
  123.             s->priv = q;
  124.             return;
  125.         }
  126.         if (s == f->priv) {
  127.             List* q = new List(a, u, p);
  128.             s->next = q;
  129.             q->priv = s;
  130.             q->next = f;
  131.             f->priv = q;
  132.             return;
  133.         }
  134.         s = s->next;
  135.     }
  136. }
  137. void eraze(List* s, const int& p) {
  138.     List* q = s;
  139.     List* f = s;
  140.     vector<tuple<int, string, int>>v;
  141.     do {
  142.         if (s->code != p)
  143.             v.push_back({ s->number,s->date,s->code });
  144.         s = s->next;
  145.     } while (s != f);
  146.     s = q;
  147.     int i = 0;
  148.     do {
  149.         if (i >= v.size()) {
  150.             s = s->next;
  151.             delete s->priv;
  152.             continue;
  153.         }
  154.         s = s->next;
  155.         ++i;
  156.     } while (s != f);
  157.     s = q;
  158.     for (int i = 0; i < v.size(); ++i) {
  159.         s->number = get<0>(v[i]);
  160.         s->date = get<1>(v[i]);
  161.         s->code = get<2>(v[i]);
  162.         if (i + 1 == v.size()) {
  163.             s->next = q;
  164.             q->priv = s;
  165.         }
  166.         s = s->next;
  167.     }
  168. }
  169. List& moving(List* s) {
  170.     map<int, vector<pair<string, int>>>m;
  171.     List* f = s;
  172.     do {
  173.         if (m.find(s->number) == m.end())
  174.             m.insert({ s->number,{} });
  175.         m[s->number].push_back({ s->date,s->code });
  176.         s = s->next;
  177.     } while (s != f);
  178.     List* q = nullptr; List* w = nullptr; List* z;
  179.     bool d = 0;
  180.     for (auto& a : m) {
  181.         if (a.second.size() > 1) {
  182.             for (auto& b : a.second) {
  183.                 if (!d) {
  184.                     q = new List(a.first, b.first, b.second);
  185.                     d = 1;
  186.                     w = q;
  187.                     continue;
  188.                 }
  189.                 w->next = new List(a.first, b.first, b.second);
  190.                 z = w;
  191.                 w = w->next;
  192.                 w->priv = z;
  193.             }
  194.         }
  195.     }
  196.     if (q != nullptr && w != nullptr) {
  197.         w->next = q;
  198.         q->priv = w;
  199.     }
  200.     return *q;
  201.  
  202. }
  203. void quantity(List* s) {
  204.     List* f = s;
  205.     map<string, map<int, int>>m;
  206.     map<int, int>j;
  207.     do {
  208.         auto a = m.find(s->date);
  209.         if (a == m.end()) {
  210.             j.insert({ s->code,1 });
  211.             m.insert({ s->date,j });
  212.             j.clear();
  213.         }
  214.         else {
  215.             if ((*a).second.find(s->code) == (*a).second.end())
  216.                 (*a).second.insert({ s->code,1 });
  217.             else
  218.                 ++(*a).second.find(s->code);
  219.         }
  220.         s = s->next;
  221.     } while (s != f);
  222.     for (auto& a : m) {
  223.         cout << "Date: " << a.first << '\n';
  224.         cout << "   " << "Diagnosis code : number" << '\n';
  225.         for (auto& b : a.second)
  226.             cout << "   " << setw(14) << b.first << " : " << b.second << '\n';
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment