Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include <functional>
- #include<string>
- #include<algorithm>
- #include <tuple>
- #include<random>
- #include <iomanip>
- #include <iterator>
- #include<set>
- #include<map>
- using namespace std;
- random_device r;
- struct List {
- int inf;
- List* pr = nullptr;
- List(int a, char) {
- this->inf = a;
- }
- List(int a) {
- int j = 10;
- this->inf = j;
- List* w = new List(--j, char());
- this->pr = w;
- for (int i = 0; i < a - 2; ++i) {
- w->pr= new List(--j, char());
- w = w->pr;
- }
- }
- };
- void output(List* s) {
- while (1) {
- cout << s->inf << ' ';
- if (s->pr == nullptr)break;
- s=s->pr;
- }
- cout << '\n';
- }
- void transform(List& y, int a) {
- vector<int>v, v1;
- List* w = &y;
- int i = 1;
- while (1) {
- if (i < a)v.push_back(w->inf);
- else v1.push_back(w->inf);
- if (w->pr == nullptr)break;
- w = w->pr;
- ++i;
- }
- copy(v.begin(), v.end(), back_inserter(v1));
- w = &y;
- i = 0;
- while (1) {
- w->inf = v1[i];
- ++i;
- if (w->pr == nullptr)break;
- w = w->pr;
- }
- }
- List& insert(List& s, int a) {
- bool b = 0;
- List* w = &s;
- if (a > w->inf) {
- List* p = new List(a, char());
- p->pr = w;//
- return *p;
- }
- while (w->pr != nullptr) {
- if (a > w->pr->inf) {
- List* q= new List(a, char());
- q->pr = w->pr;
- w->pr = q;
- b = 1;
- }
- if (b)break;
- w = w->pr;
- }
- if (!b)
- w->pr = new List(a, char());
- return s;
- }
- void erase(List& s) {
- map<int, int>m;
- List* w = &s;
- while(1){
- if (m.find(w->inf) == m.end()) {
- m.insert({ w->inf,1 });
- }
- else
- ++m[w->inf];
- if (w->pr == nullptr)break;
- w = w->pr;
- }
- bool b = 0;
- List* q;
- while (m.size() > 0) {
- auto a = m.begin();
- while ((*a).second != 1) {
- b = 0;
- w = &s;
- while (w->pr != nullptr) {
- if (w->pr->inf == (*a).first) {
- q = w->pr->pr;
- delete w->pr;
- w->pr =q;
- b = 1;
- --(*a).second;
- break;
- }
- w = w->pr;
- }
- }
- m.erase(m.begin());
- }
- }
- int main() {
- List L1(5); //создание списка на 5 элементов
- output(&L1); //вывод списка
- transform(L1, 3); //переформирование списка
- output(&L1);
- List L2 = insert(L1, 9); //вставка узла (после переворачивания список получается не отсортированный, поэтому вставляется перед первым меньшим элементом)
- output(&L2);
- erase(L2); //удаление повторяющихся элементов
- output(&L2);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment