Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.23 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. const int size = 5;
  4.  
  5. struct ListElement
  6. {
  7.     int value;
  8.     ListElement *next;
  9. };
  10.  
  11. struct List
  12. {
  13.     ListElement *head = nullptr;
  14. };
  15.  
  16. //Проверка, пустой ли список
  17. bool isListEmpty(List * list)
  18. {
  19.     return list->head == nullptr;
  20. }
  21.  
  22. //Проверка, существует ли элемент в списке
  23. bool isValueExistInList(List * list, int value)
  24. {
  25.     if (isListEmpty(list))
  26.     {
  27.         return false;
  28.     }
  29.  
  30.     ListElement * listElement = list->head;
  31.  
  32.     if (list->head->value == value)
  33.     {
  34.         return true;
  35.     }
  36.     else
  37.     {
  38.         while (listElement->next != nullptr && listElement->next->value != value)
  39.         {
  40.             if (listElement->next->value != value)
  41.             {
  42.                 listElement = listElement->next;
  43.             }
  44.         }
  45.  
  46.         return !(listElement->next == nullptr);
  47.     }
  48. }
  49.  
  50. //Добавить элемент в список
  51. void addElement(List * list, int value)
  52. {
  53.     ListElement * listElement = new ListElement;
  54.     listElement->value = value;
  55.     listElement->next = list->head;
  56.     list->head = listElement;
  57.     while (listElement->next != nullptr && listElement->value > listElement->next->value)
  58.     {
  59.         int temp = listElement->value;
  60.         listElement->value = listElement->next->value;
  61.         listElement->next->value = temp;
  62.         listElement = listElement->next;
  63.     }
  64. }
  65.  
  66. //Удалить элемент из списка
  67. void deleteElement(List * list, int value)
  68. {
  69.     if (isListEmpty(list))
  70.     {
  71.         printf("Список пуст.\n");
  72.         return;
  73.     }
  74.  
  75.     ListElement * listElement = list->head;
  76.  
  77.     if (list->head->value == value)
  78.     {
  79.         ListElement * tempListElement = new ListElement;
  80.         tempListElement = list->head;
  81.         list->head = list->head->next;
  82.         delete tempListElement;
  83.     }
  84.     else
  85.     {
  86.         while (listElement->next != nullptr && listElement->next->value != value)
  87.         {
  88.             if (listElement->next->value != value)
  89.             {
  90.                 listElement = listElement->next;
  91.             }
  92.         }
  93.  
  94.         if (listElement->next == nullptr)
  95.         {
  96.             printf("Такого значения нет в списке.\n");
  97.             return;
  98.         }
  99.  
  100.         ListElement * tempListElement = new ListElement;
  101.         tempListElement = listElement->next;
  102.         listElement->next = tempListElement->next;
  103.         delete tempListElement;
  104.     }
  105. }
  106.  
  107. //Поместить все элементы в массив
  108. void putElementsInArray(List * list, int arrayOfNumbers[])
  109. {
  110.     int index = 0;
  111.     ListElement * listElement = list->head;
  112.     while (listElement != nullptr)
  113.     {
  114.         arrayOfNumbers[index] = listElement->value;
  115.         listElement = listElement->next;
  116.         ++index;
  117.     }
  118. }
  119.  
  120. //Вывести список
  121. void showList(List * list)
  122. {
  123.     if (isListEmpty(list))
  124.     {
  125.         printf("Список пуст.\n");
  126.         return;
  127.     }
  128.     printf("Список: ");
  129.     ListElement * listElement = new ListElement;
  130.     listElement = list->head;
  131.     while (listElement != nullptr)
  132.     {
  133.         printf("%d ", listElement->value);
  134.         listElement = listElement->next;
  135.     }
  136.     printf("\n");
  137. }
  138.  
  139. void deleteElement(List * list)
  140. {
  141.     ListElement *temp = list->head;
  142.     list->head = list->head->next;
  143.     delete temp;
  144. }
  145.  
  146. void deleteList(List * list)
  147. {
  148.     while (list->head != nullptr)
  149.     {
  150.         deleteElement(list);
  151.     }
  152.  
  153.     delete list;
  154. }
  155.  
  156. int main()
  157. {
  158.     setlocale(LC_ALL, "Russian");
  159.  
  160.     List * testList = new List;
  161.  
  162.     if (!isListEmpty(testList))
  163.     {
  164.         printf("Ошибка в проверке на пустоту списка.");
  165.         return 0;
  166.     }
  167.  
  168.     addElement(testList, 1);
  169.  
  170.     if (!isValueExistInList(testList, 1))
  171.     {
  172.         printf("Ошибка в добавлении элемента.");
  173.         return 0;
  174.     }
  175.  
  176.     if (isValueExistInList(testList, 0))
  177.     {
  178.         printf("Ошибка в проверке на существование.");
  179.         return 0;
  180.     }
  181.  
  182.     deleteElement(testList, 1);
  183.  
  184.     if (isValueExistInList(testList, 1))
  185.     {
  186.         printf("Ошибка в удалении элемента.");
  187.         return 0;
  188.     }
  189.  
  190.     addElement(testList, 2);
  191.     addElement(testList, 0);
  192.     addElement(testList, 4);
  193.     addElement(testList, 1);
  194.     addElement(testList, 3);
  195.     int testArray[size];
  196.     int testCorrectArray[size] = { 0, 1, 2, 3, 4 };
  197.     putElementsInArray(testList, testArray);
  198.     for (int i = 0; i != size; ++i)
  199.     {
  200.         if (testArray[i] != testCorrectArray[i])
  201.         {
  202.             printf("Ошибка в сортированности листа.");
  203.             return 0;
  204.         }
  205.     }
  206.  
  207.     deleteList(testList);
  208.  
  209.     List * list = new List;
  210.     int operation = -1;
  211.  
  212.     while (operation)
  213.     {
  214.         printf("Введите номер операции:\n");
  215.         printf("0 - выйти\n");
  216.         printf("1 - добавить значение в список\n");
  217.         printf("2 - удалить значение из списка\n");
  218.         printf("3 - распечатать список\n");
  219.         scanf_s("%d", &operation);
  220.  
  221.         switch (operation)
  222.         {
  223.         case 0:
  224.             break;
  225.         case 1:
  226.         {
  227.             int value = 0;
  228.             printf("Введите значение добавляемого элемента: ");
  229.             scanf_s("%d", &value);
  230.             addElement(list, value);
  231.             break;
  232.         }
  233.         case 2:
  234.         {
  235.             int value = 0;
  236.             printf("Введите значение, которое нужно удалить из списка: ");
  237.             scanf_s("%d", &value);
  238.             deleteElement(list, value);
  239.             break;
  240.         }
  241.         case 3:
  242.         {
  243.             showList(list);
  244.             break;
  245.         }
  246.         default:
  247.         {
  248.             printf("Некорректная операция.\n");
  249.             break;
  250.         }
  251.         }
  252.     }
  253.  
  254.     deleteList(list);
  255.  
  256.     return 0;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement