Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions StackAndQueue/Stack/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "stack.h"

#include <stdlib.h>
#include <stdio.h>

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;
}
20 changes: 20 additions & 0 deletions StackAndQueue/Stack/stack.h
Original file line number Diff line number Diff line change
@@ -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**);
54 changes: 54 additions & 0 deletions StackAndQueue/advanced_braces_balance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "Stack\stack.h"

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

bool checkBalance(char* string) {
Stack* stack = stackNew();
if (stack == NULL) {
return false;
}

for (int index = 0; index < strlen(string); ++index) {
const char character = string[index];

if (character == '{' || character == '[' || character == '(') {
char* value = malloc(sizeof(char));
if (value == NULL) {
puts("Memory allocation error");
stackFree(&stack);
return false;
}
*value = character;
stackPush(&stack, value);
}

else if (character == '}' || character == ']' || character == ')') {
if (stack -> value == NULL) {
stackFree(&stack);
return false;
}

char* topValue = (char*) stack -> value;
char topChar = (char)*topValue;

if ((character == '}' && topChar != '{') ||
(character == ']' && topChar != '[') ||
(character == ')' && topChar != '(')) {
stackFree(&stack);
return false;
}

free(stack->value);
stackPop(&stack);
}
}

bool balanced = (stack->value == NULL);

stackFree(&stack);

return balanced;
}