diff --git a/5.Stack-and-Quene/Stack/stack.c b/5.Stack-and-Quene/Stack/stack.c new file mode 100644 index 0000000..f23663f --- /dev/null +++ b/5.Stack-and-Quene/Stack/stack.c @@ -0,0 +1,74 @@ +#include +#include +#include "stack.h" + + +OBJ* stack_new(void) +{ + return NULL; +} + +OBJ* stack_push(OBJ* top, char data) +{ + OBJ* ptr = malloc(sizeof(OBJ)); + ptr->data = data; + ptr->next = top; + + return ptr; +} + +// Взять элемент со стека +OBJ* stack_pop(OBJ* top) +{ + if(top == NULL) + { + printf("Стек пуст\n"); + return top; + } + + OBJ* ptr_next = top->next; + free(top); + + return ptr_next; +} + +OBJ* stack_delete(OBJ* top) +{ + OBJ* current = top; + while (current != NULL) + { + OBJ* temp = current; + current = current->next; + free(temp); + } + + return 0; +} + +// Функция для просмотра элемента сверху +char stack_peek(const OBJ* top) +{ + if(top == NULL) + { + printf("Стек пуст\n"); + return -1; + } + + return top->data; +} + +void show(const OBJ* top) +{ + const OBJ* current = top; + if(top == NULL) + { + printf("Стек пуст\n"); + return; + } + + while(current != NULL) + { + printf("%d\n", current->data); + current = current->next; + } +} diff --git a/5.Stack-and-Quene/Stack/stack.h b/5.Stack-and-Quene/Stack/stack.h new file mode 100644 index 0000000..83434cc --- /dev/null +++ b/5.Stack-and-Quene/Stack/stack.h @@ -0,0 +1,28 @@ +#ifndef STACK_H +#define STACK_H + +typedef struct tag_obj +{ + char data; + struct tag_obj* next; +} OBJ; + +// Создание нового стека +OBJ* stack_new(void); + +// Добавление элемента на стек +OBJ* stack_push(OBJ* top, char data); + +// Удаление верхнего элемента +OBJ* stack_pop(OBJ* top); + +// Удаление стека +OBJ* stack_delete(OBJ* top); + +// Просмотр верхнего элемента +char stack_peek(const OBJ* top); + +// Вывод содержимого +void stack_show(OBJ* top); + +#endif \ No newline at end of file diff --git a/5.Stack-and-Quene/Task_2/Sorting-yard.c b/5.Stack-and-Quene/Task_2/Sorting-yard.c new file mode 100644 index 0000000..a027cee --- /dev/null +++ b/5.Stack-and-Quene/Task_2/Sorting-yard.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include "../Stack/stack.h" + + +int precedence(char op) +{ + switch(op) + { + case '+': + case '-': + return 1; + + case '*': + case '/': + return 2; + + default: + return 0; + } +} + +int is_operator(char c) +{ + return c == '+' || c == '-' || c == '*' || c == '/'; +} + +char* infix_to_postfix(const char* infix) +{ + static char output[1000]; + output[0] = '\0'; + + OBJ* stack = stack_new(); + int output_index = 0; + + for (int i = 0; infix[i] != '\0'; i++) + { + char token = infix[i]; + + if (isspace(token)) + { + continue; + } + + if (isdigit(token)) + { + while (isdigit(infix[i])) + { + output[output_index++] = infix[i++]; + } + + output[output_index++] = ' '; + i--; + } + + else if (token == '(') + { + stack = stack_push(stack, token); + } + + else if (token == ')') + { + while (stack != NULL && stack_peek(stack) != '(') + { + output[output_index++] = stack_peek(stack); + output[output_index++] = ' '; + stack = stack_pop(stack); + } + + if (stack != NULL && stack_peek(stack) == '(') + { + stack = stack_pop(stack); + } + } + + else if (is_operator(token)) + { + while (stack != NULL && stack_peek(stack) != '(' && + precedence(stack_peek(stack)) >= precedence(token)) + { + output[output_index++] = stack_peek(stack); + output[output_index++] = ' '; + stack = stack_pop(stack); + } + + stack = stack_push(stack, token); + } + } + + while (stack != NULL) + { + output[output_index++] = stack_peek(stack); + output[output_index++] = ' '; + stack = stack_pop(stack); + } + + if (output_index > 0 && output[output_index-1] == ' ') + { + output_index--; + } + + output[output_index] = '\0'; + stack_delete(stack); + return output; +} + +int main() { + char infix[1000]; + + printf("Введите инфиксное выражение: "); + scanf("%999[^\n]", infix); + + char* postfix = infix_to_postfix(infix); + printf("Постфиксная запись: %s\n", postfix); + + return 0; +} \ No newline at end of file diff --git a/Stack/stack.c b/Stack/stack.c new file mode 100644 index 0000000..51cb4a7 --- /dev/null +++ b/Stack/stack.c @@ -0,0 +1,80 @@ +#include +#include +#include "stack.h" + + +typedef struct tag_obj +{ + int data; + struct tag_obj* next; +} OBJ; + +OBJ* stack_new(void) +{ + return NULL; +} + +OBJ* stack_push(OBJ* top, int data) +{ + OBJ* ptr = malloc(sizeof(OBJ)); + ptr->data = data; + ptr->next = top; + + return ptr; +} + +// Взять элемент со стека +OBJ* stack_pop(OBJ* top) +{ + if(top == NULL) + { + printf("Стек пуст\n"); + return top; + } + + OBJ* ptr_next = top->next; + free(top); + + return ptr_next; +} + +OBJ* stack_delete(OBJ* top) +{ + OBJ* current = top; + while (current != NULL) + { + OBJ* temp = current; + current = current->next; + free(temp); + } + + return 0; +} + +// Функция для просмотра элемента сверху +int stack_peek(const OBJ* top) +{ + if(top == NULL) + { + printf("Стек пуст\n"); + return -1; + } + + return top->data; +} + +void show(const OBJ* top) +{ + const OBJ* current = top; + if(top == NULL) + { + printf("Стек пуст\n"); + return; + } + + while(current != NULL) + { + printf("%d\n", current->data); + current = current->next; + } +} diff --git a/Stack/stack.h b/Stack/stack.h new file mode 100644 index 0000000..ee80b5f --- /dev/null +++ b/Stack/stack.h @@ -0,0 +1,28 @@ +#ifdef STACK_H +#define STACK_H + +typedef struct tag_obj +{ + int data; + struct tag_obj* next; +} OBJ; + +// Создание нового стека +OBJ* stack_new(void); + +// Добавление элемента на стек +OBJ* stack_push(OBJ* top, int data); + +// Удаление верхнего элемента +OBJ* stack_pop(OBJ* top); + +// Удаление стека +OBJ* stack_delete(OBJ* top); + +// Просмотр верхнего элемента +int stack_peek(OBJ* top); + +// Вывод содержимого +void stack_show(OBJ* top); + +#endif \ No newline at end of file