Advertisement
SkeptaProgrammer

Untitled

Dec 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. // task_2.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3. /*
  4. РЕАЛИЗОВАТЬ ДЕТСКУЮ СЧИТАЛКУ В КОЛЬЦЕВОМ СПИСКЕ. уДАЛЕНИЕ КАЖДОГО К-ТОГО ЭЛЕМЕНТА
  5. */
  6.  
  7. #include "pch.h"
  8. #include <iostream>
  9. #include <string>
  10. #include <fstream>
  11. using namespace std;
  12.  
  13. struct List
  14. {
  15. short num;
  16. List *next;
  17. bool end = false;
  18. };
  19.  
  20. List *makeList(short count)
  21. {
  22. short i=0;
  23. List *first = new List;
  24. List *next = new List;
  25. first->next = nullptr;
  26. while (i++ < count)
  27. {
  28. if (first->next == nullptr)
  29. {
  30. first->num = i;
  31. first->next = next;
  32. }
  33. else
  34. {
  35. next->next = new List;
  36. next->num = i;
  37. if (i == count) next->next = first;
  38. else
  39. next = next->next;
  40. }
  41. }
  42. next->end = true;
  43. return first;
  44. }
  45.  
  46.  
  47. void print(List *fList)
  48. {
  49. List *list = fList;
  50. List *endList = list;
  51. cout << "List:";
  52. while (!list->end)
  53. {
  54. cout << list->num << " ->";
  55. list = list->next;
  56. }
  57. cout << list->num << endl << "________________________________\n\n";
  58. }
  59.  
  60. void deleteElement(List *prev, List *forDelete)
  61. {
  62. prev->next = forDelete->next;
  63. delete[] forDelete;
  64. }
  65.  
  66. void calcElement(short deleteOrder, List *fList, short count)
  67. {
  68. List *prev = fList, *list = fList;
  69. short countAtNow = count, dOrder = 1;
  70. while (countAtNow != 1)
  71. {
  72. if (abs(dOrder - deleteOrder) == 1)
  73. {
  74. prev = list;
  75. list = list->next;
  76. cout << list->num << " ->";
  77. deleteElement(prev, list);
  78. list = prev->next;
  79. countAtNow--;
  80. dOrder = 1;
  81. }
  82. else
  83. {
  84. dOrder++;
  85. prev = list;
  86. list = list->next;
  87. }
  88. }
  89. list = list->next;
  90. cout << list->num << endl;
  91. delete[] list;
  92. }
  93.  
  94.  
  95. int main()
  96. {
  97. setlocale(0, "");
  98. ///*
  99. short deleteOrder, count;
  100. cout << "Введите количество детей, а после порядок счёта:\n"; cin >> count >> deleteOrder;
  101. List *first = makeList(count);
  102. print(first);
  103. calcElement(deleteOrder, first, count);
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement