AlexandrTalchuk

Untitled

Apr 10th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <iostream>
  2. #include "windows.h"
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct list
  7. {
  8.     int info;
  9.     list* next, *prev;
  10. };
  11.  
  12.  
  13. void Create(list*& head, list*& end);
  14. void Add(list*& head, list*& end);
  15. void Add_end(list*& end, int inf);
  16. void Add_head(list*& head, int inf);
  17. void Del_el(list*& head, int inf);
  18. void Delete(list*& head);
  19. void View(list*& head, list*& end);
  20. void View_head(list*& head);
  21. void View_end(list*& end);
  22.  
  23.  
  24. int main()
  25. {
  26.     setlocale(LC_ALL, "rus");
  27.     list* head = nullptr;
  28.     list* end = nullptr;
  29.     int choice,info=0;
  30.     while (true)
  31.     {
  32.         cout << " 1. Создание\n 2. Добавление\n 3. Удаление\n 4. Просмотр \n 5. Сортировка\n 6. Выход" << endl;
  33.         cin >> choice;
  34.         switch (choice)
  35.         {
  36.         case 1:
  37.             Create(head, end);
  38.             break;
  39.         case 2:
  40.             Add(head, end);
  41.             break;
  42.         case 3:
  43.             Del_el(head,info);
  44.             break;
  45.         case 4:
  46.             View(head,end);
  47.             break;
  48.         case 5:
  49.             //Solution(head, end);
  50.             break;
  51.         case 6:
  52.             exit(0);
  53.             break;
  54.         default:
  55.             cout << "Повторите еще раз" << endl;
  56.             break;
  57.         }
  58.     }
  59. }
  60.  
  61. void Create(list*& head, list*& end)
  62. {
  63.     if (head == NULL)
  64.     {
  65.         int kolvo, i, info;
  66.         cout << "Введите количество элементов" << endl;
  67.         cin >> kolvo;
  68.         if (cin.fail() || kolvo < 0)
  69.         {
  70.             cout << "Условия ввода не соблюдены" << endl;
  71.             return;
  72.         }
  73.         cout << "1-ый " ;
  74.         cin >> info;
  75.         list* t = new list;
  76.         t->info = info;
  77.         t->next = head;
  78.         t->prev = end;
  79.         head = end = t;
  80.         for (i = 1; i < kolvo; i++)
  81.         {
  82.             cout << i + 1 << "-ый ";
  83.             cin >> info;
  84.             Add_end(head, info);
  85.         }
  86.     }
  87.     else
  88.         cout << "Список уже создан" << endl;
  89. }
  90.  
  91. void Add(list*& head, list*& end)
  92. {
  93.     cout << " 1.Добавить элемент в начало списка\n 2.Добавить элемент в конец списка" << endl;
  94.     int u,element;
  95.     cin >> u;
  96.     switch (u)
  97.     {
  98.     case 1:
  99.         cout << "Введите добавляемый элемент" << endl;
  100.         cin >> element;
  101.         Add_head(head, element);
  102.         break;
  103.     case 2:
  104.         cout << "Введите добавляемый элемент" << endl;
  105.         cin >> element;
  106.         Add_end(end, element);
  107.         break;
  108.     default:
  109.         cout << "Данный символ ввести невозможно" << endl;
  110.         break;
  111.     }
  112. }
  113.  
  114. void Add_end(list*& end, int inf)
  115. {
  116.     list* t = new list;
  117.     t->info = inf;
  118.     t->next = NULL;
  119.     t->prev = end;
  120.     end->next = t;
  121.     end = t;
  122. }
  123.    
  124. void Add_head(list*&head,int inf)
  125. {
  126.     list* t = new list;
  127.     t->prev = NULL;
  128.     t->info = inf;
  129.     t->next = head;
  130.     head->prev = t;
  131.     head = t;
  132. }
  133.  
  134. void Del_el(list*& head, int inf)
  135. {
  136.     if (head != NULL)
  137.     {
  138.         list* t = nullptr;
  139.         t = head;
  140.         cout << inf;
  141.         head = head->next;
  142.         delete t;
  143.  
  144.     }
  145. }
  146.  
  147. void Delete(list*& head)
  148. {
  149.     list* t = nullptr;
  150.     while (head!= NULL)
  151.     {
  152.         t =head;
  153.         head = head->next;
  154.         delete t;
  155.  
  156.     }
  157. }
  158.  
  159. void View(list*& head, list*& end)
  160. {
  161.  
  162.     if (head == NULL)
  163.     {
  164.         cout << "Список пуст!" << endl;
  165.         return;
  166.     }
  167.     cout << "\n 1. Просмотр списка сначала\n 2. Просмотр списка с конца" << endl;
  168.     int r;
  169.     cin >> r;
  170.     switch (r)
  171.     {
  172.      case 1:
  173.          View_head(head);
  174.         break;
  175.      case 2:
  176.          View_end(end);
  177.         break;
  178.      default:
  179.         cout << "Значение введено неправильно" << endl;
  180.         return;
  181.     }
  182. }
  183.  
  184. void View_head(list*& head)
  185. {
  186.     list* t =head;
  187.     while (t != NULL)
  188.     {
  189.         cout << "Элемент равен " << t->info << endl;
  190.         t = t->next;
  191.     }
  192. }
  193.  
  194. void View_end(list*& end)
  195. {
  196.     list* t =end;
  197.     while (t != NULL)
  198.     {
  199.         cout << "Элемент равен " << t->info << endl;
  200.         t = t->prev;
  201.     }
  202. }
  203.  
  204. //void Solution(list*& head, list*& end)
  205. //{
  206. //
  207. //}
Add Comment
Please, Sign In to add comment