Maxim_Leo

Untitled

May 13th, 2022
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. #define n_ 10
  2. #define no_init -842150451
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct Node {
  8. int value;
  9. Node* next;
  10. };
  11.  
  12.  
  13. class List {
  14.  
  15. public:
  16. int size;
  17. Node* head;
  18. List() {
  19. head = nullptr;
  20. }
  21. bool isEmpty() {
  22. if (head == nullptr) return true;
  23. return false;
  24. }
  25.  
  26. void push_front(int data)
  27. {
  28. head = new Node{ data, head };
  29. }
  30. void pop_front()
  31. {
  32. Node* temp = head;
  33. head = head->next;
  34. delete temp;
  35. }
  36.  
  37. void insert(int data, int curr) {
  38. if (size == 0)
  39. {
  40. push_front(data);
  41. }
  42.  
  43. else {
  44. Node* prev = this->head;
  45. for (int i = 0; i < curr - 1; i++) {
  46. prev = prev->next;
  47. }
  48. Node* newNode = new Node{ data, prev->next };
  49. prev->next = newNode;
  50. }
  51. size++;
  52. }
  53. void del(int curr) {
  54. if (size == 0)
  55. {
  56. pop_front();
  57. }
  58. else {
  59. Node* prev = this->head;
  60. for (int i = 0; i < curr - 1; i++) {
  61. prev = prev->next;
  62. }
  63. Node* toDel = prev->next;
  64.  
  65. prev->next = toDel->next;
  66.  
  67. delete toDel;
  68. }
  69. size--;
  70. }
  71. void top() {
  72. cout << head->value;
  73. }
  74. void show() {
  75. Node* h = head;
  76. while (h) {
  77. cout << h->value << " ";
  78. h = h->next;
  79. }
  80. }
  81. void clear() {
  82. Node* h = head;
  83. while (h != NULL) {
  84. h = NULL;
  85. h = h->next;
  86. }
  87. }
  88. };
  89.  
  90.  
  91. struct list {
  92. int a[n_];
  93. int index;
  94. int head;
  95. int tail;
  96. };
  97. void CreateList(list* l) {
  98. l->head = INT_MIN;
  99. l->tail = INT_MIN;
  100. l->index = NULL;
  101. }
  102. bool isEmpty_(list* l) {
  103. if (l->tail == INT_MIN) return true;
  104. else return false;
  105. }
  106.  
  107. void insert_(int ind,int elem, list* l) {
  108. if (ind<0 || ind>l->index) cout << "Такого элемента нет";
  109. else {
  110. if (l->index == n_) cout << "Нет места " << endl;
  111. else if (l->tail == INT_MIN) {
  112. l->head = l->tail = elem;
  113. l->a[0] = elem;
  114. l->index++;
  115. }
  116. else {
  117. l->a[ind + 1] = elem;
  118. if (l->a[ind + 2] == INT_MIN) l->tail = elem;
  119. l->index++;
  120. }
  121. }
  122. }
  123. void del_(int ind,list* l) {
  124. if (ind<0 || ind>l->index) cout << "Такого элемента нет";
  125. else {
  126. if (l->index == 0) cout << "Нельзя удалить элемент из пустого массива";
  127. else if (l->index == 1) {
  128. l->a[0] = INT_MIN;
  129. l->head = l->tail = INT_MIN;
  130. l->index--;
  131. }
  132. else if (l->a[ind + 2] == INT_MIN) {
  133. l->a[ind + 1] = INT_MIN;
  134. l->tail = l->a[ind];
  135. l->index--;
  136. }
  137. else {
  138. l->a[ind + 1] = INT_MIN;
  139. l->index--;
  140. }
  141. }
  142. }
  143. void show_(list* l) {
  144. if (l->index == 0) cout << "Массив пуст";
  145. for (int i = 0; i < n_;i++) {
  146. if (l->a[i] != INT_MIN && l->a[i] != no_init) cout << l->a[i] << " ";
  147. }
  148. }
  149. void clear(list*l) {
  150. for (int i = 0; i < l->index; i++) {
  151. l->a[i] = INT_MIN;
  152. }
  153. }
  154. /*
  155. struct list {
  156. int a[n_];
  157. int count=0;
  158. list* next;
  159. list* head;
  160. };
  161.  
  162. void CreateList(list* l) {
  163. l->head = nullptr;
  164. l->next = nullptr;
  165. }
  166.  
  167. bool is_Empty(list* l) {
  168. if (l->head == nullptr) return true;
  169. else return false;
  170. }
  171.  
  172. void Insert(list* l, int data,int curr) {
  173. if (l->count == n_) cout << "Массив заполнен";
  174. else {
  175. if (l->head == nullptr) {
  176. l->head->next = nullptr;
  177. l->a[0] = data;
  178. l->head->a[0] = data;
  179. l->count++;
  180. }
  181. else {
  182. list* prev = l;
  183. for (int i = 0; i < curr - 1; i++) {
  184. prev = prev->next;
  185. }
  186. l->a[curr + 1] = data;
  187. }
  188. }
  189. }
  190.  
  191. void Delete(list* l,int curr) {
  192.  
  193. if (curr == 0)
  194. {
  195. l->a[0] = NULL;
  196. }
  197. else {
  198. list* prev = l->head;
  199. for (int i = 0; i < curr - 1; i++) {
  200. prev = prev->next;
  201. }
  202. list* toDel = prev->next;
  203. prev->next = toDel->next;
  204. delete toDel;
  205. l->count--;
  206. }
  207. }
  208.  
  209. void Top(list* l) {
  210. cout << l->a[0];
  211. }
  212. void Show(list* l) {
  213. int i = 0;
  214. list* h = l->head;
  215. while (l->a[i] == NULL) {
  216. cout << h->a[i] << " ";
  217. h = h->next;
  218. i++;
  219. }
  220. }
  221. void Clear() {
  222.  
  223. }
  224. */
  225. int main()
  226. {
  227. setlocale(LC_ALL, "Russian");
  228. /* List list;
  229. list.insert(5, 0);
  230. list.insert(2, 1);
  231. list.insert(-5, 2);
  232. list.show();
  233. list.clear();
  234.  
  235. list* list1=new list;
  236. Insert(list1,5, 0);
  237. Insert(list1,2, 1);
  238. Insert(list1 ,-5, 2);
  239. Show(list1);
  240. //Top(list1);
  241. cout << endl;
  242. */
  243.  
  244. int select;
  245. cout << "Выберите способ: " << endl;
  246. cout << "1)Цепной список" << endl;
  247. cout << "2)Сплошной список" << endl;
  248. cin >> select;
  249. if (select == 1) {
  250. int n, n1, x, ind;
  251. string string1;
  252. List list;
  253. cout << "Пустой список создан" << endl;
  254. cout << "Проверка списка на пустоту: " << list.isEmpty() << endl;
  255. cout << "Введите количество элементов для заполнения списка: ";
  256. cin >> n;
  257. for (int i = 0; i < n; i++) {
  258. cout << "Введите элемент: ";
  259. cin >> x;
  260. cout << "Введите индекс текущего элемента: ";
  261. cin >> ind;
  262. list.insert(x, ind);
  263. }
  264. cout << "Проверка список на пустоту: " << list.isEmpty() << endl;
  265. if (list.isEmpty() == 0) list.top();
  266. else 0;
  267. cout << endl;
  268. cout << "Вывод элементов списка" << endl;
  269. list.show();
  270. cout << endl;
  271. cout << "Хотите ли вы удалить элементы и сколько?" << endl;
  272. cin >> string1;
  273. if (string1 == "yes" or string1 == "Yes") {
  274. cin >> n1;
  275. for (int i = 0; i < n1; i++) {
  276.  
  277. cout << "Введите индекс текущего элемента: ";
  278. cin >> ind;
  279. list.del(ind);
  280. cout << endl;
  281. }
  282. list.show();
  283. }
  284. else cout << "конец";
  285.  
  286. }
  287. else if (select == 2) {
  288. list* list1 = new list;
  289. int n, n1, x = 0, ind1;
  290. string string1;
  291. CreateList(list1);
  292. cout << "Пустой список создан" << endl;
  293. cout << "Проверка список на пустоту: " << isEmpty_(list1) << endl;
  294. cout << "Введите количество элементов для заполнения списка: ";
  295. cin >> n;
  296. for (int i = 0; i < n; i++) {
  297. cout << "Введите элемент: ";
  298. cin >> x;
  299. cout << "Введите индекс текущего элемента: ";
  300. cin >> ind1;
  301. insert_(ind1, x,list1 );
  302. }
  303. cout << "Проверка списка на пустоту: " << isEmpty_(list1) << endl;
  304. cout << "Вывод элементов списка" << endl;
  305. show_(list1);
  306. cout << endl;
  307. cout << "Хотите ли вы удалить элементы и сколько?" << endl;
  308. cin >> string1;
  309. if (string1 == "yes" or string1 == "Yes") {
  310. cin >> n1;
  311. for (int i = 0; i < n1; i++) {
  312. cout << "Введите индекс текущего элемента: ";
  313. cin >> ind1;
  314. del_(ind1,list1);
  315. cout << endl;
  316. }
  317. show_(list1);
  318. }
  319. else cout << "конец";
  320. }
  321. else cout << "Неправильный номер";
  322.  
  323. return 0;
  324. }
  325.  
Advertisement
Add Comment
Please, Sign In to add comment