Created
January 29, 2018 01:36
-
-
Save dluciano/16067092a9ee04640335281c93aea474 to your computer and use it in GitHub Desktop.
Verify balanced brackets, parenthesis and others.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdbool.h> | |
#define IGNORE 0 | |
#define OPEN 1 | |
#define CLOSE 2 | |
int isOpenChar(char c){ | |
switch(c){ | |
case '(': | |
case '{': | |
case '[': | |
case '<': | |
return OPEN; | |
case ')': | |
case '}': | |
case ']': | |
case '>': | |
return CLOSE; | |
} | |
return IGNORE; | |
} | |
int getWeight(char c){ | |
switch(c){ | |
case '(': | |
case ')': | |
return 1; | |
case '{': | |
case '}': | |
return 2; | |
case '[': | |
case ']': | |
return 3; | |
case '<': | |
case '>': | |
return 4; | |
} | |
return 0; | |
} | |
typedef struct Node_{ | |
int w; | |
struct Node_* next; | |
struct Node_* anterior; | |
} Node; | |
typedef struct Queue{ | |
Node* first; | |
Node* last; | |
}Queue; | |
void push(Queue* q, int w){ | |
Node* newNode = (struct Node_*) malloc(sizeof(struct Node_)); | |
newNode->w = w; | |
if(q->first == NULL){ | |
q->first = q->last = newNode; | |
} | |
else{ | |
newNode->anterior = q->last; | |
q->last->next = newNode; | |
q->last = newNode; | |
} | |
} | |
int pop(Queue* q){ | |
if(q->first == NULL || q->last == NULL){ | |
return -1; | |
} | |
Node* oldLast = q->last; | |
int w = oldLast->w; | |
q->last = oldLast->anterior; | |
if(q->last != NULL) | |
q->last->next = NULL; | |
if(q->last == NULL) | |
q->first = NULL; | |
free(oldLast); | |
return w; | |
} | |
void print(Queue* q){ | |
if(q == NULL || q->first == NULL) | |
return; | |
Node* n = q->first; | |
int c = 0; | |
while(n != NULL){ | |
printf("%i,", n->w); | |
n = n->next; | |
} | |
printf("\n"); | |
} | |
void clear(Queue* q){ | |
if(q == NULL || q->first == NULL) | |
return; | |
Node* n = q->first; | |
Node* aux = n; | |
while(n != NULL){ | |
aux = n->next; | |
free(n); | |
n = aux; | |
} | |
} | |
bool isEmpty(Queue* q){ | |
return q->first == NULL; | |
} | |
bool isBalanced(char* text){ | |
int length = strlen(text); | |
Queue* q = (Queue*) malloc(sizeof(Queue)); | |
q->first = NULL; | |
q->last = NULL; | |
bool balanced = true; | |
for(int i = 0; i < length; i++){ | |
char c = text[i]; | |
int type = isOpenChar(c); | |
int w = getWeight(c); | |
if(type == OPEN) | |
push(q, w); | |
else if(type == CLOSE && pop(q) != w){ | |
balanced = false; | |
break; | |
} | |
} | |
balanced = balanced ? isEmpty(q) : false; | |
clear(q); | |
free(q); | |
return balanced; | |
} | |
void test(char* t){ | |
bool balanced = isBalanced(t); | |
if(balanced) | |
printf("%s is balanced", t); | |
else | |
printf("%s is NOT balanced", t); | |
printf("\n"); | |
} | |
int main(void) { | |
test(""); | |
test("hjkh"); | |
test("["); | |
test("<"); | |
test("()"); | |
test("<>"); | |
test(")("); | |
test("><"); | |
test("[{}]"); | |
test("<()>"); | |
test("<(>"); | |
test("{<}>"); | |
test("t(t{t}t)t"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment