AlexandrTalchuk

Untitled

Apr 21st, 2020
34
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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);
  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. void Solution(list*& head,list*& end, list*& Even_head, list*& Even_end, list*& Odd_head, list*& Odd_end);
  23.  
  24.  
  25. int main()
  26. {
  27. setlocale(LC_ALL, "rus");
  28. list* head = nullptr;
  29. list* end = nullptr;
  30. list* Even_head = nullptr;
  31. list* Even_end = nullptr;
  32. list* Odd_head = nullptr;
  33. list* Odd_end = nullptr;
  34. int choice, info = 0;
  35. while (true)
  36. {
  37. cout << " 1. Создание\n 2. Добавление\n 3. Удаление\n 4. Просмотр \n 5. Сортировка\n 6. Выход" << endl;
  38. cin >> choice;
  39. switch (choice)
  40. {
  41. case 1:
  42. Create(head, end);
  43. Sleep(1000);
  44. system("cls");
  45. break;
  46. case 2:
  47. Add(head, end);
  48. Sleep(1000);
  49. system("cls");
  50. break;
  51. case 3:
  52. Del_el(head);
  53. Sleep(1000);
  54. system("cls");
  55. break;
  56. case 4:
  57. View(head, end);
  58. Sleep(10000);
  59. system("cls");
  60. break;
  61. case 5:
  62. Solution(head,end, Even_head, Even_end, Odd_head, Odd_end);
  63. View(Even_head, Even_end);
  64. View(Odd_head, Odd_end);
  65. Sleep(3000);
  66. system("cls");
  67. break;
  68. case 6:
  69. Delete(head);
  70. Delete(Even_head);
  71. Delete(Odd_head);
  72. return 0;
  73. break;
  74. default:
  75. cout << "Повторите еще раз" << endl;
  76. break;
  77. }
  78. }
  79. }
  80.  
  81. void Create(list*& head, list*& end)
  82. {
  83. if (head == NULL)
  84. {
  85. int kolvo, i, info;
  86. cout << "Введите количество элементов" << endl;
  87. cin >> kolvo;
  88. if (cin.fail() || kolvo < 0)
  89. {
  90. cout << "Условия ввода не соблюдены" << endl;
  91. return;
  92. }
  93. cout << "1-ый ";
  94. cin >> info;
  95. list* t = new list;
  96. t->info = info;
  97. t->next = end=NULL;
  98. t->prev = head=NULL;
  99. head = end = t;
  100. for (i = 1; i < kolvo; i++)
  101. {
  102. cout << i + 1 << "-ый ";
  103. cin >> info;
  104. Add_end(end, info);
  105. }
  106. }
  107. else
  108. cout << "Список уже создан" << endl;
  109. }
  110.  
  111. void Add(list*& head, list*& end)
  112. {
  113. cout << " 1.Добавить элемент в начало списка\n 2.Добавить элемент в конец списка" << endl;
  114. int u, element;
  115. cin >> u;
  116. switch (u)
  117. {
  118. case 1:
  119. cout << "Введите добавляемый элемент" << endl;
  120. cin >> element;
  121. Add_head(head, element);
  122. break;
  123. case 2:
  124. cout << "Введите добавляемый элемент" << endl;
  125. cin >> element;
  126. Add_end(end, element);
  127. break;
  128. default:
  129. cout << "Данный символ ввести невозможно" << endl;
  130. break;
  131. }
  132. }
  133.  
  134. void Add_end(list*& end, int inf)
  135. {
  136. list* t = new list;
  137. t->info = inf;
  138. t->next = NULL;
  139. t->prev = end;
  140. end->next = t;
  141. end = t;
  142. }
  143.  
  144. void Add_head(list*& head, int inf)
  145. {
  146. list* t = new list;
  147. t->prev = NULL;
  148. t->info = inf;
  149. t->next = head;
  150. head->prev = t;
  151. head = t;
  152. }
  153.  
  154. void Del_el(list*& head)
  155. {
  156. if (head != NULL)
  157. {
  158. list* t = nullptr;
  159. t = head;
  160. head = head->next;
  161. delete t;
  162.  
  163. }
  164. }
  165.  
  166. void Delete(list*& head)
  167. {
  168. list* t = nullptr;
  169. while (head != NULL)
  170. {
  171. t = head;
  172. head = head->next;
  173. delete t;
  174.  
  175. }
  176. }
  177.  
  178. void View(list*& head, list*& end)
  179. {
  180.  
  181. if (head == NULL)
  182. {
  183. cout << "Список пуст!" << endl;
  184. return;
  185. }
  186. cout << "\n 1. Просмотр списка сначала\n 2. Просмотр списка с конца" << endl;
  187. int r;
  188. cin >> r;
  189. switch (r)
  190. {
  191. case 1:
  192. View_head(head);
  193. break;
  194. case 2:
  195. View_end(end);
  196. break;
  197. default:
  198. cout << "Значение введено неправильно" << endl;
  199. return;
  200. }
  201. }
  202.  
  203. void View_head(list*& head)
  204. {
  205. list* t = head;
  206. while (t != NULL)
  207. {
  208. cout << "Элемент равен " << t->info << endl;
  209. t = t->next;
  210. }
  211. }
  212.  
  213. void View_end(list*& end)
  214. {
  215. list* t = end;
  216. while (t != NULL)
  217. {
  218. cout << "Элемент равен " << t->info << endl;
  219. t = t->prev;
  220. }
  221. }
  222. void Solution(list*& head, list*& end, list*& Even_head, list*& Even_end, list*& Odd_head, list*& Odd_end)
  223. {
  224. list* current = head;
  225. int k = 1;
  226. while (current != NULL)
  227. {
  228. if (current->info %2== 0)
  229. {
  230. if (Even_head == NULL)
  231. {
  232. list* t = current;
  233. current = current->next;
  234. t->prev = NULL;
  235. t->next = NULL;
  236. Even_head = Even_end = t;
  237. }
  238. else
  239. {
  240. list* tmp = current;
  241. current = current->next;
  242. tmp->next = Even_head;
  243. tmp->prev = NULL;
  244. Even_head->prev = tmp;
  245. Even_head = tmp;
  246. }
  247. }
  248. else
  249. {
  250. if (Odd_head == NULL)
  251. {
  252. list* t = current;
  253. current = current->next;
  254. t->prev = NULL;
  255. t->next = NULL;
  256. Odd_head = Odd_end = t;
  257. }
  258. else
  259. {
  260. list* tmp = current;
  261. current = current->next;
  262. tmp->next = Odd_head;
  263. tmp->prev = NULL;
  264. Odd_head->prev = tmp;
  265. Odd_head = tmp;
  266. }
  267. }
  268.  
  269. }
  270. }
RAW Paste Data