#include <stdio.h>
#include <stdlib.h>
struct node {
char data;
struct node * prev_top;
};
void push( struct node ** curr_top_ref , char data )
{
struct node
* temp_node
= ( struct node
* )malloc( sizeof( struct node
));
if( temp_node != NULL )
{
temp_node->data = data;
temp_node->prev_top = *curr_top_ref;
*curr_top_ref = temp_node;
}
}
void pop( struct node ** curr_top_ref )
{
struct node * top;
if( curr_top_ref != NULL )
{
top = *curr_top_ref;
*curr_top_ref = top->prev_top;
}
}
int isbracketbalanced( char expr[] )
{
struct node * curr_stack_top = NULL;
int i = 0;
while(expr[i])
{
if( expr[i]=='(' || expr[i] == '{' || expr[i] == '[' )
{
push( &curr_stack_top , expr[i] );
}
if( curr_stack_top == NULL )
{
return 0;
}
switch(expr[i])
{
case ')':
if( curr_stack_top-> data == '(' )
pop( &curr_stack_top);
else
return 0;
break;
case '}':
if( curr_stack_top-> data == '{' )
pop( &curr_stack_top);
else
return 0;
break;
case ']':
if( curr_stack_top-> data == '[' )
pop( &curr_stack_top);
else
return 0;
break;
}
i++;
}
if( curr_stack_top == NULL )
{
return 1;
}
else
{
return 0;
}
}
int main(void) {
// your code goes here
char test1[] = "{[]}";
if( isbracketbalanced(test1))
printf("brackets are balanced \n"); else
printf("brackets are not balanced \n");
return 0;
}