SOURCE CODE:
“Balance.h” file
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
#define maxvalue 20
struct stack
{
int capacity,size,top;
char *array;
};
typedef struct stack *STACK;
STACK create()
{
STACK s;
s=(struct stack*)malloc(sizeof(struct stack));
s->capacity=maxvalue;
s->top=-1;
s->size=0;
s->array=(char*)malloc(sizeof(char)*maxvalue);
return s;
}
int isempty(STACK s)
{
return s->top==-1;
}
int isfull(STACK s)
{
return s->size=s->capacity;
}
void push(char x, STACK s)
{
if(isfull(s))
printf("\nSTACK NOT CREATED");
else
{
s->top++;
s->array[s->top]=x;
s->size++;
}
}
void pop(STACK s)
{
if(isempty(s))
printf("\n STACK NOT CREATED");
else
s->top--;
s->size--;
}
char check(STACK s)
{
if(s==NULL)
printf("\n STACK NOT CREATED");
return s->array[s->top] ;
}
int top(STACK s)
{
if(s==NULL)
printf("\n STACK NOT CREATED");
return s->top;
}
“Balance.c” file
#include"balance.h"
#include<string.h>
void main()
{
STACK s;
char e[20];
int i,len,tp;
clrscr();
printf("\n Enter The Expression");
scanf("%s",e);
len=strlen(e);
create();
for(i=0;i<len;i++)
{
if((e[i]=='(')||(e[i]=='[')||(e[i]=='{'))
push(e[i],s);
else if(e[i]==')')
{
if(check(s)=='(')
pop(s);
}
else if(e[i]=='}')
{
if('{'==check(s))
pop(s);
}
else if(e[i]==']')
{
if('['==check(s))
pop(s);
}
}
tp=top(s);
if(tp==0)
printf("\n Balanced Parenthesis");
else
printf("\n Imbalanced Parenthesis");
getch();
}
OUTPUT:
Enter the Expression: {[(a+b)*(p-q)]/(x/y)}
Balanced Parenthesis
Enter the Expression: {[(a+b)*(p-q])/(x/y)}
Imbalanced Parenthesis
EmoticonEmoticon