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
106 changes: 106 additions & 0 deletions src/28oct25/loop_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "loop_list.h"
#include <stdio.h>
#include <stdlib.h>


static LoopListNode* createNode(int value)
{
LoopListNode* node = (LoopListNode*)malloc(sizeof(LoopListNode));
node->value = value;
node->next = NULL;
return node;
}

LoopList* loopListNew(void)
{
LoopList* list = (LoopList*)malloc(sizeof(LoopList));
list->head = NULL;
return list;
}

void loopListInsert(LoopList* list, int value)
{
LoopListNode* newNode = createNode(value);

if (!list->head) {
list->head = newNode;
newNode->next = newNode;
return;
}

LoopListNode* current = list->head;
while (current->next != list->head) {
current = current->next;
}

current->next = newNode;
newNode->next = list->head;
}

void loopListRemove(LoopList* list, int value)
{
LoopListNode* current = list->head;
LoopListNode* prev = NULL;

do {
if (current->value == value)
break;
prev = current;
current = current->next;
} while (current != list->head);

if (current->value != value)
return;

if (current == list->head) {
if (current->next == list->head) {
list->head = NULL;
} else {
LoopListNode* last = list->head;
while (last->next != list->head) {
last = last->next;
}
list->head = current->next;
last->next = list->head;
}
} else {
prev->next = current->next;
}

free(current);
}

void loopListPrint(LoopList* list)
{
printf("[");
if (list->head) {
LoopListNode* current = list->head;
do {
printf("%d", current->value);
if (current->next != list->head)
printf(", ");
current = current->next;
} while (current != list->head);
}
printf("]\n");
}

void loopListDelete(LoopList* list)
{
if (list->head) {
LoopListNode* current = list->head;
LoopListNode* last = list->head;
while (last->next != list->head) {
last = last->next;
}
last->next = NULL;

while (current) {
LoopListNode* next = current->next;
free(current);
current = next;
}
}

free(list);
}
29 changes: 29 additions & 0 deletions src/28oct25/loop_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <stdbool.h>

typedef struct LoopListNode LoopListNode;

struct LoopListNode {
int value;
LoopListNode* next;
};

typedef struct {
LoopListNode* head;
} LoopList;

// Создает новый циклический список
LoopList* loopListNew(void);

// Вставляет элемент в конец циклического списка
void loopListInsert(LoopList* list, int value);

// Удаляет первое вхождение элемента с заданным значением
void loopListRemove(LoopList* list, int value);

// Распечатывает содержимое циклического списка
void loopListPrint(LoopList* list);

// Удаляет список и освобождает память
void loopListDelete(LoopList* list);
67 changes: 67 additions & 0 deletions src/28oct25/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "sorted_list.h"
#include <stdio.h>
#include <stdlib.h>

static void printList(SortedList* list)
{
int size;
int* array = sortedListToArray(list, &size);
if (!array) {
printf("[]\n");
return;
}

printf("[");
for (int i = 0; i < size; i++) {
printf("%d", array[i]);
if (i < size - 1)
printf(", ");
}
printf("]\n");

free(array);
}

int main(void)
{
SortedList* list = sortedListNew();
int choice = -1;
int value;

printf("0 – выйти\n");
printf("1 – добавить значение в сортированный список\n");
printf("2 – удалить значение из списка\n");
printf("3 – распечатать список\n");

while (choice != 0) {
printf("> ");
if (scanf("%d", &choice) != 1) {
while (getchar() != '\n');
continue;
}

switch (choice) {
case 1:
printf("Введите значение: ");
scanf("%d", &value);
sortedListInsert(list, value);
break;
case 2:
printf("Введите значение для удаления: ");
scanf("%d", &value);
sortedListRemove(list, value);
break;
case 3:
printList(list);
break;
case 0:
break;
default:
printf("Неверный выбор!\n");
break;
}
}

sortedListDelete(list);
return 0;
}
48 changes: 48 additions & 0 deletions src/28oct25/schitalochka.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "loop_list.h"
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int n, m;
scanf("%d %d", &n, &m);
LoopList* list = loopListNew();

for (int i = 1; i <= n; ++i)
loopListInsert(list, i);

if (!list->head) {
loopListDelete(list);
return 0;
}

LoopListNode* current = list->head;

LoopListNode* prev = list->head;
while (prev->next != list->head)
prev = prev->next;

printf("Порядок удаления: ");
while (list->head->next != list->head) {
for (int i = 0; i < m - 1; ++i) {
prev = current;
current = current->next;
}

printf("%d ", current->value);
LoopListNode* toDelete = current;

if (toDelete == list->head)
list->head = toDelete->next;

prev->next = toDelete->next;
current = toDelete->next;
free(toDelete);
}

printf("\nПоследний оставшийся: %d\n", list->head->value);

loopListDelete(list);

return 0;
}
104 changes: 104 additions & 0 deletions src/28oct25/sorted_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "sorted_list.h"
#include <stdio.h>
#include <stdlib.h>

struct SortedListNode {
int value;
SortedListNode* next;
};

static SortedListNode* createNode(int value)
{
SortedListNode* node = (SortedListNode*)malloc(sizeof(SortedListNode));
node->value = value;
node->next = NULL;
return node;
}

SortedList* sortedListNew(void)
{
SortedList* list = (SortedList*)malloc(sizeof(SortedList));
list->head = NULL;
return list;
}

void sortedListInsert(SortedList* list, int value)
{
SortedListNode* newNode = createNode(value);

if (!list->head || list->head->value >= value) {
newNode->next = list->head;
list->head = newNode;
return;
}

SortedListNode* current = list->head;
while (current->next && current->next->value < value) {
current = current->next;
}

newNode->next = current->next;
current->next = newNode;
}

void sortedListRemove(SortedList* list, int value)
{
SortedListNode* current = list->head;
SortedListNode* prev = NULL;

while (current && current->value != value) {
prev = current;
current = current->next;
}

if (!current)
return;

if (!prev) {
list->head = current->next;
} else {
prev->next = current->next;
}

free(current);
}

int* sortedListToArray(SortedList* list, int* outSize)
{
// Считаем количество элементов
int size = 0;
SortedListNode* current = list->head;
while (current) {
size++;
current = current->next;
}

// Выделяем память под массив
int* array = (int*)malloc(size * sizeof(int));
if (!array) {
*outSize = 0;
return NULL;
}

// Копируем значения
current = list->head;
for (int i = 0; i < size; i++) {
array[i] = current->value;
current = current->next;
}

*outSize = size;
return array;
}

void sortedListDelete(SortedList* list)
{
SortedListNode* current = list->head;
while (current) {
SortedListNode* next = current->next;
free(current);
current = next;
}

free(list);
}
24 changes: 24 additions & 0 deletions src/28oct25/sorted_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <stdbool.h>

typedef struct SortedListNode SortedListNode;

typedef struct {
SortedListNode* head;
} SortedList;

// Создает новый отсортированный список
SortedList* sortedListNew(void);

// Вставляет элемент в список, сохраняя порядок сортировки
void sortedListInsert(SortedList* list, int value);

// Удаляет первое вхождение элемента с заданным значением
void sortedListRemove(SortedList* list, int value);

// Возвращает содержимое списка в виде массива (caller должен освободить память)
int* sortedListToArray(SortedList* list, int* outSize);

// Удаляет список и освобождает память
void sortedListDelete(SortedList* list);
Loading