Skip to content

Comments

ДЗ №5#11

Open
shknoko wants to merge 11 commits intomasterfrom
05_stack_and_queue
Open

ДЗ №5#11
shknoko wants to merge 11 commits intomasterfrom
05_stack_and_queue

Conversation

@shknoko
Copy link
Owner

@shknoko shknoko commented Oct 23, 2025

  • Stack implementation
  • Fixed bug in StackNode definition
  • Brackets checker
  • Added peek function in stack
  • Shunting yard algorithm

@shknoko shknoko self-assigned this Oct 23, 2025
@shknoko shknoko requested a review from WoWaster October 23, 2025 20:51
Copy link

@georgiy-belyanin georgiy-belyanin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вроде Cool. 6/6 и 6/6.

Правда нет ветки отдельной, только со стеком -- ну да ладно, кажется, ее ни у кого нет.


void push(Stack* stack, char data)
{
StackNode* element = (StackNode*)calloc(1, sizeof(StackNode));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: конечно, calloc() может не сработать, это лучше бы учитывать и проверять, что вызов вернул не NULL.

Comment on lines +36 to +42
if (isEmpty(stack)) {
return '\0';
}

StackNode* oldNode = stack->head;
char data = oldNode->data;
stack->head = stack->head->next;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: нетривиальные предположения лучше помещать в assert()

Suggested change
if (isEmpty(stack)) {
return '\0';
}
StackNode* oldNode = stack->head;
char data = oldNode->data;
stack->head = stack->head->next;
if (isEmpty(stack)) {
return '\0';
}
assert(stack->head != NULL);
StackNode* oldNode = stack->head;
char data = oldNode->data;
stack->head = stack->head->next;

Comment on lines +58 to +64
void deleteStack(Stack* stack)
{
while (!isEmpty(stack)) {
pop(stack);
}
free(stack);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: обычно функции удаления хорошо делать устойчивыми к NULL входам.

Suggested change
void deleteStack(Stack* stack)
{
while (!isEmpty(stack)) {
pop(stack);
}
free(stack);
}
void deleteStack(Stack* stack)
{
if (stack == NULL) return;
while (!isEmpty(stack)) {
pop(stack);
}
free(stack);
}

}
*res = *res && isEmpty(stack);
deleteStack(stack);
return 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: немного странно, что получить не 0 нельзя. Хотя функция может привести к UB (например, если память закончилась).

Comment on lines +12 to +23
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;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Я бы попробовал тут switch, но, вообще говоря, код в любом случае будто бы немного дублируется. Так что просто вкусовщина.

fgets(input, n + 1, stdin);

bool res = true;
bracketsChecker(input, &res);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: а тут return code не проверяется.

#include <stdlib.h>
#include <string.h>

int getPriority(char operation)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: обычно precedence вместо priority

{
unsigned lastUsedResIndex = 0;
Stack* stack = newStack();
unsigned long len = strlen(str);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor:

Suggested change
unsigned long len = strlen(str);
size_t len = strlen(str);

Ну это совсем придирка, просто strlen() отдает size_t, наверняка есть платформы, где unsigned long будет 32 бита, а size_t --- 64.

Comment on lines +29 to +36
while (i < len && isdigit(str[i])) {
res[lastUsedResIndex] = str[i];
lastUsedResIndex++;
i++;
}
if (i < len) {
i--;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Есть у меня некоторые подозрения, что можно было написать вот так:

Suggested change
while (i < len && isdigit(str[i])) {
res[lastUsedResIndex] = str[i];
lastUsedResIndex++;
i++;
}
if (i < len) {
i--;
}
res[lastUsedResIndex] = str[i];
lastUsedResIndex++;

И поведение было бы такое же. Ну в смысле -- нет необходимости во вложенном цикле. Но это так -- с первого взгляда.

А то с этим сдвигом влево не совсем понятно выходит, что происходит.

Comment on lines +59 to +62
if (isEmpty(stack)) {
deleteStack(stack);
return 1;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: одно из немногих мест, когда я бы рассмотрел goto. Так легче "не забыть почистить память". Ну и строк меньше.

    if (isEmpty(stack)) goto error;
    
    // ...
    error:
    deleteStack(stack);
    return 1;

А вот в Go это бы сделал defer :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants