Advertisement
BagaevDanil

Untitled

May 20th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. struct myList
  7. {
  8.     int value;
  9.     myList *next = nullptr;
  10.     myList *prev = nullptr;
  11. };
  12.  
  13. void PrintList(myList *key, myList *keyEnd)
  14. {
  15.     int ind = 1;
  16.     if (key == nullptr)
  17.     {
  18.         cout << "[Пустой лист]\n\n";
  19.         return;
  20.     }
  21.  
  22.     while (key->next != nullptr)
  23.     {
  24.         cout << key->value << " [" << ind << "]   ";
  25.         ind++;
  26.         key = key->next;
  27.     }
  28.     cout << key->value << " [" << ind << "]\n\n";
  29.  
  30. //    while (keyEnd->prev != nullptr)
  31. //    {
  32. //        cout << keyEnd->value << " [" << ind << "]   ";
  33. //        ind--;
  34. //        keyEnd = keyEnd->prev;
  35. //    }
  36. //    cout << keyEnd->value << " [" << ind << "]\n\n";
  37.     //ind++;
  38.     //key = key->next;
  39. }
  40.  
  41. int SelectBut(int *indMenu)
  42. {
  43.     int but = getch();
  44.     if (but == 13)
  45.         return *indMenu;
  46.     if (but == 27)
  47.         return 0;
  48.     if (but - 48 >= 0 && but - 48 <= 5)
  49.         return but - 48;
  50.     if (but == 224)
  51.     {
  52.         but = getch();
  53.         if (but == 72)
  54.             (*indMenu)--;
  55.         else if (but == 80)
  56.             (*indMenu)++;
  57.         *indMenu = ((*indMenu) + 6) % 6;
  58.         return -2;
  59.     }
  60.     return -3;
  61.     //cout << but; //...................................................................
  62. }
  63.  
  64. void PrintMenu(int select, myList *key,  myList *keyEnd)
  65. {
  66.     system("cls");
  67.  
  68.     PrintList(key, keyEnd);
  69.     char arrow[] = "<--";
  70.     char arrMenu[6][256] =
  71.     {
  72.     "0) Выход........................",
  73.     "1) Создание.....................",
  74.     "2) Удаление.....................",
  75.     "3) Создание несколько...........",
  76.     "4) Удаление несколько...........",
  77.     "5) Сортировка..................."
  78.     };
  79.     for (int i = 0; i < 6; i++)
  80.     {
  81.         cout << arrMenu[i];
  82.         if (select == i)
  83.             cout << arrow;
  84.         cout << endl;
  85.     }
  86.  
  87. }
  88.  
  89. int SizeList(myList *key)
  90. {
  91.     if (key == nullptr)
  92.         return 0;
  93.  
  94.     int ind = 1;
  95.     while (key->next != 0)
  96.     {
  97.         key = key->next;
  98.         ind++;
  99.     }
  100.     return (ind);
  101. }
  102.  
  103. void AddCell(myList **key, myList *cell,myList **keyEnd)
  104. {
  105.     if (*key == nullptr)
  106.     {
  107.         cout << "Это ваш первый эллемент\nВведите значение: ";
  108.         *key = new myList;
  109.         cin >> (*key)->value;
  110.         (*keyEnd) = (*key);
  111.         return;
  112.     }
  113.  
  114.     cout << "После какого эллемента добавть новый: ";
  115.     int ind;
  116.     cin >> ind;
  117.  
  118.     int sizeList = SizeList(cell);
  119.     if ((ind > sizeList) || (ind < 0))
  120.     {
  121.         cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
  122.         int but = getch();
  123.         return;
  124.     }
  125.  
  126.     cout << "Введите значение: ";
  127.     int value;
  128.     cin >> value;
  129.  
  130.     myList *demo = new myList;
  131.     demo->value = value;
  132.  
  133.     if (ind == 0)
  134.     {
  135.         demo->next = (*key);
  136.         (*key)->prev = demo;
  137.         (*key) = demo;
  138.         return;
  139.     }
  140.  
  141.     for (int i = 1; i < ind; i++)
  142.         cell = cell->next;
  143.  
  144.     demo->next = cell->next;
  145.     cell->next = demo;
  146.     if (demo->next != nullptr)
  147.         (demo->next)->prev = demo;
  148.     demo->prev = cell;
  149.  
  150.     if (ind == sizeList)
  151.     {
  152.         (*keyEnd) = demo;
  153.     }
  154. }
  155.  
  156. void AddCellSeveral(myList **key, myList *cell)
  157. {
  158.     cout << "Сколько эллементов создать: ";
  159.     int num, name = 1, ind = 1;
  160.     cin >> num;
  161.  
  162.     if (num <= 0)
  163.     {
  164.         cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
  165.         int but = getch();
  166.         return;
  167.     }
  168.  
  169.     if (*key == nullptr)
  170.     {
  171.         cout << "Это ваши первые эллементы\nВведите значения:\n" << name << " эллемент: ";
  172.         *key = new myList;
  173.         cin >> (*key)->value;
  174.         name++;
  175.         num--;
  176.         cell = *key;
  177.         //cout << cell;
  178.     }
  179.     else
  180.     {
  181.         cout << "После какого эллемента создать: ";
  182.         cin >> ind;
  183.  
  184.         if ((ind > SizeList(cell)) || (ind < 0))
  185.         {
  186.             cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
  187.             int but = getch();
  188.             return;
  189.         }
  190.     }
  191.  
  192.     if(ind == 0)
  193.     {
  194.         cout << "Введите значения:\n" << name << " эллемент: ";
  195.         myList *demo = new myList;
  196.         cin >> demo->value;
  197.         demo->next = (*key);
  198.  
  199.         (*key) = demo;
  200.         num--;
  201.         name++;
  202.         ind = 1;
  203.         cell = (*key);
  204.     }
  205.  
  206.     for (int i = 1; i < ind; i++)
  207.         cell = cell->next;
  208.  
  209.     for (int i = 0; i < num; i++)
  210.     {
  211.         cout << "Введите значения:\n" << name << " эллемент: ";
  212.         myList *demo = new myList;
  213.         cin >> demo->value;
  214.         demo->next = cell->next;
  215.         cell->next = demo;
  216.         cell = cell->next;
  217.         name++;
  218.     }
  219.  
  220. }
  221.  
  222. void DeleteCell(myList **key, myList *cell, myList **keyEnd)
  223. {
  224.     if (*key == nullptr)
  225.     {
  226.         cout << "Список пустой!\nНажмите любую кнопку, чтобы вернуться в меню...";
  227.         int but = getch();
  228.         return;
  229.     }
  230.  
  231.     cout << "Какой эллемент удалить: ";
  232.     int ind;
  233.     cin >> ind;
  234.     int sizeList = SizeList(cell);
  235.     if ((ind > sizeList) || (ind <= 0))
  236.     {
  237.         cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
  238.         int but = getch();
  239.         return;
  240.     }
  241.  
  242.     if (ind == 1)
  243.     {
  244.         myList *demo = (*key);
  245.         (*key) = (*key)->next;
  246.         if ((*key) != nullptr)
  247.             (*key)->prev = nullptr;
  248.         delete[] demo;
  249.         return;
  250.     }
  251.  
  252.     for (int i = 1; i < ind-1; i++)
  253.         cell = cell->next;
  254.     myList *demo = cell->next;
  255.     cell->next = cell->next->next;
  256.  
  257.     if (cell->next != nullptr)
  258.         cell->next->prev = cell;
  259.  
  260.     if (ind == sizeList)
  261.     {
  262.         (*keyEnd) = cell;
  263.     }
  264.  
  265.     delete[] demo;
  266. }
  267.  
  268. void DeleteCellSeveral(myList **key, myList *cell)
  269. {
  270.     if (*key == nullptr)
  271.     {
  272.         cout << "Список пустой!\nНажмите любую кнопку, чтобы вернуться в меню...";
  273.         int but = getch();
  274.         return;
  275.     }
  276.  
  277.     cout << "С какого эллемента удалить: ";
  278.     int ind;
  279.     cin >> ind;
  280.  
  281.     if ((ind > SizeList(cell)) || (ind <= 0))
  282.     {
  283.         cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
  284.         int but = getch();
  285.         return;
  286.     }
  287.  
  288.     cout << "Сколько эллементов удалить: ";
  289.     int num;
  290.     cin >> num;
  291.  
  292.     if (ind + num - 1 > SizeList(cell))
  293.     {
  294.         cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
  295.         int but = getch();
  296.         return;
  297.     }
  298.  
  299.     if (ind == 1)
  300.     {
  301.         for (int i = 0; i < num; i++)
  302.         {
  303.             myList *demo = (*key);
  304.             (*key) = (*key)->next;
  305.             delete[] demo;
  306.         }
  307.         return;
  308.     }
  309.  
  310.     for (int i = 1; i < ind-1; i++)
  311.         cell = cell->next;
  312.  
  313.     for (int i = 0; i < num; i++)
  314.     {
  315.         myList *demo = cell->next;
  316.         cell->next = demo->next;
  317.         delete[] demo;
  318.     }
  319. }
  320.  
  321. void SortList(myList **key, myList *cell)
  322. {
  323.     if (*key == nullptr)
  324.     {
  325.         cout << "Список пустой!\nНажмите любую кнопку, чтобы вернуться в меню...";
  326.         int but = getch();
  327.         return;
  328.     }
  329.  
  330.     int sizeL = SizeList(*key);
  331.  
  332.     for (int i = 0; i < sizeL - 1; i++)
  333.     {
  334.         for (int j = 0; j < sizeL - 1; j++)
  335.         {
  336.             myList *cellN = (cell->next);
  337.             if (cell->value > cellN->value)
  338.             {
  339.                 int value = cell->value;
  340.                 cell->value = cellN->value;
  341.                 cellN->value = value;
  342.  
  343.             }
  344.             cell = cell->next;
  345.         }
  346.         cell = (*key);
  347.     }
  348.  
  349. }
  350.  
  351. void SortList1(myList **key, myList *cell)
  352. {
  353.     if (*key == nullptr)
  354.     {
  355.         cout << "Список пустой!\nНажмите любую кнопку, чтобы вернуться в меню...";
  356.         int but = getch();
  357.         return;
  358.     }
  359.  
  360.     int sizeL = SizeList(*key);
  361.  
  362.     myList *pred = nullptr;
  363.     for (int i = 0; i < sizeL - 1; i++)
  364.     {
  365.         for (int j = 0; j < sizeL - 1; j++)
  366.         {
  367.             //cout << i << " - " << j << endl;
  368.             myList *cellN = (cell->next);
  369.             if (cell->value > cellN->value)
  370.             {
  371.                 if (j == 0)
  372.                 {
  373.                     cell->next = cellN->next;
  374.                     cellN->next = cell;
  375.                     (*key) = cellN;
  376.                     pred = (*key);
  377.                 }
  378.                 else
  379.                 {
  380.                     cell->next = cellN->next;
  381.                     cellN->next = cell;
  382.                     pred->next = cellN;
  383.                     pred = cellN;
  384.                 }
  385.             }
  386.             else
  387.             {
  388.                 pred = cell;
  389.                 cell = cell->next;
  390.             }
  391.         }
  392.         pred = nullptr;
  393.         cell = (*key);
  394.     }
  395.  
  396. }
  397.  
  398. main()
  399. {
  400.     setlocale(LC_ALL, "Russian");
  401.     myList *key = nullptr;
  402.     myList *keyEnd = nullptr;
  403.  
  404.     int indMenu = 0;
  405.     int but = -2;
  406.  
  407.     while (but != 0)
  408.     {
  409.  
  410.         PrintMenu(indMenu, key, keyEnd);
  411.         but = SelectBut(&indMenu);
  412.  
  413.         if (but == 1)
  414.             AddCell(&key, key, &keyEnd);
  415.         else if (but == 2)
  416.             DeleteCell(&key, key, &keyEnd);
  417.         else if (but == 3)
  418.             AddCellSeveral(&key, key);
  419.         else if (but == 4)
  420.             DeleteCellSeveral(&key, key);
  421.         else if (but == 5)
  422.             SortList1(&key, key);
  423.     }
  424.  
  425. }
  426.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement