From e43c97e4d667a2006aa384b35f1c79dc467e90d8 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Wed, 15 Oct 2025 17:30:50 +0300 Subject: [PATCH 1/2] Stack 1.0 --- 5/stack.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5/stack.h | 18 ++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 5/stack.c create mode 100644 5/stack.h diff --git a/5/stack.c b/5/stack.c new file mode 100644 index 0000000..eaae60e --- /dev/null +++ b/5/stack.c @@ -0,0 +1,46 @@ +#include "stack.h" + + +void init(Stack* stack) { + (*stack).top = NULL; + (*stack).size = 0; +} + +void push(Stack* stack, char value) { + Node* new_node = malloc(sizeof(Node)); + + (*new_node).value = value; + (*new_node).next = (*stack).top; + + (*stack).top = new_node; + (*stack).size++; +} + + + +Node* pop(Stack* stack) { + if ((*stack).top == NULL) { + return NULL; + } + + Node* out = (*stack).top; + (*stack).top = (*out).next; + + (*stack).size--; + + return out; +} + +void printStack(Stack* stack) { + if ((*stack).size == 0) { + printf("\n"); + return; + } + Node* current = (*stack).top; + do { + printf("%c ", (*current).value); + current = (*current).next; + } + while (current != NULL); + printf("\n"); +} diff --git a/5/stack.h b/5/stack.h new file mode 100644 index 0000000..b2d5857 --- /dev/null +++ b/5/stack.h @@ -0,0 +1,18 @@ +#include +#include + +typedef struct Node { + char value; + struct Node* next; +} Node; + +typedef struct Stack { + Node* top; + int size; +} Stack; + + +void init(Stack* stack); +void push(Stack* stack, char value); +Node* pop(Stack* stack); +void printStack(Stack* stack); From a081be5b874c0d3d1003c5ea013152a9a8a97f2a Mon Sep 17 00:00:00 2001 From: stuffacc Date: Wed, 15 Oct 2025 17:35:58 +0300 Subject: [PATCH 2/2] Advance balance --- 5/advanced_balance.c | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 5/advanced_balance.c diff --git a/5/advanced_balance.c b/5/advanced_balance.c new file mode 100644 index 0000000..b77fd5c --- /dev/null +++ b/5/advanced_balance.c @@ -0,0 +1,70 @@ +#include "stack.h" + +void main() { + char string[200] = {' '}; + + fgets(string, sizeof(string), stdin); + Stack stack; + init(&stack); + + int res = 1; + + for (int i = 0; i < 200; i++) { + if (string[i] == '\n') { + break; + } + else if ((string[i] == '(') || (string[i] == '[') || (string[i] == '{')) { + push(&stack, string[i]); + } + + else if (string[i] == ')') { + Node* pope = pop(&stack); + if (pope == NULL) { + res = 0; + break; + } + char popChar = (*pope).value; + if (popChar != '(') { + free(pope); + res = 0; + break; + } + } + + else if (string[i] == ']') { + Node* pope = pop(&stack); + if (pope == NULL) { + res = 0; + break; + } + char popChar = (*pope).value; + if (popChar != '[') { + free(pope); + res = 0; + break; + } + } + + else if (string[i] == '}') { + Node* pope = pop(&stack); + if (pope == NULL) { + res = 0; + break; + } + char popChar = (*pope).value; + if (popChar != '{') { + free(pope); + res = 0; + break; + } + } + } + + if (res == 0 || stack.size != 0) { + printf("Баланса нет\n"); + } + else { + printf("Баланс есть\n"); + } +} +