Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // task_2.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- /*
- РЕАЛИЗОВАТЬ ДЕТСКУЮ СЧИТАЛКУ В КОЛЬЦЕВОМ СПИСКЕ. уДАЛЕНИЕ КАЖДОГО К-ТОГО ЭЛЕМЕНТА
- */
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- struct List
- {
- short num;
- List *next;
- bool end = false;
- };
- List *makeList(short count)
- {
- short i=0;
- List *first = new List;
- List *next = new List;
- first->next = nullptr;
- while (i++ < count)
- {
- if (first->next == nullptr)
- {
- first->num = i;
- first->next = next;
- }
- else
- {
- next->next = new List;
- next->num = i;
- if (i == count) next->next = first;
- else
- next = next->next;
- }
- }
- next->end = true;
- return first;
- }
- void print(List *fList)
- {
- List *list = fList;
- List *endList = list;
- cout << "List:";
- while (!list->end)
- {
- cout << list->num << " ->";
- list = list->next;
- }
- cout << list->num << endl << "________________________________\n\n";
- }
- void deleteElement(List *prev, List *forDelete)
- {
- prev->next = forDelete->next;
- delete[] forDelete;
- }
- void calcElement(short deleteOrder, List *fList, short count)
- {
- List *prev = fList, *list = fList;
- short countAtNow = count, dOrder = 1;
- while (countAtNow != 1)
- {
- if (abs(dOrder - deleteOrder) == 1)
- {
- prev = list;
- list = list->next;
- cout << list->num << " ->";
- deleteElement(prev, list);
- list = prev->next;
- countAtNow--;
- dOrder = 1;
- }
- else
- {
- dOrder++;
- prev = list;
- list = list->next;
- }
- }
- list = list->next;
- cout << list->num << endl;
- delete[] list;
- }
- int main()
- {
- setlocale(0, "");
- ///*
- short deleteOrder, count;
- cout << "Введите количество детей, а после порядок счёта:\n"; cin >> count >> deleteOrder;
- List *first = makeList(count);
- print(first);
- calcElement(deleteOrder, first, count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement