From 345bc50da4bd95691dfc61981e264e969c33a881 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 20:23:21 +0300 Subject: [PATCH 01/23] Add stack library header file --- src/stack_and_queue/stack/stack.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/stack_and_queue/stack/stack.h diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h new file mode 100644 index 0000000..0822bbf --- /dev/null +++ b/src/stack_and_queue/stack/stack.h @@ -0,0 +1,30 @@ +#pragma once + +typedef struct StackNode { + int value; + struct StackNode* next; +} StackNode; + +typedef struct Stack { + StackNode* head; +} Stack; + +// Creates new stack. +// Returns pointer on the created stack. +Stack* newStack(); + +// Deletes stack. +// Takes pointer on the stack. +void deleteStack(Stack* stack); + +// Pushes new value on the top of the stack. +// Takes pointer on the stack and value for push. +void push(Stack* stack, int value); + +// Deletes the top value of the stack and returns it. +// Takes pointer on the stack. +int pop(Stack* stack); + +// Prints the top value of the stack. +// Takes pointer on the stack. +void peek(Stack* stack); From 4737d32eed5973489422ffb1074afc99a74a9abe Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 20:23:52 +0300 Subject: [PATCH 02/23] Add stack library realisation file --- src/stack_and_queue/stack/stack.c | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/stack_and_queue/stack/stack.c diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c new file mode 100644 index 0000000..e61ca28 --- /dev/null +++ b/src/stack_and_queue/stack/stack.c @@ -0,0 +1,54 @@ +#include "stack.h" + +#include +#include + +Stack* newStack() +{ + Stack* stack = malloc(sizeof(Stack)); + stack->head = NULL; + return stack; +} + +void deleteStack(Stack* stack) +{ + StackNode* highest = stack->head; + while (highest != NULL) { + StackNode* newHighest = highest->next; + free(highest); + highest = newHighest; + } + free(stack); +} + +void push(Stack* stack, int value) +{ + StackNode* newNode = malloc(sizeof(StackNode)); + newNode->value = value; + newNode->next = stack->head; + stack->head = newNode; +} + +int pop(Stack* stack) +{ + if (stack->head == NULL) { + printf("The stack is empty. \n"); + return -1; + } + + StackNode* poppedNode = stack->head; + int value = poppedNode->value; + stack->head = poppedNode->next; + free(poppedNode); + return value; +} + +void peek(Stack* stack) +{ + if (stack->head == NULL) { + printf("The stack is empty. \n"); + return; + } + + printf("The highest element: %d \n", stack->head->value); +} From 405bf4c1c824f79f37d9c5bb3d1867adb39a2e66 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 20:49:07 +0300 Subject: [PATCH 03/23] Update stack header file commentaries --- src/stack_and_queue/stack/stack.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index 0822bbf..cbbfc15 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -23,8 +23,10 @@ void push(Stack* stack, int value); // Deletes the top value of the stack and returns it. // Takes pointer on the stack. +// If the stack is empty returns -1. int pop(Stack* stack); // Prints the top value of the stack. // Takes pointer on the stack. +// Prints message if the stack is empty. void peek(Stack* stack); From ce89c997bf05d3bf5641b96e76c43c96e38cc9ad Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 20:49:36 +0300 Subject: [PATCH 04/23] Update stack realisation file --- src/stack_and_queue/stack/stack.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index e61ca28..c745db1 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -32,7 +32,6 @@ void push(Stack* stack, int value) int pop(Stack* stack) { if (stack->head == NULL) { - printf("The stack is empty. \n"); return -1; } From f4d1e691c16db183d8f85ce3a3c8b5f183c4a9ff Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 20 Oct 2025 14:59:09 +0300 Subject: [PATCH 05/23] Update stack realisation file --- src/stack_and_queue/stack/stack.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index c745db1..3b2b2c2 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -3,9 +3,18 @@ #include #include +typedef struct StackNode { + int value; + struct StackNode* next; +} StackNode; + +typedef struct Stack { + StackNode* head; +} Stack; + Stack* newStack() { - Stack* stack = malloc(sizeof(Stack)); + Stack* stack = malloc(sizeof(*stack)); stack->head = NULL; return stack; } @@ -23,7 +32,7 @@ void deleteStack(Stack* stack) void push(Stack* stack, int value) { - StackNode* newNode = malloc(sizeof(StackNode)); + StackNode* newNode = malloc(sizeof(*newNode)); newNode->value = value; newNode->next = stack->head; stack->head = newNode; @@ -42,12 +51,11 @@ int pop(Stack* stack) return value; } -void peek(Stack* stack) +int peek(Stack* stack) { if (stack->head == NULL) { - printf("The stack is empty. \n"); - return; + return -1; } - printf("The highest element: %d \n", stack->head->value); + return stack->head->value; } From d389c7db73352e9106299573aedd4817a7017bd2 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 20 Oct 2025 15:00:04 +0300 Subject: [PATCH 06/23] Update stack header file --- src/stack_and_queue/stack/stack.h | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index cbbfc15..b0a355f 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -1,13 +1,8 @@ #pragma once -typedef struct StackNode { - int value; - struct StackNode* next; -} StackNode; +typedef struct StackNode StackNode; -typedef struct Stack { - StackNode* head; -} Stack; +typedef struct Stack Stack; // Creates new stack. // Returns pointer on the created stack. @@ -26,7 +21,7 @@ void push(Stack* stack, int value); // If the stack is empty returns -1. int pop(Stack* stack); -// Prints the top value of the stack. +// Returns the top value of the stack. // Takes pointer on the stack. -// Prints message if the stack is empty. -void peek(Stack* stack); +// If the stack is empty returns -1. +int peek(Stack* stack); From ceeeb02df60f409dd711964c0f065f73ac0447b5 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 19:58:39 +0300 Subject: [PATCH 07/23] Update stack header file --- src/stack_and_queue/stack/stack.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index b0a355f..4439245 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -1,5 +1,7 @@ #pragma once +#include + typedef struct StackNode StackNode; typedef struct Stack Stack; @@ -8,7 +10,7 @@ typedef struct Stack Stack; // Returns pointer on the created stack. Stack* newStack(); -// Deletes stack. +// Deletes stack. Frees all memory used in it. // Takes pointer on the stack. void deleteStack(Stack* stack); @@ -16,7 +18,7 @@ void deleteStack(Stack* stack); // Takes pointer on the stack and value for push. void push(Stack* stack, int value); -// Deletes the top value of the stack and returns it. +// Deletes the top value of the stack and returns it. Frees memory used for top node. // Takes pointer on the stack. // If the stack is empty returns -1. int pop(Stack* stack); @@ -25,3 +27,7 @@ int pop(Stack* stack); // Takes pointer on the stack. // If the stack is empty returns -1. int peek(Stack* stack); + +// Returns true if the stack is empty and false if not. +// Takes pointer on the stack. +bool isEmpty(Stack* stack); From f344512d86c6205ed189fc76e409be20f046ad8d Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 19:59:13 +0300 Subject: [PATCH 08/23] Update stack realisation file --- src/stack_and_queue/stack/stack.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index 3b2b2c2..ff1989f 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -1,5 +1,6 @@ #include "stack.h" +#include #include #include @@ -21,11 +22,8 @@ Stack* newStack() void deleteStack(Stack* stack) { - StackNode* highest = stack->head; - while (highest != NULL) { - StackNode* newHighest = highest->next; - free(highest); - highest = newHighest; + while (stack->head != NULL) { + pop(stack); } free(stack); } @@ -40,7 +38,7 @@ void push(Stack* stack, int value) int pop(Stack* stack) { - if (stack->head == NULL) { + if (isEmpty(stack)) { return -1; } @@ -53,9 +51,14 @@ int pop(Stack* stack) int peek(Stack* stack) { - if (stack->head == NULL) { + if (isEmpty(stack)) { return -1; } return stack->head->value; } + +bool isEmpty(Stack* stack) +{ + return stack->head == NULL; +} From caa1bf95e1910f6be4efd5797263ddf2c071bbc5 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 20:13:52 +0300 Subject: [PATCH 09/23] Update stack header file --- src/stack_and_queue/stack/stack.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index 4439245..536c186 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -2,8 +2,6 @@ #include -typedef struct StackNode StackNode; - typedef struct Stack Stack; // Creates new stack. From 0d99e196eefce8fde90f718f2f5037ca77dc654d Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 20:14:07 +0300 Subject: [PATCH 10/23] Update stack realisation file --- src/stack_and_queue/stack/stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index ff1989f..7dfb49a 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -9,9 +9,9 @@ typedef struct StackNode { struct StackNode* next; } StackNode; -typedef struct Stack { +struct Stack { StackNode* head; -} Stack; +}; Stack* newStack() { From 7f111b6a933814c20330843e9efd0d0241831585 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 23:04:07 +0300 Subject: [PATCH 11/23] Add advanced bracket balance homework --- .../stack/advanced_bracket_balance.c | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/stack_and_queue/stack/advanced_bracket_balance.c diff --git a/src/stack_and_queue/stack/advanced_bracket_balance.c b/src/stack_and_queue/stack/advanced_bracket_balance.c new file mode 100644 index 0000000..86a3e65 --- /dev/null +++ b/src/stack_and_queue/stack/advanced_bracket_balance.c @@ -0,0 +1,80 @@ +#include "stack.h" +#include +#include + +bool isBracket(char c) +{ + if (c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}') { + return true; + } + return false; +} + +// Saves opening brackets in stack. Deletes pairs. +// Takes bracket symbol and pointer on the bracket stack. +// Returns true if brackets closes right or opens. Else returns false. +bool bracketProcessing(char bracket, Stack* bracketStack) +{ + if (bracket == '(') { + push(bracketStack, 1); + } + if (bracket == '[') { + push(bracketStack, 2); + } + if (bracket == '{') { + push(bracketStack, 3); + } + if (bracket == ')') { + if (pop(bracketStack) != 1) { + return false; + } + } + if (bracket == ']') { + if (pop(bracketStack) != 2) { + return false; + } + } + if (bracket == '}') { + if (pop(bracketStack) != 3) { + return false; + } + } + return true; +} + +int main() +{ + int size = 0; + printf("Input size of string: "); + scanf("%d", &size); + + Stack* bracketStack = newStack(); + char currentChar = 0; + // Костыль для избавления от символа переноса строки после ввода размера. + scanf("%c", ¤tChar); + bool isBalanced = true; + printf("Input string: "); + for (int i = 0; i < size; i++) { + scanf("%c", ¤tChar); + if (isBracket(currentChar)) { + isBalanced = bracketProcessing(currentChar, bracketStack); + if (!isBalanced) { + break; + } + } + } + // Проверка, что не осталось незакрытых скобок. + if (pop(bracketStack) != -1) { + isBalanced = false; + } + + if (isBalanced) { + printf("Brackets balanced. \n"); + } else { + printf("Brackets not balanced. \n"); + } + + deleteStack(bracketStack); + + return 0; +} From f70cccdd4bd1e4cf087d8a09b397c29b49cc2f9e Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 16 Oct 2025 14:48:44 +0300 Subject: [PATCH 12/23] Update advanced bracket balance homework --- .../stack/advanced_bracket_balance.c | 117 +++++++++--------- 1 file changed, 58 insertions(+), 59 deletions(-) diff --git a/src/stack_and_queue/stack/advanced_bracket_balance.c b/src/stack_and_queue/stack/advanced_bracket_balance.c index 86a3e65..77a2ce9 100644 --- a/src/stack_and_queue/stack/advanced_bracket_balance.c +++ b/src/stack_and_queue/stack/advanced_bracket_balance.c @@ -1,80 +1,79 @@ #include "stack.h" #include #include +#include bool isBracket(char c) { - if (c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}') { - return true; - } - return false; + return (c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}'); } -// Saves opening brackets in stack. Deletes pairs. -// Takes bracket symbol and pointer on the bracket stack. -// Returns true if brackets closes right or opens. Else returns false. -bool bracketProcessing(char bracket, Stack* bracketStack) +// Checks brackets balance. +// Returns true if brackets balanced and false if not. +// Takes string. +bool isBracketsBalanced(char* string) { - if (bracket == '(') { - push(bracketStack, 1); - } - if (bracket == '[') { - push(bracketStack, 2); - } - if (bracket == '{') { - push(bracketStack, 3); - } - if (bracket == ')') { - if (pop(bracketStack) != 1) { - return false; - } - } - if (bracket == ']') { - if (pop(bracketStack) != 2) { - return false; + int size = strlen(string); + Stack* brackets = newStack(); + + for (int i = 0; i < size; i++) { + // If char is opening bracket, pushes individual for all bracket types number in the stack. + // If char is closing bracket, pops number from the stack. If number is not right, returns false. + if (isBracket(string[i])) { + if (string[i] == '(') { + push(brackets, 1); + } + if (string[i] == '[') { + push(brackets, 2); + } + if (string[i] == '{') { + push(brackets, 3); + } + if (string[i] == ')') { + if (pop(brackets) != 1) { + deleteStack(brackets); + return false; + } + } + if (string[i] == ']') { + if (pop(brackets) != 2) { + deleteStack(brackets); + return false; + } + } + if (string[i] == '}') { + if (pop(brackets) != 3) { + deleteStack(brackets); + return false; + } + } } } - if (bracket == '}') { - if (pop(bracketStack) != 3) { - return false; - } + // Check for all brackets closed. + if (pop(brackets) != -1) { + deleteStack(brackets); + return false; } + + deleteStack(brackets); return true; } int main() { - int size = 0; - printf("Input size of string: "); - scanf("%d", &size); - - Stack* bracketStack = newStack(); - char currentChar = 0; - // Костыль для избавления от символа переноса строки после ввода размера. - scanf("%c", ¤tChar); - bool isBalanced = true; - printf("Input string: "); - for (int i = 0; i < size; i++) { - scanf("%c", ¤tChar); - if (isBracket(currentChar)) { - isBalanced = bracketProcessing(currentChar, bracketStack); - if (!isBalanced) { - break; - } - } - } - // Проверка, что не осталось незакрытых скобок. - if (pop(bracketStack) != -1) { - isBalanced = false; - } - - if (isBalanced) { - printf("Brackets balanced. \n"); - } else { - printf("Brackets not balanced. \n"); - } + char a[] = "T(e){s}[t]"; + char b[] = "123"; + char c[] = ""; + char d[] = "("; + char e[] = "({a)}"; + char f[] = "((({{{[[[]]]}}})))"; - deleteStack(bracketStack); + printf("%s - %d\n", a, isBracketsBalanced(a)); + printf("%s - %d\n", b, isBracketsBalanced(b)); + printf("%s - %d\n", c, isBracketsBalanced(c)); + printf("%s - %d\n", d, isBracketsBalanced(d)); + printf("%s - %d\n", e, isBracketsBalanced(e)); + printf("%s - %d\n", f, isBracketsBalanced(f)); return 0; } From a64578d6795a98eb4de8322b3991081c3343dcc9 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 20:09:03 +0300 Subject: [PATCH 13/23] Update bracket balance checker --- src/stack_and_queue/stack/advanced_bracket_balance.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stack_and_queue/stack/advanced_bracket_balance.c b/src/stack_and_queue/stack/advanced_bracket_balance.c index 77a2ce9..82eca38 100644 --- a/src/stack_and_queue/stack/advanced_bracket_balance.c +++ b/src/stack_and_queue/stack/advanced_bracket_balance.c @@ -50,7 +50,7 @@ bool isBracketsBalanced(char* string) } } // Check for all brackets closed. - if (pop(brackets) != -1) { + if (isEmpty(brackets)) { deleteStack(brackets); return false; } From 0af264a34539a7f302442aeb1b94586502565835 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 20:20:56 +0300 Subject: [PATCH 14/23] Update bracket balance checker --- src/stack_and_queue/stack/advanced_bracket_balance.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stack_and_queue/stack/advanced_bracket_balance.c b/src/stack_and_queue/stack/advanced_bracket_balance.c index 82eca38..4ea2006 100644 --- a/src/stack_and_queue/stack/advanced_bracket_balance.c +++ b/src/stack_and_queue/stack/advanced_bracket_balance.c @@ -50,7 +50,7 @@ bool isBracketsBalanced(char* string) } } // Check for all brackets closed. - if (isEmpty(brackets)) { + if (!isEmpty(brackets)) { deleteStack(brackets); return false; } From 41b1bd6736d82be3b3d4eafc24e93617ba2fe5c0 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Wed, 22 Oct 2025 20:51:42 +0300 Subject: [PATCH 15/23] Add compile instruction for advanced bracket balance --- src/stack_and_queue/stack/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/stack_and_queue/stack/README.md diff --git a/src/stack_and_queue/stack/README.md b/src/stack_and_queue/stack/README.md new file mode 100644 index 0000000..1ba0686 --- /dev/null +++ b/src/stack_and_queue/stack/README.md @@ -0,0 +1,4 @@ +## Compile inctruction for advanced brackets balance +gcc -Wall -Wextra -pedantic -O2 stack.c -c + +gcc -Wall -Wextra -pedantic -O2 stack.o advanced_bracket_balance.c From 3dea46b86f7a5ddac492b076929fe6723189ff59 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 9 Nov 2025 00:05:20 +0300 Subject: [PATCH 16/23] Update .gitignore with adding ignoring CMake build directory --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index b48d248..b1e499b 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,7 @@ Module.symvers Mkfile.old dkms.conf +# CMake +build + # End of https://www.toptal.com/developers/gitignore/api/c From d73f226079b7c651274ab1778cf1f5efad68254c Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 9 Nov 2025 00:06:26 +0300 Subject: [PATCH 17/23] Add CMakeLists.txt for build advanced bracket balance --- CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bfed53f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.25) +project(advanced_bracket_balance C) + +add_library(stack src/stack_and_queue/stack/stack.c) + +add_executable(advanced_bracket_balance src/stack_and_queue/stack/advanced_bracket_balance.c) +target_link_libraries(advanced_bracket_balance PRIVATE stack) +target_compile_options(advanced_bracket_balance PRIVATE -Wall -Wextra -pedantic) From ca828c10c04d088f420644ece4e7fa91addb3591 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 9 Nov 2025 00:06:58 +0300 Subject: [PATCH 18/23] Update instructions how to build --- src/stack_and_queue/stack/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/stack_and_queue/stack/README.md b/src/stack_and_queue/stack/README.md index 1ba0686..b35bd8e 100644 --- a/src/stack_and_queue/stack/README.md +++ b/src/stack_and_queue/stack/README.md @@ -1,4 +1,3 @@ ## Compile inctruction for advanced brackets balance -gcc -Wall -Wextra -pedantic -O2 stack.c -c - -gcc -Wall -Wextra -pedantic -O2 stack.o advanced_bracket_balance.c +cmake . -B build\ +cmake --build build From 479f1beda0e36128b2791710bdf6cccb47be291c Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 1 Dec 2025 14:55:36 +0300 Subject: [PATCH 19/23] Add build --- CMakeLists.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index bfed53f..d9c782e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,6 @@ cmake_minimum_required(VERSION 3.25) + +# Advanced bracket balance project(advanced_bracket_balance C) add_library(stack src/stack_and_queue/stack/stack.c) @@ -6,3 +8,18 @@ add_library(stack src/stack_and_queue/stack/stack.c) add_executable(advanced_bracket_balance src/stack_and_queue/stack/advanced_bracket_balance.c) target_link_libraries(advanced_bracket_balance PRIVATE stack) target_compile_options(advanced_bracket_balance PRIVATE -Wall -Wextra -pedantic) + +# Purely functional queue test +project(purely_functional_queue_test C) + +add_library(purely_functional_queue src/control_work_2/purely_functional_queue/purely_functional_queue.c) + +add_executable(purely_functional_queue_test src/control_work_2/purely_functional_queue/test.c) +target_link_libraries(purely_functional_queue_test PRIVATE purely_functional_queue) +target_compile_options(purely_functional_queue PRIVATE -Wall -Wextra -pedantic) + +# Comparsion +project(comparsion C) + +add_executable(comparsion src/control_work_2/comparsion_in_binary/comparsion_in_binary.c) +target_compile_options(comparsion PRIVATE -Wall -Wextra -pedantic) From 59c3af7fd19e9ccee6937af3de649bb173a12f45 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 1 Dec 2025 14:55:55 +0300 Subject: [PATCH 20/23] Add comparsion --- .../comparsion_in_binary.c | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/control_work_2/comparsion_in_binary/comparsion_in_binary.c diff --git a/src/control_work_2/comparsion_in_binary/comparsion_in_binary.c b/src/control_work_2/comparsion_in_binary/comparsion_in_binary.c new file mode 100644 index 0000000..26b66fe --- /dev/null +++ b/src/control_work_2/comparsion_in_binary/comparsion_in_binary.c @@ -0,0 +1,44 @@ +#include +#include +#include + +/* +Compares two numbers in binary form без ведущих нулей, исключая число 0. +If first is bigger, returns 1, if second - -1. +If they equvalent, returns 0. +*/ +int compare(bool* a, bool* b, int aLen, int bLen) +{ + if (aLen > bLen) { + return 1; + } + if (bLen > aLen) { + return -1; + } + for (int i = aLen - 1; i >= 0; --i) { + if (a[i] > b[i]) { + return 1; + } + if (b[i] > a[i]) { + return -1; + } + } + return 0; +} + +int main() +{ + bool a[] = { 1, 1, 0 }; + bool b[] = { 1, 0, 1 }; + assert(compare(a, b, 3, 3) == 1 && "Compare 1 incorrect"); + bool a2[] = { 0 }; + bool b2[] = { 1 }; + assert(compare(a2, b2, 1, 1) == -1 && "Compare 2 incorrect"); + bool a3[] = { 1 }; + bool b3[] = { 1 }; + assert(compare(a3, b3, 1, 1) == 0 && "Compare 3 incorrect"); + + printf("All test are correct.\n"); + + return 0; +} From 24f7e7c15ce03e4679164e33beb5c141724222a9 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 1 Dec 2025 14:56:23 +0300 Subject: [PATCH 21/23] Add header queue --- .../purely_functional_queue.h | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/control_work_2/purely_functional_queue/purely_functional_queue.h diff --git a/src/control_work_2/purely_functional_queue/purely_functional_queue.h b/src/control_work_2/purely_functional_queue/purely_functional_queue.h new file mode 100644 index 0000000..57f1fee --- /dev/null +++ b/src/control_work_2/purely_functional_queue/purely_functional_queue.h @@ -0,0 +1,24 @@ +#pragma once + +typedef struct Queue Queue; + +/* +Creates purely functional queue and returns pointer on it. +*/ +Queue* createQueue(); + +/* +Pushes value in the queue. +*/ +void enqueue(Queue* queue, int value); + +/* +Pops value from the queue. +If queue is empty, returns -1. +*/ +int dequeue(Queue* queue); + +/* +Creates deletes queue and frees memory. +*/ +void deleteQueue(Queue* queue); From f2547e7f68623327a64bac7f77af1decfb0b71f6 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 1 Dec 2025 14:56:40 +0300 Subject: [PATCH 22/23] Add realization queue --- .../purely_functional_queue.c | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/control_work_2/purely_functional_queue/purely_functional_queue.c diff --git a/src/control_work_2/purely_functional_queue/purely_functional_queue.c b/src/control_work_2/purely_functional_queue/purely_functional_queue.c new file mode 100644 index 0000000..c81bb16 --- /dev/null +++ b/src/control_work_2/purely_functional_queue/purely_functional_queue.c @@ -0,0 +1,77 @@ +#include "purely_functional_queue.h" + +#include +#include + +typedef struct ListElement { + int value; + struct ListElement* next; + struct ListElement* prev; +} ListElement; + +typedef struct Queue { + ListElement* fHead; + ListElement* rHead; +} Queue; + +void swapLists(Queue* queue) +{ + ListElement* currentElement = queue->rHead; + ListElement* previousElement = currentElement; + while (currentElement != NULL) { + ListElement* temp = currentElement->next; + currentElement->next = currentElement->prev; + currentElement->prev = temp; + + previousElement = currentElement; + currentElement = currentElement->next; + } + + queue->fHead = previousElement; + queue->rHead = NULL; +} + +Queue* createQueue() +{ + Queue* queue = malloc(sizeof(*queue)); + assert(queue != NULL && "Error! Memory is not allocated."); + queue->fHead = NULL; + queue->rHead = NULL; + return queue; +} + +void enqueue(Queue* queue, int value) +{ + ListElement* newElement = malloc(sizeof(*newElement)); + assert(newElement != NULL && "Error! Memory is not allocated."); + newElement->value = value; + newElement->next = NULL; + newElement->prev = queue->rHead; + queue->rHead = newElement; +} + +int dequeue(Queue* queue) +{ + if (queue->fHead == NULL) { + swapLists(queue); + } + if (queue->fHead == NULL) { + return -1; + } + + int value = queue->fHead->value; + ListElement* newHead = queue->fHead->prev; + free(queue->fHead); + queue->fHead = newHead; + queue->fHead->next = NULL; + + return value; +} + +void deleteQueue(Queue* queue) +{ + while (queue->fHead != NULL || queue->rHead != NULL) { + dequeue(queue); + } + free(queue); +} From 7cff046cbed3a71ab815e55a34fa6225820b96fd Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 1 Dec 2025 14:57:04 +0300 Subject: [PATCH 23/23] Add test for queue --- .../purely_functional_queue/test.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/control_work_2/purely_functional_queue/test.c diff --git a/src/control_work_2/purely_functional_queue/test.c b/src/control_work_2/purely_functional_queue/test.c new file mode 100644 index 0000000..ab32d40 --- /dev/null +++ b/src/control_work_2/purely_functional_queue/test.c @@ -0,0 +1,19 @@ +#include "purely_functional_queue.c" + +#include +#include + +int main() +{ + Queue* queue = createQueue(); + assert(queue != NULL && queue->fHead == NULL && queue->rHead == NULL && "Queue created incorrect."); + assert(dequeue(queue) == -1 && "Dequeue from empty queue works incorrect."); + enqueue(queue, 1); + assert(queue->rHead->value == 1 && "Enqueue works incorrect."); + assert(dequeue(queue) == 1 && "Dequeue works incorrect."); + enqueue(queue, 1); + enqueue(queue, 2); + assert(dequeue(queue) == 1 && "Dequeue with many elements works incorrect."); + assert(queue->fHead->value == 2 && "ListSwap works incorrect."); + deleteQueue(queue); +}