Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions 6/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Устанавливаем минимальную версию CMake
cmake_minimum_required(VERSION 3.25)
# Указываем название проекта и используемый язык(и)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

project(LoopSortedList C)
project(MainSortedList C)

# Перечисляем библиотеки
add_library(SortedList sorted_list.c)

# Указываем исполняемый файл
add_executable(LoopSortedList loop_sorted_list.c)
add_executable(MainSortedList main_sorted_list.c)
# Связываемся с библиотеками
target_link_libraries(LoopSortedList PRIVATE SortedList)

target_link_libraries(MainSortedList PRIVATE SortedList)
61 changes: 61 additions & 0 deletions 6/loop_sorted_list.c
Original file line number Diff line number Diff line change
@@ -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);
}
43 changes: 43 additions & 0 deletions 6/main_sorted_list.c
Original file line number Diff line number Diff line change
@@ -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);
}
}
106 changes: 106 additions & 0 deletions 6/sorted_list.c
Original file line number Diff line number Diff line change
@@ -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");
}
22 changes: 22 additions & 0 deletions 6/sorted_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>

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);