diff --git a/StackAndQueue/Stack/stack.c b/StackAndQueue/Stack/stack.c new file mode 100644 index 0000000..108392a --- /dev/null +++ b/StackAndQueue/Stack/stack.c @@ -0,0 +1,54 @@ +#include "stack.h" + +#include +#include + +Stack* stackNew() { + Stack* stack = malloc(sizeof(Stack)); + if (stack == NULL) { + puts("Memory allocation error"); + return NULL; + } + stack -> previous = NULL; + stack -> value = NULL; + return stack; +} + +void stackPush(Stack** stack, void* value) { + if (stack == NULL || *stack == NULL) { + puts("Incorrect arguments error"); + return; + } + Stack* stackTop = malloc(sizeof(Stack)); + if (stackTop == NULL) { + puts("Memory allocation error"); + return; + } + stackTop -> value = value; + stackTop -> previous = *stack; + *stack = stackTop; +} + +void stackPop(Stack** stack) { + if (stack == NULL || *stack == NULL) { + puts("Incorrect arguments error"); + return; + } + Stack* stackTop = (*stack) -> previous; + if (stackTop != NULL) { + free(*stack); + *stack = stackTop; + } +} + +void stackFree(Stack** stack) { + if (stack == NULL || *stack == NULL) { + puts("Incorrect arguments error"); + return; + } + while ((*stack)->previous != NULL) { + stackPop(stack); + } + free(*stack); + *stack = NULL; +} diff --git a/StackAndQueue/Stack/stack.h b/StackAndQueue/Stack/stack.h new file mode 100644 index 0000000..646ac0b --- /dev/null +++ b/StackAndQueue/Stack/stack.h @@ -0,0 +1,20 @@ +#pragma once + +// The StackStruct structure with the value and previous fields +// Defined as Stack +typedef struct StackStruct { + void * value; + struct StackStruct *previous; +} Stack; + +// Create a new stack +Stack* stackNew(); + +// Add a new element to the top of the stack +void stackPush(Stack**, void*); + +// Remove an element from the top of the stack +void stackPop(Stack**); + +// Free the stack +void stackFree(Stack**); diff --git a/StackAndQueue/sorting_station.c b/StackAndQueue/sorting_station.c new file mode 100644 index 0000000..4da7898 --- /dev/null +++ b/StackAndQueue/sorting_station.c @@ -0,0 +1,62 @@ +#include "Stack\stack.h" +#include +#include +#include +#include + +int getPriority(char operation) { + return (operation == '+' || operation == '-') ? 1 : (operation == '*' || operation == '/') ? 2 : 0; +} + +char* sortingStationAlgorithm(char* expression) { + expression = strdup(expression); + + Stack* stack = stackNew(); + + char* token = strtok(expression, " "); + + char* result = malloc(strlen(expression) * 2); + result[0] = '\0'; + + while (token != NULL) { + if (isdigit(token[0])) { + strcat(result, token); + strcat(result, " "); + } + else if (token[0] == '(') { + stackPush(&stack, strdup(token)); + } + else if (token[0] == ')') { + while (stack != NULL && stack -> value != NULL && + ((char*) stack -> value)[0] != '(') { + strcat(result, (char*) stack -> value); + strcat(result, " "); + stackPop(&stack); + } + if (stack && stack -> value) { + stackPop(&stack); + } + } + else if (token[0] == '*' || token[0] == '/' || token[0] == '+' || token[0] == '-') { + while (stack != NULL && stack -> value != NULL && + getPriority(((char*) stack -> value)[0]) >= getPriority(token[0])) { + strcat(result, (char*) stack -> value); + strcat(result, " "); + stackPop(&stack); + } + stackPush(&stack, strdup(token)); + } + token = strtok(NULL, " "); + } + + while (stack != NULL && stack -> value != NULL) { + strcat(result, (char*) stack -> value); + strcat(result, " "); + stackPop(&stack); + } + + free(expression); + stackFree(&stack); + + return result; +}