Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- struct Element {
- public:
- int Data;
- Element* next;
- Element(int Data) {
- this->Data = Data;
- }
- };
- struct RingList {
- Element* anchor = NULL;
- int size = 0;
- };
- Element* RingList_GetPrevious(Element* elem);
- Element* RingList_FindLast(RingList& ring) {
- Element* head = ring.anchor;
- Element* walker = ring.anchor;
- while (walker->next != head) {
- walker = walker->next;
- }
- return walker;
- }
- void RingList_Print(RingList& ring) {
- Element* head = ring.anchor;
- Element* walker = ring.anchor;
- while (walker->next != head) {
- cout << walker->Data << " ";
- walker = walker->next;
- }
- cout << walker->Data << endl;
- }
- void RingList_Remove(RingList& ring, Element* elem) {
- if (ring.anchor = elem) {
- Element* last = RingList_FindLast(ring);
- Element* another = ring.anchor->next;
- last->next = another;
- ring.anchor = another;
- }
- else {
- Element* prev = RingList_GetPrevious(elem);
- prev->next = elem->next;
- elem = elem->next;
- }
- ring.size--;
- }
- Element* RingList_GetPrevious(Element* elem) {
- Element* walker = elem;
- while (walker->next != elem) {
- walker = walker->next;
- }
- return walker;
- }
- void ChikiBricki(RingList& ring, int step) {
- Element* walker = ring.anchor;
- while (ring.size > 1) {
- for (int i = 0; i < step; i++) {
- walker = walker->next;
- }
- RingList_Print(ring);
- RingList_Remove(ring, walker);
- }
- }
- void RingList_Push(RingList &ring, int Data) {
- if (ring.anchor == NULL) {
- ring.anchor = new Element(Data);
- ring.anchor->next = ring.anchor;
- }
- else {
- Element* last = RingList_FindLast(ring);
- last->next = new Element(Data);
- last->next->next = ring.anchor;
- }
- ring.size++;
- }
- int main()
- {
- const int step = 3;
- srand(time(0));
- RingList ring;
- for (int i = 0; i < 15; i++) {
- RingList_Push(ring, i);
- }
- ChikiBricki(ring, step);
- RingList_Print(ring);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement