Mephistopheles_

List with some functions

Mar 22nd, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 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. using namespace std;
  13. random_device r;
  14. struct  List {
  15.     int inf;
  16.     List* pr = nullptr;
  17.     List(int a, char) {
  18.         this->inf = a;
  19.     }
  20.     List(int a) {
  21.         int j = 10;
  22.         this->inf = j;
  23.         List* w = new List(--j, char());
  24.         this->pr = w;
  25.         for (int i = 0; i < a - 2; ++i) {
  26.             w->pr= new List(--j, char());
  27.             w = w->pr;
  28.         }
  29.     }
  30. };
  31. void output(List* s) {
  32.     while (1) {
  33.         cout << s->inf << ' ';
  34.         if (s->pr == nullptr)break;
  35.         s=s->pr;
  36.     }
  37.     cout << '\n';
  38. }
  39. void transform(List& y, int a) {
  40.     vector<int>v, v1;
  41.     List* w = &y;
  42.     int i = 1;
  43.     while (1) {
  44.         if (i < a)v.push_back(w->inf);
  45.         else v1.push_back(w->inf);
  46.         if (w->pr == nullptr)break;
  47.         w = w->pr;
  48.         ++i;
  49.     }
  50.     copy(v.begin(), v.end(), back_inserter(v1));
  51.     w = &y;
  52.     i = 0;
  53.     while (1) {
  54.         w->inf = v1[i];
  55.         ++i;
  56.         if (w->pr == nullptr)break;
  57.         w = w->pr;
  58.     }
  59. }
  60. List& insert(List& s, int a) {
  61.     bool b = 0;
  62.     List* w = &s;
  63.     if (a > w->inf) {
  64.         List* p = new List(a, char());
  65.         p->pr = w;//
  66.         return *p;
  67.     }
  68.     while (w->pr != nullptr) {
  69.         if (a > w->pr->inf) {
  70.             List* q= new List(a, char());
  71.             q->pr = w->pr;
  72.             w->pr = q;
  73.             b = 1;
  74.         }
  75.         if (b)break;
  76.         w = w->pr;
  77.     }
  78.     if (!b)
  79.         w->pr = new List(a, char());
  80.     return s;
  81. }
  82. void erase(List& s) {
  83.     map<int, int>m;
  84.     List* w = &s;
  85.     while(1){
  86.         if (m.find(w->inf) == m.end()) {
  87.             m.insert({ w->inf,1 });
  88.         }
  89.         else
  90.             ++m[w->inf];
  91.         if (w->pr == nullptr)break;
  92.         w = w->pr;
  93.     }
  94.     bool b = 0;
  95.     List* q;
  96.     while (m.size() > 0) {
  97.         auto a = m.begin();
  98.         while ((*a).second != 1) {
  99.             b = 0;
  100.             w = &s;
  101.             while (w->pr != nullptr) {
  102.                 if (w->pr->inf == (*a).first) {
  103.                     q = w->pr->pr;
  104.                     delete w->pr;
  105.                     w->pr =q;
  106.                     b = 1;
  107.                     --(*a).second;
  108.                     break;
  109.                 }
  110.                 w = w->pr;
  111.             }
  112.         }
  113.         m.erase(m.begin());
  114.     }
  115. }
  116. int main() {
  117.     List L1(5); //создание списка на 5 элементов
  118.     output(&L1); //вывод списка
  119.     transform(L1, 3); //переформирование списка
  120.     output(&L1);
  121.     List L2 = insert(L1, 9); //вставка узла (после переворачивания список получается не отсортированный, поэтому вставляется перед первым меньшим элементом)
  122.     output(&L2);
  123.     erase(L2); //удаление повторяющихся элементов
  124.     output(&L2);
  125.     system("pause");
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment