30)Write a C program to parenthesized infix expression from user to check weather it is well parenthsized or not using Static implementation of stack.


#include<stdio.h>
#include<conio.h>
#define MAX 10
int top=-1;
int stack[MAX];
void push(char);
char pop();
void main()
{
char exp[MAX],tmp;
int i,flag=1;
clrscr();
printf(“\nENTER THE INFIX EXPR”);
gets(exp);
for(i=0;i<strlen(exp);i++)
{
if(exp[i]=='(‘ || exp[i]=='{‘||exp[i]=='[‘)
{
push(exp[i]);
}
if(exp[i]==’)’|| exp[i]==’}’||exp[i]==’]’)
{
if(top==-1)
{
flag=0;
}
else
{
tmp=pop();
if(exp[i]==’)’ && ( tmp=='{‘ || tmp=='[‘))
{
flag=0;
}
if(exp[i]==’}’ && ( tmp=='(‘ || tmp=='[‘))
{
flag=0;
}
if(exp[i]==’]’ && ( tmp=='(‘ || tmp=='{‘))
{
flag=0;
}
}
}
}
if(top>=0)
flag=0;
if(flag==1)
printf(“\nVALID EXPRE”);
else
printf(“\nINVALID EXPRE”);
getch();
}
void push(char c)
{
if(top==MAX-1)
{
printf(“\nSTACK IS FULL”);
}
else
{
top++;
stack[top]=c;
}
}
char pop()
{
char c;
c=stack[top];
top–;
return c;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s