# Application Of Stack Adt – Balancing Parenthesis Data Structure Algorithm

Stack Adt Balancing Parenthesis Algorithm it has header file Balance.h it contains stack structure , STACK create(),int isempty(STACK s),int isfull(STACK s),void push(char x, STACK s),void pop(STACK s),char check(STACK s),int top(STACK s) by using these function and data variable it will perform Balancing. Data structure Lab Source code Programming algorithm - CS1152 c/c++
“Balance.h” file Application of Stack Adt Balancing Parenthesis Data Structure Programming
#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 Application of Stack Adt Balancing Parenthesis Data Structure Algorithm
#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 Data structure Lab Source code Programming algorithm - CS1152 c/c++
Enter the Expression: {[(a+b)*(p-q)]/(x/y)}
Balanced Parenthesis
Enter the Expression: {[(a+b)*(p-q])/(x/y)}
Imbalanced Parenthesis