Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 KB | None | 0 0
  1. #include <cstddef>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const string HELP_MSG = "0 – exit\n1 – add to begin\n2 – add to end\n3 – remove from begin\n4 – remove from end\n5 - print list\n6 – swap\n\n";
  7.  
  8. struct Node
  9. {
  10.     int value;
  11.     Node *next = NULL;
  12. };
  13.  
  14. //создать новый лист с одним элементом
  15. Node *init(int a)
  16. {
  17.     Node *node = new Node;
  18.     node->value = a;
  19.     return (node);
  20. };
  21.  
  22. void print(Node *list)
  23. {
  24.     Node *node = list;
  25.     while (node != NULL)
  26.     {
  27.         cout << node->value << " ";
  28.         node = node->next;
  29.     }
  30.     cout << endl;
  31. };
  32.  
  33. Node *push_back(Node *list, int a)
  34. {
  35.     if (list == NULL)
  36.     {
  37.         return init(a);
  38.     }
  39.  
  40.     Node *new_node = new Node;
  41.     new_node->value = a;
  42.     new_node->next = list;
  43.     return new_node;
  44. }
  45.  
  46. Node *push_front(Node *list, int a)
  47. {
  48.     if (list == NULL)
  49.     {
  50.         return init(a);
  51.     }
  52.  
  53.     Node *new_node = new Node;
  54.     new_node->value = a;
  55.  
  56.     Node *t = list;
  57.     while (t->next != NULL)
  58.     {
  59.         t = t->next;
  60.     }
  61.     t->next = new_node;
  62.  
  63.     return list;
  64. }
  65.  
  66. Node *pull_back(Node *list)
  67. {
  68.     if (list == NULL)
  69.     {
  70.         cout << "oops! error =(" << endl;
  71.         exit(1);
  72.     }
  73.  
  74.     return list->next;
  75. }
  76.  
  77. Node *pull_front(Node *list)
  78. {
  79.     if (list == NULL)
  80.     {
  81.         cout << "oops! error =(" << endl;
  82.         exit(1);
  83.     }
  84.  
  85.     if (list->next == NULL)
  86.         return NULL;
  87.  
  88.     Node *t = list;
  89.  
  90.     while (t->next->next != NULL)
  91.     {
  92.         t = t->next;
  93.     }
  94.     t->next = NULL;
  95.  
  96.     return list;
  97. }
  98.  
  99. int size(Node *list)
  100. {
  101.     int s = 0;
  102.     Node *t = list;
  103.  
  104.     while (t != NULL)
  105.     {
  106.         t = t->next;
  107.         s++;
  108.     }
  109.     return s;
  110. }
  111.  
  112. Node *get(Node *list, int index)
  113. {
  114.     if (size(list) <= index)
  115.     {
  116.         cout << "oops! error =(" << endl;
  117.         exit(1);
  118.     }
  119.  
  120.     int i = 0;
  121.     Node *t = list;
  122.  
  123.     while (i != index)
  124.     {
  125.         t = t->next;
  126.         i++;
  127.     }
  128.     return t;
  129. };
  130.  
  131. Node *swap(Node *list, int first_index, int second_index)
  132. {
  133.     int n = size(list);
  134.  
  135.     if (first_index < 0 || first_index >= n || second_index < 0 || second_index >= n)
  136.     {
  137.         cout << "oops! error =(" << endl;
  138.         exit(1);
  139.     }
  140.  
  141.     if (first_index == second_index)
  142.     {
  143.         return list;
  144.     }
  145.  
  146.     if (first_index > second_index)
  147.     {
  148.         int buf = first_index;
  149.         first_index = second_index;
  150.         second_index = buf;
  151.     }
  152.  
  153.     Node *first = get(list, first_index);
  154.     Node *second = get(list, second_index);
  155.  
  156.     if (first_index == 0)
  157.     {
  158.  
  159.         // F S ... (n - 1)
  160.         if (second_index == first_index + 1)
  161.         {
  162.             Node *second_next = second->next;
  163.  
  164.             second->next = first;
  165.             first->next = second_next;
  166.             list = second;
  167.         }
  168.  
  169.         // F ... S
  170.         else if (second_index == n - 1)
  171.         {
  172.             Node *second_prev = get(list, second_index - 1);
  173.             Node *first_next = first->next;
  174.  
  175.             second->next = first_next;
  176.             second_prev->next = first;
  177.             first->next = NULL;
  178.             list = second;
  179.         }
  180.  
  181.         // F ... S ... (n - 1)
  182.         else
  183.         {
  184.             Node *second_prev = get(list, second_index - 1);
  185.             Node *second_next = second->next;
  186.             Node *first_next = first->next;
  187.  
  188.             second->next = first_next;
  189.             second_prev = first;
  190.             first->next = second->next;
  191.             list = second;
  192.         }
  193.     }
  194.  
  195.     else if (first_index > 0)
  196.     {
  197.         // 0 ... F S
  198.         if (first_index == n - 2 && second_index == n - 1)
  199.         {
  200.             Node *first_prev = get(list, first_index - 1);
  201.  
  202.             first_prev->next = second;
  203.             second->next = first;
  204.             first->next = NULL;
  205.         }
  206.  
  207.         // 0 ... F ... S
  208.         else if (second_index == n - 1)
  209.         {
  210.             Node *first_prev = get(list, first_index - 1);
  211.             Node *first_next = first->next;
  212.             Node *second_prev = get(list, second_index - 1);
  213.  
  214.             first_prev->next = second;
  215.             second->next = first_next;
  216.             second_prev->next = first;
  217.             first->next = NULL;
  218.         }
  219.  
  220.         // 0 ... F S ... (n - 1)
  221.         else if (second_index == first_index + 1)
  222.         {
  223.             Node *first_prev = get(list, first_index - 1);
  224.             Node *second_next = second->next;
  225.  
  226.             first_prev->next = second;
  227.             second->next = first;
  228.             first->next = second_next;
  229.         }
  230.  
  231.         // 0 ... F ... S  ... (n - 1)
  232.         else
  233.         {
  234.             Node *first_prev = get(list, first_index - 1);
  235.             Node *first_next = first->next;
  236.             Node *second_prev = get(list, second_index - 1);
  237.             Node *second_next = second->next;
  238.  
  239.             first_prev->next = second;
  240.             second->next = first_next;
  241.             second_prev->next = first;
  242.             first->next = second_next;
  243.         }
  244.     }
  245.  
  246.     return list;
  247. };
  248.  
  249. Node *swap35(Node *list)
  250. {
  251.     int list_size = size(list);
  252.  
  253.     if (list_size < 2)
  254.     {
  255.         return list;
  256.     }
  257.  
  258.     int first_index = -1;
  259.     int second_index = -1;
  260.  
  261.     Node *t = list;
  262.     int i = 0;
  263.  
  264.     while (t != NULL)
  265.     {
  266.         if (t->value % 3 == 0)
  267.         {
  268.             first_index = i;
  269.             break;
  270.         }
  271.         i++;
  272.         t = t->next;
  273.     }
  274.  
  275.     t = list;
  276.     i = 0;
  277.  
  278.     while (t != NULL)
  279.     {
  280.         if (t->value % 5 == 0)
  281.         {
  282.             second_index = i;
  283.             break;
  284.         }
  285.         i++;
  286.         t = t->next;
  287.     }
  288.  
  289.     if (first_index == -1 || second_index == -1)
  290.         return list;
  291.  
  292.     if (first_index == second_index)
  293.         return list;
  294.  
  295.     return swap(list, first_index, second_index);
  296. }
  297.  
  298. int main()
  299. {
  300.     //create list
  301.     Node *list = NULL;
  302.  
  303.     int a;
  304.     int b;
  305.  
  306.     while (true)
  307.     {
  308.         cout << HELP_MSG;
  309.         cin >> a;
  310.  
  311.         switch (a)
  312.         {
  313.         case 0:
  314.             exit(0);
  315.             break;
  316.  
  317.         case 1:
  318.             cin >> b;
  319.             list = push_back(list, b);
  320.             break;
  321.  
  322.         case 2:
  323.             cin >> b;
  324.             list = push_front(list, b);
  325.             break;
  326.  
  327.         case 3:
  328.             list = pull_back(list);
  329.             break;
  330.  
  331.         case 4:
  332.             list = pull_front(list);
  333.             break;
  334.  
  335.         case 5:
  336.             print(list);
  337.             break;
  338.  
  339.         case 6:
  340.             list = swap35(list);
  341.             break;
  342.         }
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement