From 5dbfb049f3c56bf5e5827babc074851b958ab608 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Thu, 23 Oct 2025 21:41:57 +0300 Subject: [PATCH 01/12] Stack implementation --- 05_stack_and_queue/stack.c | 57 ++++++++++++++++++++++++++++++++++++++ 05_stack_and_queue/stack.h | 10 +++++++ 2 files changed, 67 insertions(+) create mode 100644 05_stack_and_queue/stack.c create mode 100644 05_stack_and_queue/stack.h diff --git a/05_stack_and_queue/stack.c b/05_stack_and_queue/stack.c new file mode 100644 index 0000000..4684929 --- /dev/null +++ b/05_stack_and_queue/stack.c @@ -0,0 +1,57 @@ +#include "stack.h" +#include +#include + +typedef struct StackNode +{ + char data; + StackNode* next; +} StackNode; + +typedef struct Stack +{ + StackNode *head; +} Stack; + +Stack* newStack(void) +{ + Stack* stack = calloc(1, sizeof(Stack)); + return stack; +} + +bool isEmpty(Stack* stack) +{ + return stack->head == NULL; +} + +void push(Stack* stack, char data) +{ + StackNode* element = (StackNode*)calloc(1, sizeof(StackNode)); + + element->data = data; + element->next = stack->head; + stack->head = element; +} + +char pop(Stack* stack) +{ + if (isEmpty(stack)) { + return '\0'; + } + + StackNode* oldNode = stack->head; + char data = oldNode->data; + stack->head = stack->head->next; + + free(oldNode); + + return data; +} + +void deleteStack(Stack* stack) +{ + while (!isEmpty(stack)) { + pop(stack); + } + free(stack); +} diff --git a/05_stack_and_queue/stack.h b/05_stack_and_queue/stack.h new file mode 100644 index 0000000..8bd9405 --- /dev/null +++ b/05_stack_and_queue/stack.h @@ -0,0 +1,10 @@ +#pragma once +#include + +typedef struct Stack Stack; + +Stack* newStack(void); +bool isEmpty(Stack*); +void push(Stack* stack, char data); +char pop(Stack* stack); +void deleteStack(Stack* stack); \ No newline at end of file From 484086e95c867f18a29fddfcde83cb1bc01a8c1c Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Thu, 23 Oct 2025 22:13:38 +0300 Subject: [PATCH 02/12] Fixed bug in StackNode definition --- 05_stack_and_queue/stack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/05_stack_and_queue/stack.c b/05_stack_and_queue/stack.c index 4684929..5456669 100644 --- a/05_stack_and_queue/stack.c +++ b/05_stack_and_queue/stack.c @@ -5,7 +5,7 @@ typedef struct StackNode { char data; - StackNode* next; + struct StackNode* next; } StackNode; typedef struct Stack From 5035101d24eb5410e2300e77bb80617e9567d977 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Thu, 23 Oct 2025 22:27:05 +0300 Subject: [PATCH 03/12] Brackets checker --- 05_stack_and_queue/bracketsChecker.c | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 05_stack_and_queue/bracketsChecker.c diff --git a/05_stack_and_queue/bracketsChecker.c b/05_stack_and_queue/bracketsChecker.c new file mode 100644 index 0000000..44b1db0 --- /dev/null +++ b/05_stack_and_queue/bracketsChecker.c @@ -0,0 +1,44 @@ +#include "stack.h" +#include +#include +#include +#include + +int bracketsChecker(char* str, bool* res) +{ + Stack* stack = newStack(); + for (unsigned long i = 0; i < strlen(str); i++) { + if ((str[i] == '(') || (str[i] == '[') || (str[i] == '{')) { + push(stack, str[i]); + } else if ((str[i] == ')') || (str[i] == ']') || (str[i] == '}')) { + if (!isEmpty(stack)) { + char popped = pop(stack); + if (((str[i] == ')') && (popped != '(')) || ((str[i] == ']') && (popped != '[')) || ((str[i] == '}') && (popped != '{'))) { + *res = false; + } + } else { + *res = false; + } + } + } + *res = *res && isEmpty(stack); + deleteStack(stack); + return 0; +} + + +int main(void) +{ + int n = 0; + scanf("%d\n", &n); + + char* input = calloc(n + 1, sizeof(char)); + fgets(input, n + 1, stdin); + + bool res = true; + bracketsChecker(input, &res); + + printf("%d\n", res); + free(input); + return 0; +} \ No newline at end of file From 6e97862ba388dbcb09a65caa290558f2bc39d510 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Thu, 23 Oct 2025 23:26:25 +0300 Subject: [PATCH 04/12] Added peek function in stack --- 05_stack_and_queue/stack.c | 9 +++++++++ 05_stack_and_queue/stack.h | 1 + 2 files changed, 10 insertions(+) diff --git a/05_stack_and_queue/stack.c b/05_stack_and_queue/stack.c index 5456669..602555e 100644 --- a/05_stack_and_queue/stack.c +++ b/05_stack_and_queue/stack.c @@ -48,6 +48,15 @@ char pop(Stack* stack) return data; } +char peek(Stack* stack) +{ + if (isEmpty(stack)) { + return '\0'; + } + + return stack->head->data; +} + void deleteStack(Stack* stack) { while (!isEmpty(stack)) { diff --git a/05_stack_and_queue/stack.h b/05_stack_and_queue/stack.h index 8bd9405..6d576ad 100644 --- a/05_stack_and_queue/stack.h +++ b/05_stack_and_queue/stack.h @@ -7,4 +7,5 @@ Stack* newStack(void); bool isEmpty(Stack*); void push(Stack* stack, char data); char pop(Stack* stack); +char peek(Stack* stack); void deleteStack(Stack* stack); \ No newline at end of file From 6b827511325a2c6e4faf20cc549a359b0e4227a0 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Thu, 23 Oct 2025 23:45:11 +0300 Subject: [PATCH 05/12] Shunting yard algorithm --- 05_stack_and_queue/sortStation.c | 104 +++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 05_stack_and_queue/sortStation.c diff --git a/05_stack_and_queue/sortStation.c b/05_stack_and_queue/sortStation.c new file mode 100644 index 0000000..f56b3a6 --- /dev/null +++ b/05_stack_and_queue/sortStation.c @@ -0,0 +1,104 @@ +#include "stack.h" +#include +#include +#include +#include + +int getPriority(char operation) +{ + if (operation == '*' || operation == '/') { + return 2; + } else if (operation == '+' || operation == '-') { + return 1; + } + return 0; +} + +int sortStation(char* str, char* res) +{ + unsigned lastUsedResIndex = 0; + Stack* stack = newStack(); + for (unsigned long i = 0; i < strlen(str); i++) { + if (isdigit(str[i])) { + if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { + res[lastUsedResIndex] = ' '; + lastUsedResIndex++; + } + res[lastUsedResIndex] = str[i]; + lastUsedResIndex++; + } else if ((str[i] == '+') || (str[i] == '-') || (str[i] == '*') || (str[i] == '/')) { + while (!isEmpty(stack)) { + char peeked = peek(stack); + if (peeked == '(') { + break; + } + if (getPriority(peeked) >= getPriority(str[i])) { + if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { + res[lastUsedResIndex] = ' '; + lastUsedResIndex++; + } + res[lastUsedResIndex] = pop(stack); + lastUsedResIndex++; + } else { + break; + } + } + push(stack, str[i]); + } else if (str[i] == '(') { + push(stack, str[i]); + } else if (str[i] == ')') { + char popped = pop(stack); + while (popped != '(') { + if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { + res[lastUsedResIndex] = ' '; + lastUsedResIndex++; + } + res[lastUsedResIndex] = popped; + lastUsedResIndex++; + + if (!isEmpty(stack)) { + popped = pop(stack); + } else { + deleteStack(stack); + return 1; + } + } + } + } + while (!isEmpty(stack)) { + char popped = pop(stack); + if (popped == '(') { + deleteStack(stack); + return 1; + } + if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { + res[lastUsedResIndex] = ' '; + lastUsedResIndex++; + } + res[lastUsedResIndex] = popped; + lastUsedResIndex++; + } + deleteStack(stack); + return 0; +} + +int main(void) { + int n = 0; + scanf("%d\n", &n); + + char* input = calloc(n + 1, sizeof(char)); + fgets(input, n + 1, stdin); + + char* res = calloc(n + 1, sizeof(char)); + res[n] = '\0'; + + if (sortStation(input, res) == 0) { + printf("%s\n", res); + } else { + printf("Входная строка некорректна"); + } + + free(input); + free(res); + return 0; +} \ No newline at end of file From 7f560c997f91a662ed0531d6db07f49799b3effc Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 27 Oct 2025 16:53:03 +0300 Subject: [PATCH 06/12] Fixed multi-digits numbers and brackets handling --- 05_stack_and_queue/sortStation.c | 33 +++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/05_stack_and_queue/sortStation.c b/05_stack_and_queue/sortStation.c index f56b3a6..420f03b 100644 --- a/05_stack_and_queue/sortStation.c +++ b/05_stack_and_queue/sortStation.c @@ -18,14 +18,21 @@ int sortStation(char* str, char* res) { unsigned lastUsedResIndex = 0; Stack* stack = newStack(); - for (unsigned long i = 0; i < strlen(str); i++) { + unsigned long len = strlen(str); + for (unsigned long i = 0; i < len; i++) { if (isdigit(str[i])) { if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { res[lastUsedResIndex] = ' '; lastUsedResIndex++; } - res[lastUsedResIndex] = str[i]; - lastUsedResIndex++; + while (i < len && isdigit(str[i])) { + res[lastUsedResIndex] = str[i]; + lastUsedResIndex++; + i++; + } + if (i < len) { + i--; + } } else if ((str[i] == '+') || (str[i] == '-') || (str[i] == '*') || (str[i] == '/')) { while (!isEmpty(stack)) { char peeked = peek(stack); @@ -47,22 +54,30 @@ int sortStation(char* str, char* res) } else if (str[i] == '(') { push(stack, str[i]); } else if (str[i] == ')') { - char popped = pop(stack); - while (popped != '(') { + char peeked = 0; + if (!isEmpty(stack)) { + peeked = peek(stack); + } else { + deleteStack(stack); + return 1; + } + while (peeked != '(') { if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { res[lastUsedResIndex] = ' '; lastUsedResIndex++; } - res[lastUsedResIndex] = popped; + res[lastUsedResIndex] = peeked; lastUsedResIndex++; + pop(stack); if (!isEmpty(stack)) { - popped = pop(stack); + peeked = peek(stack); } else { deleteStack(stack); return 1; } } + pop(stack); } } while (!isEmpty(stack)) { @@ -78,6 +93,7 @@ int sortStation(char* str, char* res) res[lastUsedResIndex] = popped; lastUsedResIndex++; } + res[lastUsedResIndex] = '\0'; deleteStack(stack); return 0; } @@ -90,7 +106,6 @@ int main(void) { fgets(input, n + 1, stdin); char* res = calloc(n + 1, sizeof(char)); - res[n] = '\0'; if (sortStation(input, res) == 0) { printf("%s\n", res); @@ -101,4 +116,4 @@ int main(void) { free(input); free(res); return 0; -} \ No newline at end of file +} From eb271f98b3331aefab03a3aebf3d51cc3e5003dd Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 27 Oct 2025 17:12:39 +0300 Subject: [PATCH 07/12] Changed check for an empty stack --- 05_stack_and_queue/sortStation.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/05_stack_and_queue/sortStation.c b/05_stack_and_queue/sortStation.c index 420f03b..57c5d73 100644 --- a/05_stack_and_queue/sortStation.c +++ b/05_stack_and_queue/sortStation.c @@ -55,12 +55,12 @@ int sortStation(char* str, char* res) push(stack, str[i]); } else if (str[i] == ')') { char peeked = 0; - if (!isEmpty(stack)) { - peeked = peek(stack); - } else { + if (isEmpty(stack)) { deleteStack(stack); return 1; + peeked = peek(stack); } + peeked = peek(stack); while (peeked != '(') { if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { res[lastUsedResIndex] = ' '; @@ -70,12 +70,12 @@ int sortStation(char* str, char* res) lastUsedResIndex++; pop(stack); - if (!isEmpty(stack)) { - peeked = peek(stack); - } else { + if (isEmpty(stack)) { deleteStack(stack); return 1; + peeked = peek(stack); } + peeked = peek(stack); } pop(stack); } From 79686d98c1132fa2ffb7b3941a3a17f1478b92d9 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 27 Oct 2025 17:20:55 +0300 Subject: [PATCH 08/12] Added src directory --- 05_stack_and_queue/{ => src}/bracketsChecker.c | 0 05_stack_and_queue/{ => src}/sortStation.c | 0 05_stack_and_queue/{ => src}/stack.c | 0 05_stack_and_queue/{ => src}/stack.h | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename 05_stack_and_queue/{ => src}/bracketsChecker.c (100%) rename 05_stack_and_queue/{ => src}/sortStation.c (100%) rename 05_stack_and_queue/{ => src}/stack.c (100%) rename 05_stack_and_queue/{ => src}/stack.h (100%) diff --git a/05_stack_and_queue/bracketsChecker.c b/05_stack_and_queue/src/bracketsChecker.c similarity index 100% rename from 05_stack_and_queue/bracketsChecker.c rename to 05_stack_and_queue/src/bracketsChecker.c diff --git a/05_stack_and_queue/sortStation.c b/05_stack_and_queue/src/sortStation.c similarity index 100% rename from 05_stack_and_queue/sortStation.c rename to 05_stack_and_queue/src/sortStation.c diff --git a/05_stack_and_queue/stack.c b/05_stack_and_queue/src/stack.c similarity index 100% rename from 05_stack_and_queue/stack.c rename to 05_stack_and_queue/src/stack.c diff --git a/05_stack_and_queue/stack.h b/05_stack_and_queue/src/stack.h similarity index 100% rename from 05_stack_and_queue/stack.h rename to 05_stack_and_queue/src/stack.h From f066f71916ff259513d79d51bec141d918c65f65 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Mon, 27 Oct 2025 17:31:28 +0300 Subject: [PATCH 09/12] Removed useless code --- 05_stack_and_queue/src/sortStation.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/05_stack_and_queue/src/sortStation.c b/05_stack_and_queue/src/sortStation.c index 57c5d73..ecb9269 100644 --- a/05_stack_and_queue/src/sortStation.c +++ b/05_stack_and_queue/src/sortStation.c @@ -58,7 +58,6 @@ int sortStation(char* str, char* res) if (isEmpty(stack)) { deleteStack(stack); return 1; - peeked = peek(stack); } peeked = peek(stack); while (peeked != '(') { @@ -73,7 +72,6 @@ int sortStation(char* str, char* res) if (isEmpty(stack)) { deleteStack(stack); return 1; - peeked = peek(stack); } peeked = peek(stack); } From 0dd22be11edfd59c72fadcb8c6fcb77e3923bcbb Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Sat, 1 Nov 2025 00:21:24 +0300 Subject: [PATCH 10/12] fixed codestyle --- 05_stack_and_queue/src/bracketsChecker.c | 1 - 05_stack_and_queue/src/sortStation.c | 7 ++++--- 05_stack_and_queue/src/stack.c | 8 +++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/05_stack_and_queue/src/bracketsChecker.c b/05_stack_and_queue/src/bracketsChecker.c index 44b1db0..8ec24df 100644 --- a/05_stack_and_queue/src/bracketsChecker.c +++ b/05_stack_and_queue/src/bracketsChecker.c @@ -26,7 +26,6 @@ int bracketsChecker(char* str, bool* res) return 0; } - int main(void) { int n = 0; diff --git a/05_stack_and_queue/src/sortStation.c b/05_stack_and_queue/src/sortStation.c index ecb9269..7491d05 100644 --- a/05_stack_and_queue/src/sortStation.c +++ b/05_stack_and_queue/src/sortStation.c @@ -4,7 +4,7 @@ #include #include -int getPriority(char operation) +int getPriority(char operation) { if (operation == '*' || operation == '/') { return 2; @@ -14,7 +14,7 @@ int getPriority(char operation) return 0; } -int sortStation(char* str, char* res) +int sortStation(char* str, char* res) { unsigned lastUsedResIndex = 0; Stack* stack = newStack(); @@ -96,7 +96,8 @@ int sortStation(char* str, char* res) return 0; } -int main(void) { +int main(void) +{ int n = 0; scanf("%d\n", &n); diff --git a/05_stack_and_queue/src/stack.c b/05_stack_and_queue/src/stack.c index 602555e..03c6cbf 100644 --- a/05_stack_and_queue/src/stack.c +++ b/05_stack_and_queue/src/stack.c @@ -2,15 +2,13 @@ #include #include -typedef struct StackNode -{ +typedef struct StackNode { char data; struct StackNode* next; } StackNode; -typedef struct Stack -{ - StackNode *head; +typedef struct Stack { + StackNode* head; } Stack; Stack* newStack(void) From 46d6e710211e8b670ef66ea14dfabaa3c2b8ebf7 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Wed, 3 Dec 2025 02:39:01 +0300 Subject: [PATCH 11/12] Fixed codestyle --- 05_stack_and_queue/src/bracketsChecker.c | 5 +++-- 05_stack_and_queue/src/sortStation.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/05_stack_and_queue/src/bracketsChecker.c b/05_stack_and_queue/src/bracketsChecker.c index 8ec24df..0fef5ec 100644 --- a/05_stack_and_queue/src/bracketsChecker.c +++ b/05_stack_and_queue/src/bracketsChecker.c @@ -1,8 +1,9 @@ #include "stack.h" -#include + #include -#include +#include #include +#include int bracketsChecker(char* str, bool* res) { diff --git a/05_stack_and_queue/src/sortStation.c b/05_stack_and_queue/src/sortStation.c index 7491d05..a423903 100644 --- a/05_stack_and_queue/src/sortStation.c +++ b/05_stack_and_queue/src/sortStation.c @@ -1,8 +1,9 @@ #include "stack.h" -#include -#include + #include +#include #include +#include int getPriority(char operation) { From cfdd7fd9f996c83d7a76c62af639649085bdc2f1 Mon Sep 17 00:00:00 2001 From: Nikolay Baboshin <231540704+shknoko@users.noreply.github.com> Date: Sun, 23 Nov 2025 23:04:09 +0300 Subject: [PATCH 12/12] Added CMakeLists --- 05_stack_and_queue/src/CMakeLists.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 05_stack_and_queue/src/CMakeLists.txt diff --git a/05_stack_and_queue/src/CMakeLists.txt b/05_stack_and_queue/src/CMakeLists.txt new file mode 100644 index 0000000..cabd90b --- /dev/null +++ b/05_stack_and_queue/src/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.10) +project(Stack_and_queue) + +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(Stack stack.c) + +add_executable(sortStation sortStation.c) +add_executable(bracketsChecker bracketsChecker.c) + +target_link_libraries(sortStation Stack) +target_link_libraries(bracketsChecker Stack) + +add_compile_options(-Wall -Wextra -pedantic)