Advertisement
RadioNurshat

Чики-брики пальчик выкинь

Mar 2nd, 2021
1,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. struct Element {
  5. public:
  6.     int Data;
  7.     Element* next;
  8.     Element(int Data) {
  9.         this->Data = Data;
  10.     }
  11. };
  12. struct RingList {
  13.     Element* anchor = NULL;
  14.     int size = 0;
  15. };
  16. Element* RingList_GetPrevious(Element* elem);
  17. Element* RingList_FindLast(RingList& ring) {
  18.     Element* head = ring.anchor;
  19.     Element* walker = ring.anchor;
  20.     while (walker->next != head) {
  21.         walker = walker->next;
  22.     }
  23.     return walker;
  24. }
  25. void RingList_Print(RingList& ring) {
  26.     Element* head = ring.anchor;
  27.     Element* walker = ring.anchor;
  28.     while (walker->next != head) {
  29.         cout << walker->Data << " ";
  30.         walker = walker->next;
  31.     }
  32.     cout << walker->Data << endl;
  33. }
  34. void RingList_Remove(RingList& ring, Element* elem) {
  35.  
  36.     if (ring.anchor = elem) {
  37.         Element* last = RingList_FindLast(ring);
  38.         Element* another = ring.anchor->next;
  39.         last->next = another;
  40.         ring.anchor = another;
  41.  
  42.     }
  43.     else {
  44.         Element* prev = RingList_GetPrevious(elem);
  45.         prev->next = elem->next;
  46.         elem = elem->next;
  47.     }
  48.    
  49.     ring.size--;
  50. }
  51. Element* RingList_GetPrevious(Element* elem) {
  52.     Element* walker = elem;
  53.     while (walker->next != elem) {
  54.         walker = walker->next;
  55.     }
  56.     return walker;
  57. }
  58. void ChikiBricki(RingList& ring, int step) {
  59.     Element* walker = ring.anchor;
  60.     while (ring.size > 1) {
  61.         for (int i = 0; i < step; i++) {
  62.             walker = walker->next;
  63.         }
  64.         RingList_Print(ring);
  65.         RingList_Remove(ring, walker);
  66.     }
  67. }
  68. void RingList_Push(RingList &ring, int Data) {
  69.     if (ring.anchor == NULL) {
  70.         ring.anchor = new Element(Data);
  71.         ring.anchor->next = ring.anchor;
  72.     }
  73.     else {
  74.         Element* last = RingList_FindLast(ring);
  75.         last->next = new Element(Data);
  76.         last->next->next = ring.anchor;
  77.     }
  78.     ring.size++;
  79. }
  80.  
  81.  
  82. int main()
  83. {
  84.     const int step = 3;
  85.     srand(time(0));
  86.     RingList ring;
  87.  
  88.     for (int i = 0; i < 15; i++) {
  89.         RingList_Push(ring, i);
  90.     }
  91.     ChikiBricki(ring, step);
  92.     RingList_Print(ring);
  93. }
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement