From 8a3369908c645670668e9374febdc620c7d844b2 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Tue, 30 Sep 2025 21:38:27 +0300 Subject: [PATCH 01/23] Balance of brackets checker --- 15.09/src/1.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 15.09/src/1.c diff --git a/15.09/src/1.c b/15.09/src/1.c new file mode 100644 index 0000000..c8ae4ee --- /dev/null +++ b/15.09/src/1.c @@ -0,0 +1,38 @@ +#include +#include + +int main(void) +{ + int n = 0; + scanf("%d\n", &n); + + char *string = malloc((n + 1) * sizeof(char)); + for (int i = 0; i < n; i++) { + scanf("%d\n", string[i]); + } + + int balance = 0; + + for (int charNum = 0; charNum < n; charNum++) { + if (string[charNum] == '(') { + balance++; + } else if (string[charNum] == ')') { + balance--; + if (balance < 0) { + printf("Баланс скобок не соблюдён\n"); + free(string); + return 0; + } + } + } + + if (balance != 0) { + printf("Баланс скобок не соблюдён\n"); + free(string); + return 0; + } + + printf("Баланс скобок соблюдён\n"); + free(string); + return 0; +} \ No newline at end of file From 0018de60159775841b2b337031fc3f9f25cc6e9d Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Tue, 30 Sep 2025 21:38:50 +0300 Subject: [PATCH 02/23] substring count --- 15.09/src/2.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 15.09/src/2.c diff --git a/15.09/src/2.c b/15.09/src/2.c new file mode 100644 index 0000000..bbe4320 --- /dev/null +++ b/15.09/src/2.c @@ -0,0 +1,42 @@ +#include +#include + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + + +int main(void) +{ + int substrCount = 0; + int nS = 0; + int nS1 = 0; + scanf("%d%d\n", &nS, &nS1); + + char *s = malloc((nS + 1) * sizeof(char)); + char *s1 = malloc((nS1 + 1) * sizeof(char)); + + fgets(s, nS + 1, stdin); + getchar(); + fgets(s1, nS1 + 1, stdin); + + for (int i = 0; i < nS; i++) { + int strike = 0; + + for (int o = i; o < MIN(nS, i + nS1); o++) { + if (s1[o - i] != s[o]) { + break; + } + strike++; + } + + if (strike == nS1) { + substrCount++; + } + } + + printf("%d\n", substrCount); + + free(s); + free(s1); + + return 0; +} \ No newline at end of file From dc4b0b5390c37bfe410a027fddbca146dbeb293a Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Tue, 30 Sep 2025 21:39:04 +0300 Subject: [PATCH 03/23] zero elements count --- 15.09/src/3.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 15.09/src/3.c diff --git a/15.09/src/3.c b/15.09/src/3.c new file mode 100644 index 0000000..c0abb49 --- /dev/null +++ b/15.09/src/3.c @@ -0,0 +1,25 @@ +#include +#include + +int main(void) +{ + int n = 0; + int counter = 0; + scanf("%d\n", &n); + + int *array = malloc(n * sizeof(int)); + for (int i = 0; i < n; i++) { + scanf("%d", &array[i]); + } + + for (int i = 0; i < n; i++) { + if (!array[i]) { + counter++; + } + } + + printf("%d\n", counter); + + free(array); + return 0; +} \ No newline at end of file From c7ba069e890f9d2af0e85b7c7ceeecc1b517b6d6 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 27 Oct 2025 19:10:16 +0300 Subject: [PATCH 04/23] removed logic from main --- 15.09/src/1.c | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/15.09/src/1.c b/15.09/src/1.c index c8ae4ee..f9506e7 100644 --- a/15.09/src/1.c +++ b/15.09/src/1.c @@ -1,38 +1,26 @@ #include #include +#include +#include + +int bracketsChecker(char* string, bool* result) +{ + +} int main(void) { int n = 0; scanf("%d\n", &n); - char *string = malloc((n + 1) * sizeof(char)); - for (int i = 0; i < n; i++) { - scanf("%d\n", string[i]); - } - - int balance = 0; + char* input = calloc(n + 1, sizeof(char)); + fgets(input, n + 1, stdin); - for (int charNum = 0; charNum < n; charNum++) { - if (string[charNum] == '(') { - balance++; - } else if (string[charNum] == ')') { - balance--; - if (balance < 0) { - printf("Баланс скобок не соблюдён\n"); - free(string); - return 0; - } - } - } + bool result = true; - if (balance != 0) { - printf("Баланс скобок не соблюдён\n"); - free(string); - return 0; - } + bracketsChecker(input, &result); + printf("%d\n", result); - printf("Баланс скобок соблюдён\n"); - free(string); + free(input); return 0; } \ No newline at end of file From 59d25c1d12cc1e1935b7e0e6c993463ec21c953b Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 27 Oct 2025 19:21:06 +0300 Subject: [PATCH 05/23] Brackets checker --- 15.09/src/1.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/15.09/src/1.c b/15.09/src/1.c index f9506e7..54bd5eb 100644 --- a/15.09/src/1.c +++ b/15.09/src/1.c @@ -5,7 +5,25 @@ int bracketsChecker(char* string, bool* result) { + int balance = 0; + unsigned long len = strlen(string); + for (unsigned long i = 0; i < len; i++) { + if (string[i] == '(') { + balance++; + } else if (string[i] == ')') { + balance--; + if (balance < 0) { + *result = false; + } + } + } + + if (balance != 0) { + *result = false; + } + + return 0; } int main(void) From 1bc898ff64ebded61faf4caf53b214b9ffebb957 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Thu, 30 Oct 2025 17:20:51 +0300 Subject: [PATCH 06/23] Removed logic from main --- 15.09/src/2.c | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/15.09/src/2.c b/15.09/src/2.c index bbe4320..731f113 100644 --- a/15.09/src/2.c +++ b/15.09/src/2.c @@ -3,37 +3,43 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) - -int main(void) +int substrCount(char* s, char* s1, int lenS, int lenS1, int* counter) { - int substrCount = 0; - int nS = 0; - int nS1 = 0; - scanf("%d%d\n", &nS, &nS1); - - char *s = malloc((nS + 1) * sizeof(char)); - char *s1 = malloc((nS1 + 1) * sizeof(char)); - - fgets(s, nS + 1, stdin); - getchar(); - fgets(s1, nS1 + 1, stdin); - - for (int i = 0; i < nS; i++) { - int strike = 0; + for (int i = 0; i < lenS; i++) { + int matchLen = 0; - for (int o = i; o < MIN(nS, i + nS1); o++) { + for (int o = i; o < MIN(lenS, i + lenS1); o++) { if (s1[o - i] != s[o]) { break; } - strike++; + matchLen++; } - if (strike == nS1) { - substrCount++; + if (matchLen == lenS1) { + (*counter)++; } } - printf("%d\n", substrCount); + return 0; +} + +int main(void) +{ + int counter = 0; + int lenS = 0; + int lenS1 = 0; + scanf("%d%d\n", &lenS, &lenS1); + + char* s = malloc((lenS + 1) * sizeof(char)); + char* s1 = malloc((lenS1 + 1) * sizeof(char)); + + fgets(s, lenS + 1, stdin); + getchar(); + fgets(s1, lenS1 + 1, stdin); + + substrCount(s, s1, lenS, lenS1, &counter); + + printf("%d\n", counter); free(s); free(s1); From 9e128980b45765e325dd76398896b47ad7c90230 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Thu, 30 Oct 2025 18:04:24 +0300 Subject: [PATCH 07/23] changed return type --- 15.09/src/1.c | 4 ++-- 15.09/src/2.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/15.09/src/1.c b/15.09/src/1.c index 54bd5eb..46f032f 100644 --- a/15.09/src/1.c +++ b/15.09/src/1.c @@ -3,7 +3,7 @@ #include #include -int bracketsChecker(char* string, bool* result) +bool bracketsChecker(char* string, bool* result) { int balance = 0; unsigned long len = strlen(string); @@ -23,7 +23,7 @@ int bracketsChecker(char* string, bool* result) *result = false; } - return 0; + return ; } int main(void) diff --git a/15.09/src/2.c b/15.09/src/2.c index 731f113..51e3d51 100644 --- a/15.09/src/2.c +++ b/15.09/src/2.c @@ -1,9 +1,10 @@ #include #include +#include #define MIN(a, b) ((a) < (b) ? (a) : (b)) -int substrCount(char* s, char* s1, int lenS, int lenS1, int* counter) +bool substrCount(char* s, char* s1, int lenS, int lenS1, int* counter) { for (int i = 0; i < lenS; i++) { int matchLen = 0; @@ -20,7 +21,7 @@ int substrCount(char* s, char* s1, int lenS, int lenS1, int* counter) } } - return 0; + return false; } int main(void) From b34f58acda89349f79c4b4fc12bbc253c9a29b04 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Thu, 30 Oct 2025 18:08:30 +0300 Subject: [PATCH 08/23] removed pointers in functions --- 15.09/src/1.c | 11 ++++++----- 15.09/src/2.c | 9 +++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/15.09/src/1.c b/15.09/src/1.c index 46f032f..e5f0148 100644 --- a/15.09/src/1.c +++ b/15.09/src/1.c @@ -3,8 +3,9 @@ #include #include -bool bracketsChecker(char* string, bool* result) +bool bracketsChecker(char* string) { + bool result = true; int balance = 0; unsigned long len = strlen(string); @@ -14,16 +15,16 @@ bool bracketsChecker(char* string, bool* result) } else if (string[i] == ')') { balance--; if (balance < 0) { - *result = false; + result = false; } } } if (balance != 0) { - *result = false; + result = false; } - return ; + return result; } int main(void) @@ -36,7 +37,7 @@ int main(void) bool result = true; - bracketsChecker(input, &result); + result = bracketsChecker(input); printf("%d\n", result); free(input); diff --git a/15.09/src/2.c b/15.09/src/2.c index 51e3d51..c803d82 100644 --- a/15.09/src/2.c +++ b/15.09/src/2.c @@ -4,8 +4,9 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) -bool substrCount(char* s, char* s1, int lenS, int lenS1, int* counter) +bool substrCount(char* s, char* s1, int lenS, int lenS1) { + int counter = 0; for (int i = 0; i < lenS; i++) { int matchLen = 0; @@ -17,11 +18,11 @@ bool substrCount(char* s, char* s1, int lenS, int lenS1, int* counter) } if (matchLen == lenS1) { - (*counter)++; + counter++; } } - return false; + return counter; } int main(void) @@ -38,7 +39,7 @@ int main(void) getchar(); fgets(s1, lenS1 + 1, stdin); - substrCount(s, s1, lenS, lenS1, &counter); + counter = substrCount(s, s1, lenS, lenS1); printf("%d\n", counter); From 3f2979fcd1388da28c51b853069632309b3fa491 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Sun, 9 Nov 2025 18:51:02 +0300 Subject: [PATCH 09/23] List implementation --- 06_lists/src/list.c | 192 ++++++++++++++++++++++++++++++++++++++++++++ 06_lists/src/list.h | 15 ++++ 2 files changed, 207 insertions(+) create mode 100644 06_lists/src/list.c create mode 100644 06_lists/src/list.h diff --git a/06_lists/src/list.c b/06_lists/src/list.c new file mode 100644 index 0000000..2e8412e --- /dev/null +++ b/06_lists/src/list.c @@ -0,0 +1,192 @@ +#include "list.h" +#include +#include + +typedef struct Node { + int data; + struct Node* next; +} Node; + +typedef struct List { + Node* head; +} List; + +List* newList() +{ + List* newList = calloc(1, sizeof(List)); + return newList; +} + +Node* createNode(int data) +{ + Node* newNode = calloc(1, sizeof(Node)); + + if (newNode == NULL) { + return NULL; + } + + newNode->data = data; + newNode->next = NULL; + + return newNode; +} + +bool insert(List* list, int data, unsigned index) +{ + if (list == NULL) { + return false; + } + + Node* newNode = createNode(data); + + if (newNode == NULL) { + return false; + } + + if (index == 0) { + newNode->next = list->head; + list->head = newNode; + return true; + } + + if (list->head == NULL) { + free(newNode); + return false; + } + + Node* before = list->head; + + for (unsigned i = 1; i < index; i++) { + before = before->next; + + if (before == NULL) { + free(newNode); + return false; + } + } + + Node* ahead = before->next; + before->next = newNode; + newNode->next = ahead; + + return true; +} + +bool pop(List* list, unsigned index, int* res) +{ + if (list == NULL || list->head == NULL || res == NULL) { + return false; + } + + Node* before = list->head; + + if (index == 0) { + *res = before->data; + list->head = before->next; + free(before); + return true; + } + + for (unsigned i = 1; i < index; i++) { + before = before->next; + + if (before == NULL) { + return false; + } + } + + Node* current = before->next; + if (current == NULL) { + return false; + } + + *res = current->data; + before->next = current->next; + + free(current); + return true; +} + +bool find(List* list, unsigned index, int* res) +{ + if (list == NULL || list->head == NULL || res == NULL) { + return false; + } + + Node* current = list->head; + + for (unsigned i = 0; i < index; i++) { + current = current->next; + + if (current == NULL) { + return false; + } + } + + *res = current->data; + return true; +} + +bool indexOf(List* list, int data, unsigned* res) +{ + if (list == NULL || list->head == NULL || res == NULL) { + return false; + } + + Node* current = list->head; + + unsigned i = 0; + while (current != NULL && current->data != data) { + i++; + current = current->next; + } + + if (current == NULL) { + return false; + } + + *res = i; + return true; +} + +bool isEmpty(List* list) +{ + return list == NULL || list->head == NULL; +} + +bool deleteList(List* list) +{ + if (list == NULL) { + return true; + } + + Node* current = list->head; + + while (current != NULL) { + Node* next = current->next; + free(current); + current = next; + } + + free(list); + return true; +} + +bool len(List* list, unsigned* res) +{ + unsigned len = 0; + + if (list == NULL || res == NULL) { + return false; + } + + Node* current = list->head; + + while (current != NULL) { + len++; + current = current->next; + } + + *res = len; + return true; +} diff --git a/06_lists/src/list.h b/06_lists/src/list.h new file mode 100644 index 0000000..d75cb19 --- /dev/null +++ b/06_lists/src/list.h @@ -0,0 +1,15 @@ +#pragma once +#include + +typedef struct Node Node; +typedef struct List List; + +List* newList(); +Node* createNode(int data); +bool insert(List* list, int data, unsigned index); +bool pop(List* list, unsigned index, int* res); +bool find(List* list, unsigned index, int* res); +bool indexOf(List* list, int data, unsigned* res); +bool isEmpty(List* list); +bool deleteList(List* list); +bool len(List* list, unsigned* res); From 52ecf128203426dd60cbfd12ed08f5d8f70a452b Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Wed, 12 Nov 2025 03:41:46 +0300 Subject: [PATCH 10/23] Added printList, extended indexOf and isEmpty --- 06_lists/src/list.c | 56 +++++++++++++++++++++++++++++++++++++-------- 06_lists/src/list.h | 5 ++-- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/06_lists/src/list.c b/06_lists/src/list.c index 2e8412e..6f72207 100644 --- a/06_lists/src/list.c +++ b/06_lists/src/list.c @@ -1,4 +1,5 @@ #include "list.h" +#include #include #include @@ -127,31 +128,40 @@ bool find(List* list, unsigned index, int* res) return true; } -bool indexOf(List* list, int data, unsigned* res) +int indexOf(List* list, int data, unsigned* res) { - if (list == NULL || list->head == NULL || res == NULL) { - return false; + if (list == NULL || res == NULL) { + return 2; + } + + if (list->head == NULL) { + *res = 0; + return 1; } Node* current = list->head; unsigned i = 0; - while (current != NULL && current->data != data) { + while (current != NULL && current->data < data) { i++; current = current->next; } - if (current == NULL) { - return false; + *res = i; + if (current == NULL || current->data != data) { + return 1; } - *res = i; - return true; + return 0; } -bool isEmpty(List* list) +int isEmpty(List* list) { - return list == NULL || list->head == NULL; + if (list == NULL) { + return 2; + } + + return list->head == NULL; } bool deleteList(List* list) @@ -190,3 +200,29 @@ bool len(List* list, unsigned* res) *res = len; return true; } + +bool printList(List* list, unsigned startIndex, unsigned endIndex) +{ + if (list == NULL || list->head == NULL || startIndex >= endIndex) { + return false; + } + + Node* current = list->head; + for (unsigned i = 0; i < startIndex; i++) { + current = current->next; + if (current == NULL) { + return false; + } + } + + for (unsigned i = startIndex; i < endIndex; i++) { + printf("%d ", current->data); + current = current->next; + if (current == NULL) { + break; + } + } + printf("\n"); + + return true; +} diff --git a/06_lists/src/list.h b/06_lists/src/list.h index d75cb19..708d0c5 100644 --- a/06_lists/src/list.h +++ b/06_lists/src/list.h @@ -9,7 +9,8 @@ Node* createNode(int data); bool insert(List* list, int data, unsigned index); bool pop(List* list, unsigned index, int* res); bool find(List* list, unsigned index, int* res); -bool indexOf(List* list, int data, unsigned* res); -bool isEmpty(List* list); +int indexOf(List* list, int data, unsigned* res); +int isEmpty(List* list); bool deleteList(List* list); bool len(List* list, unsigned* res); +bool printList(List* list, unsigned startIndex, unsigned endIndex); From 616b4a47e89eaad56f70899d0023ec7a89a9e44c Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Wed, 12 Nov 2025 03:41:54 +0300 Subject: [PATCH 11/23] Sorted list program --- 06_lists/src/sortedList.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 06_lists/src/sortedList.c diff --git a/06_lists/src/sortedList.c b/06_lists/src/sortedList.c new file mode 100644 index 0000000..4daaac2 --- /dev/null +++ b/06_lists/src/sortedList.c @@ -0,0 +1,39 @@ +#include "list.h" +#include + +int main(void) +{ + int op = 0; + List* list = newList(); + if (list == NULL) { + return 1; + } + + while (scanf("%d", &op) == 1) { + if (op == 0) { + deleteList(list); + return 0; + } else if (op == 1) { + unsigned index = 0; + int val = 0; + scanf("%d", &val); + + if (indexOf(list, val, &index) != 2) { + insert(list, val, index); + } + } else if (op == 2) { + int val = 0; + scanf("%d", &val); + + unsigned index = 0; + if (!indexOf(list, val, &index)) { + pop(list, index, &val); + } + } else if (op == 3) { + printList(list, 0, -1); + } + } + + deleteList(list); + return 0; +} From 336d68d4866371cbfafc58b7f4d5ac90f61dfe5a Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Wed, 12 Nov 2025 03:59:12 +0300 Subject: [PATCH 12/23] Rewrited with switch --- 06_lists/src/sortedList.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/06_lists/src/sortedList.c b/06_lists/src/sortedList.c index 4daaac2..2b05f7e 100644 --- a/06_lists/src/sortedList.c +++ b/06_lists/src/sortedList.c @@ -10,27 +10,32 @@ int main(void) } while (scanf("%d", &op) == 1) { - if (op == 0) { + unsigned index = 0; + int val = 0; + + switch (op) { + case 0: deleteList(list); return 0; - } else if (op == 1) { - unsigned index = 0; - int val = 0; + case 1: scanf("%d", &val); if (indexOf(list, val, &index) != 2) { insert(list, val, index); } - } else if (op == 2) { - int val = 0; + + break; + case 2: scanf("%d", &val); - unsigned index = 0; if (!indexOf(list, val, &index)) { pop(list, index, &val); } - } else if (op == 3) { + + break; + case 3: printList(list, 0, -1); + break; } } From 8b3062632f51d7a9dc2417783824406922801809 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Sun, 23 Nov 2025 22:34:30 +0300 Subject: [PATCH 13/23] Circular list implementation --- 06_lists/src/circularList.c | 203 ++++++++++++++++++++++++++++++++++++ 06_lists/src/circularList.h | 13 +++ 2 files changed, 216 insertions(+) create mode 100644 06_lists/src/circularList.c create mode 100644 06_lists/src/circularList.h diff --git a/06_lists/src/circularList.c b/06_lists/src/circularList.c new file mode 100644 index 0000000..04f8a0a --- /dev/null +++ b/06_lists/src/circularList.c @@ -0,0 +1,203 @@ +#include "circularList.h" +#include +#include +#include + +typedef struct Node { + int data; + struct Node* next; +} Node; + +typedef struct List { + Node* head; +} List; + +List* newList() +{ + List* newList = calloc(1, sizeof(List)); + return newList; +} + +Node* createNode(int data) +{ + Node* newNode = calloc(1, sizeof(Node)); + + if (newNode == NULL) { + return NULL; + } + + newNode->data = data; + newNode->next = NULL; + + return newNode; +} + +bool insert(List* list, int data, unsigned index) +{ + if (list == NULL) { + return false; + } + + Node* newNode = createNode(data); + + if (newNode == NULL) { + return false; + } + + if (list->head == NULL) { + if (index == 0) { + newNode->next = newNode; + list->head = newNode; + return true; + } + free(newNode); + return false; + } + + if (index == 0) { + newNode->next = list->head; + Node* current = list->head; + + while (current->next != list->head) { + current = current->next; + } + + current->next = newNode; + list->head = newNode; + return true; + } + + Node* before = list->head; + + for (unsigned i = 1; i < index; i++) { + before = before->next; + + if (before == list->head) { + free(newNode); + return false; + } + } + + Node* ahead = before->next; + before->next = newNode; + newNode->next = ahead; + + return true; +} + +bool del(List* list, unsigned index) +{ + if (list == NULL || list->head == NULL) { + return false; + } + + Node* before = list->head; + + if (index == 0) { + if (before->next == before) { + free(before); + list->head = NULL; + return true; + } + + Node* current = before; + + while (current->next != before) { + current = current->next; + } + current->next = before->next; + list->head = before->next; + free(before); + return true; + } + + for (unsigned i = 1; i < index; i++) { + before = before->next; + + if (before == list->head) { + return false; + } + } + + Node* current = before->next; + if (current == NULL || current == list->head) { + return false; + } + + before->next = current->next; + free(current); + return true; +} + +int isEmpty(List* list) +{ + if (list == NULL) { + return 2; + } + + return list->head == NULL; +} + +bool deleteList(List* list) +{ + if (list == NULL) { + return true; + } + + if (list->head != NULL) { + Node* current = list->head->next; + while (current != list->head) { + Node* next = current->next; + free(current); + current = next; + } + free(list->head); + } + + free(list); + return true; +} + +bool len(List* list, unsigned* res) +{ + unsigned len = 0; + + if (list == NULL || res == NULL) { + return false; + } + + if (list->head == NULL) { + *res = 0; + return true; + } + + Node* current = list->head; + len = 1; + + while (current->next != list->head) { + len++; + current = current->next; + } + + *res = len; + return true; +} + +bool find(List* list, unsigned index, int* res) +{ + if (list == NULL || list->head == NULL || res == NULL) { + return false; + } + + Node* current = list->head; + for (unsigned i = 0; i < index; i++) { + current = current->next; + + if (current == list->head) { + return false; + } + } + + *res = current->data; + return true; +} diff --git a/06_lists/src/circularList.h b/06_lists/src/circularList.h new file mode 100644 index 0000000..0503ff8 --- /dev/null +++ b/06_lists/src/circularList.h @@ -0,0 +1,13 @@ +#pragma once +#include + +typedef struct Node Node; +typedef struct List List; + +List* newList(); +Node* createNode(int data); +bool insert(List* list, int data, unsigned index); +bool pop(List* list, unsigned index, int* res); +int isEmpty(List* list); +bool deleteList(List* list); +bool len(List* list, unsigned* res); From 25e713afacd872300aac5c909a287ceec8a21785 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Sun, 23 Nov 2025 22:45:50 +0300 Subject: [PATCH 14/23] Josephus problem --- 06_lists/src/josephusProblem.c | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 06_lists/src/josephusProblem.c diff --git a/06_lists/src/josephusProblem.c b/06_lists/src/josephusProblem.c new file mode 100644 index 0000000..d6ad41a --- /dev/null +++ b/06_lists/src/josephusProblem.c @@ -0,0 +1,40 @@ +#include "circularList.h" +#include + + + +int main(void) +{ + int n = 0, m = 0; + scanf("%d%d", &n, &m); + + List* list = newList(); + int listLen = n; + + for (int i = 1; i <= n; i++) { + if (!insert(list, i, i - 1)) { + deleteList(list); + return 1; + } + } + + int currentIndex = m - 1; + while (listLen > 2) { + if (!del(list, currentIndex)) { + deleteList(list); + return 1; + } + listLen--; + currentIndex = (currentIndex + m - 1) % listLen; + } + + int res = -1; + if (!find(list, 0, &res)) { + deleteList(list); + return 1; + } + + printf("%d", res); + deleteList(list); + return 0; +} \ No newline at end of file From c7750f726956778f719dd32c5c5272312218188b Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 24 Nov 2025 00:12:15 +0300 Subject: [PATCH 15/23] Fixed circularList.h --- 06_lists/src/circularList.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/06_lists/src/circularList.h b/06_lists/src/circularList.h index 0503ff8..049fcf6 100644 --- a/06_lists/src/circularList.h +++ b/06_lists/src/circularList.h @@ -7,7 +7,8 @@ typedef struct List List; List* newList(); Node* createNode(int data); bool insert(List* list, int data, unsigned index); -bool pop(List* list, unsigned index, int* res); +bool del(List* list, unsigned index); int isEmpty(List* list); bool deleteList(List* list); bool len(List* list, unsigned* res); +bool find(List* list, unsigned index, int* res); \ No newline at end of file From 9798927e2cd26bf69c452b90100ac4bbdcd5d586 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Wed, 26 Nov 2025 23:01:44 +0300 Subject: [PATCH 16/23] Fixed condition --- 06_lists/src/josephusProblem.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/06_lists/src/josephusProblem.c b/06_lists/src/josephusProblem.c index d6ad41a..da09218 100644 --- a/06_lists/src/josephusProblem.c +++ b/06_lists/src/josephusProblem.c @@ -1,8 +1,6 @@ #include "circularList.h" #include - - int main(void) { int n = 0, m = 0; @@ -19,7 +17,7 @@ int main(void) } int currentIndex = m - 1; - while (listLen > 2) { + while (listLen > 1) { if (!del(list, currentIndex)) { deleteList(list); return 1; From 9634190c8a2533eced505955db3905f684ff9533 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Tue, 2 Dec 2025 20:02:02 +0300 Subject: [PATCH 17/23] Updated .gitignore --- .gitignore | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.gitignore b/.gitignore index 9eef975..590185d 100644 --- a/.gitignore +++ b/.gitignore @@ -58,11 +58,6 @@ dkms.conf ### VisualStudioCode ### .vscode/* -!.vscode/settings.json -!.vscode/tasks.json -!.vscode/launch.json -!.vscode/extensions.json -!.vscode/*.code-snippets # Local History for Visual Studio Code .history/ From ea39643b12a1b844f2059d50e418f5cfbf25e31f Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Tue, 2 Dec 2025 20:19:01 +0300 Subject: [PATCH 18/23] Fixed codestyle --- 06_lists/src/circularList.c | 3 ++- 06_lists/src/list.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/06_lists/src/circularList.c b/06_lists/src/circularList.c index 04f8a0a..a7b4c25 100644 --- a/06_lists/src/circularList.c +++ b/06_lists/src/circularList.c @@ -1,7 +1,8 @@ #include "circularList.h" + +#include #include #include -#include typedef struct Node { int data; diff --git a/06_lists/src/list.c b/06_lists/src/list.c index 6f72207..821db69 100644 --- a/06_lists/src/list.c +++ b/06_lists/src/list.c @@ -1,7 +1,8 @@ #include "list.h" + +#include #include #include -#include typedef struct Node { int data; From 1f0fff6814852e1ed91c32df078b8e66e1ae2b0a Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Tue, 2 Dec 2025 21:51:57 +0300 Subject: [PATCH 19/23] Refactored sortedList --- 06_lists/src/sortedList.c | 40 ++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/06_lists/src/sortedList.c b/06_lists/src/sortedList.c index 2b05f7e..1fe8c0c 100644 --- a/06_lists/src/sortedList.c +++ b/06_lists/src/sortedList.c @@ -1,6 +1,34 @@ -#include "list.h" #include +#include "list.h" + +bool add(List* list, int val) +{ + unsigned index = 0; + if (indexOf(list, val, &index) != 2) { + insert(list, val, index); + return true; + } + + return false; +} + +bool rm(List* list, int val) +{ + unsigned index = -1; + int res = indexOf(list, val, &index); + + if (!res) { + pop(list, index, &val); + } else if (res == 2) { + if (index != 0) { + return false; + } + } + + return true; +} + int main(void) { int op = 0; @@ -20,16 +48,18 @@ int main(void) case 1: scanf("%d", &val); - if (indexOf(list, val, &index) != 2) { - insert(list, val, index); + if (!add(list, val)) { + deleteList(list); + return 1; } break; case 2: scanf("%d", &val); - if (!indexOf(list, val, &index)) { - pop(list, index, &val); + if (!rm(list, val)) { + deleteList(list); + return 1; } break; From 3deb7dc84cceadf7cfb8d410e76277d122c94a6c Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Tue, 2 Dec 2025 22:02:53 +0300 Subject: [PATCH 20/23] Refactored josephusProblem --- 06_lists/src/josephusProblem.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/06_lists/src/josephusProblem.c b/06_lists/src/josephusProblem.c index da09218..e7ae1d8 100644 --- a/06_lists/src/josephusProblem.c +++ b/06_lists/src/josephusProblem.c @@ -1,10 +1,11 @@ #include "circularList.h" #include -int main(void) +bool josephus(int n, int m, int* res) { - int n = 0, m = 0; - scanf("%d%d", &n, &m); + if (n <= 0 || m <= 0) { + return false; + } List* list = newList(); int listLen = n; @@ -12,27 +13,41 @@ int main(void) for (int i = 1; i <= n; i++) { if (!insert(list, i, i - 1)) { deleteList(list); - return 1; + return false; } } - int currentIndex = m - 1; + int currentIndex = (m - 1) % n; while (listLen > 1) { if (!del(list, currentIndex)) { deleteList(list); - return 1; + return false; } listLen--; currentIndex = (currentIndex + m - 1) % listLen; } - int res = -1; - if (!find(list, 0, &res)) { + int last = -1; + if (!find(list, 0, &last)) { deleteList(list); + return false; + } + + deleteList(list); + *res = last; + return true; +} + +int main(void) +{ + int n = 0, m = 0; + scanf("%d%d", &n, &m); + + int res = -1; + if (!josephus(n, m, &res)) { return 1; } printf("%d", res); - deleteList(list); return 0; } \ No newline at end of file From a8c118bca24052c2261cdbb90ca9e907541f16df Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 8 Dec 2025 02:26:30 +0300 Subject: [PATCH 21/23] Removed redundant condition --- 06_lists/src/sortedList.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/06_lists/src/sortedList.c b/06_lists/src/sortedList.c index 1fe8c0c..bcbdf04 100644 --- a/06_lists/src/sortedList.c +++ b/06_lists/src/sortedList.c @@ -21,9 +21,7 @@ bool rm(List* list, int val) if (!res) { pop(list, index, &val); } else if (res == 2) { - if (index != 0) { - return false; - } + return false; } return true; From bedc32a6425d1229e13a2cef3f768d81df234a98 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 8 Dec 2025 02:27:44 +0300 Subject: [PATCH 22/23] Added newline character in printf --- 06_lists/src/josephusProblem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/06_lists/src/josephusProblem.c b/06_lists/src/josephusProblem.c index e7ae1d8..71ecb24 100644 --- a/06_lists/src/josephusProblem.c +++ b/06_lists/src/josephusProblem.c @@ -48,6 +48,6 @@ int main(void) return 1; } - printf("%d", res); + printf("%d\n", res); return 0; } \ No newline at end of file From f268eaae72263741d9b7e12efac2d9e5b8945b69 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 8 Dec 2025 02:45:03 +0300 Subject: [PATCH 23/23] Added CMakeLists --- 06_lists/src/CMakeLists.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 06_lists/src/CMakeLists.txt diff --git a/06_lists/src/CMakeLists.txt b/06_lists/src/CMakeLists.txt new file mode 100644 index 0000000..a258f54 --- /dev/null +++ b/06_lists/src/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.10) +project(06_lists) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../bin) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib) + +add_library(List list.c) +add_library(circularList circularList.c) + +add_executable(sortedList sortedList.c) +target_link_libraries(sortedList List) + +add_executable(josephus josephusProblem.c) +target_link_libraries(josephus circularList) + +add_compile_options(-Wall -Wextra -pedantic) \ No newline at end of file