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
4 changes: 4 additions & 0 deletions src/stack_and_queue/stack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Compile inctruction for advanced brackets balance
gcc -Wall -Wextra -pedantic -O2 stack.c -c

gcc -Wall -Wextra -pedantic -O2 stack.o advanced_bracket_balance.c
79 changes: 79 additions & 0 deletions src/stack_and_queue/stack/advanced_bracket_balance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "stack.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

bool isBracket(char c)
{
return (c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}');
}

// Checks brackets balance.
// Returns true if brackets balanced and false if not.
// Takes string.
bool isBracketsBalanced(char* string)
{
int size = strlen(string);
Stack* brackets = newStack();

for (int i = 0; i < size; i++) {
// If char is opening bracket, pushes individual for all bracket types number in the stack.
// If char is closing bracket, pops number from the stack. If number is not right, returns false.
if (isBracket(string[i])) {
if (string[i] == '(') {
push(brackets, 1);
}
if (string[i] == '[') {
push(brackets, 2);
}
if (string[i] == '{') {
push(brackets, 3);
}
if (string[i] == ')') {
if (pop(brackets) != 1) {
deleteStack(brackets);
return false;
}
}
if (string[i] == ']') {
if (pop(brackets) != 2) {
deleteStack(brackets);
return false;
}
}
if (string[i] == '}') {
if (pop(brackets) != 3) {
deleteStack(brackets);
return false;
}
}
}
}
// Check for all brackets closed.
if (!isEmpty(brackets)) {
deleteStack(brackets);
return false;
}

deleteStack(brackets);
return true;
}

int main()
{
char a[] = "T(e){s}[t]";
char b[] = "123";
char c[] = "";
char d[] = "(";
char e[] = "({a)}";
char f[] = "((({{{[[[]]]}}})))";

printf("%s - %d\n", a, isBracketsBalanced(a));
printf("%s - %d\n", b, isBracketsBalanced(b));
printf("%s - %d\n", c, isBracketsBalanced(c));
printf("%s - %d\n", d, isBracketsBalanced(d));
printf("%s - %d\n", e, isBracketsBalanced(e));
printf("%s - %d\n", f, isBracketsBalanced(f));

return 0;
}
64 changes: 64 additions & 0 deletions src/stack_and_queue/stack/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "stack.h"

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

typedef struct StackNode {
int value;
struct StackNode* next;
} StackNode;

struct Stack {
StackNode* head;
};

Stack* newStack()
{
Stack* stack = malloc(sizeof(*stack));
stack->head = NULL;
return stack;
}

void deleteStack(Stack* stack)
{
while (stack->head != NULL) {
pop(stack);
}
free(stack);
}

void push(Stack* stack, int value)
{
StackNode* newNode = malloc(sizeof(*newNode));
newNode->value = value;
newNode->next = stack->head;
stack->head = newNode;
}

int pop(Stack* stack)
{
if (isEmpty(stack)) {
return -1;
}

StackNode* poppedNode = stack->head;
int value = poppedNode->value;
stack->head = poppedNode->next;
free(poppedNode);
return value;
}

int peek(Stack* stack)
{
if (isEmpty(stack)) {
return -1;
}

return stack->head->value;
}

bool isEmpty(Stack* stack)
{
return stack->head == NULL;
}
31 changes: 31 additions & 0 deletions src/stack_and_queue/stack/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <stdbool.h>

typedef struct Stack Stack;

// Creates new stack.
// Returns pointer on the created stack.
Stack* newStack();

// Deletes stack. Frees all memory used in it.
// Takes pointer on the stack.
void deleteStack(Stack* stack);

// Pushes new value on the top of the stack.
// Takes pointer on the stack and value for push.
void push(Stack* stack, int value);

// Deletes the top value of the stack and returns it. Frees memory used for top node.
// Takes pointer on the stack.
// If the stack is empty returns -1.
int pop(Stack* stack);

// Returns the top value of the stack.
// Takes pointer on the stack.
// If the stack is empty returns -1.
int peek(Stack* stack);

// Returns true if the stack is empty and false if not.
// Takes pointer on the stack.
bool isEmpty(Stack* stack);