diff --git a/6/loop_sorted_list.c b/6/loop_sorted_list.c new file mode 100644 index 0000000..d0f9335 --- /dev/null +++ b/6/loop_sorted_list.c @@ -0,0 +1,61 @@ +#include "sorted_list.h" + +void main() +{ + List list; + initList(&list); + + int n; + int m; + + printf("Количество войнов: "); + scanf("%d", &n); + + printf("Убивают каждого: "); + scanf("%d", &m); + + if (n <= 0) { + printf("n должно быть > 0\n"); + return; + } + + if (m == 1) { + printf("Нужно встать на %d место\n", n); + return; + } + + for (int i = 1; i <= n; i++) { + addValue(&list, i); + } + + Node* head = list.head; + + while (head->next != NULL) { + head = (*head).next; + } + + (*head).next = list.head; + int num = 1; + + head = list.head; + Node* prev = NULL; + + while (list.size > 1) { + if (num == m) { + (*prev).next = (*head).next; + + head = (*prev).next; + num = 1; + + list.size--; + + continue; + } + + num++; + prev = head; + head = (*head).next; + } + + printf("Нужно встать на %d место\n", (*head).value); +} diff --git a/6/main_sorted_list.c b/6/main_sorted_list.c new file mode 100644 index 0000000..ecd2d22 --- /dev/null +++ b/6/main_sorted_list.c @@ -0,0 +1,43 @@ +#include "sorted_list.h" + +void main(int argc, char* argv[]) +{ + List list; + initList(&list); + + printf("0 – выйти\n1 – добавить значение в сортированный список\n2 – удалить значение из списка\n3 – распечатать список\n\n"); + + char inp; + printf("Введите команду: "); + scanf("%c", &inp); + + while (inp != '0') { + int value; + switch (inp) { + case '1': + printf("Добавить: "); + scanf("%d", &value); + + addValue(&list, value); + + break; + + case '2': + printf("Удалить: "); + scanf("%d", &value); + + removeValue(&list, value); + + break; + + case '3': + printf("Список: "); + printList(&list); + break; + default: + printf("Введите команду: "); + break; + } + scanf("%c", &inp); + } +} diff --git a/6/sorted_list.c b/6/sorted_list.c new file mode 100644 index 0000000..3ca7d5b --- /dev/null +++ b/6/sorted_list.c @@ -0,0 +1,106 @@ +#include "sorted_list.h" + +void initList(List* list) +{ + (*list).head = NULL; + (*list).size = 0; +} + +Node* createNode(int value, Node* next) +{ + Node* node = malloc(sizeof(Node)); + if (node == NULL) { + printf("Ошибка выделения памяти\n"); + exit(-1); + } + + (*node).next = next; + (*node).value = value; + + return node; +} + +void addValue(List* list, int value) +{ + Node* head = (*list).head; + + if (head == NULL) { + Node* node = createNode(value, NULL); + + (*list).head = node; + (*list).size++; + return; + } + + Node* buff = NULL; + + while (head->value < value) { + if ((*head).next == NULL) { + Node* node = createNode(value, NULL); + + (*head).next = node; + + (*list).size++; + return; + } + + buff = head; + head = (*head).next; + } + + Node* node = createNode(value, head); + + (*list).size++; + + if (buff != NULL) { + (*buff).next = node; + } + + else { + (*list).head = node; + } +} + +void removeValue(List* list, int value) +{ + Node* iter = (*list).head; + + if (iter == NULL) { + return; + } + + Node* buff = NULL; + + while (iter != NULL) { + int listValue = (*iter).value; + if (listValue == value) { + if (buff != NULL) { + (*buff).next = (*iter).next; + } + + else { + (*list).head = (*iter).next; + } + + free(iter); + (*list).size--; + + iter = (*list).head; + continue; + } + + buff = iter; + iter = (*iter).next; + } +} + +void printList(List* list) +{ + Node* iter = (*list).head; + while (iter != NULL) { + int value = (*iter).value; + printf("%d ", value); + iter = (*iter).next; + } + printf("\n"); +} diff --git a/6/sorted_list.h b/6/sorted_list.h new file mode 100644 index 0000000..e8d60ba --- /dev/null +++ b/6/sorted_list.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include + +struct Node { + int value; + struct Node* next; + +} typedef Node; + +struct List { + Node* head; + int size; +} typedef List; + +void initList(List* list); +Node* createNode(int value, Node* next); + +void addValue(List* list, int value); +void removeValue(List* list, int value); + +void printList(List* list);