# include< stdio.h>
# include< stdlib.h>
struct link
{
char ch;
struct link *next;
};
struct link * pop2(struct link *rec)
{
char ch='a';
struct link *temp;
char x;
while((ch!='(')&&(rec!=NULL))
{
x=rec->ch;
if(x!='(')
printf("%c",x);
temp=rec->next;
free(rec);
rec=temp;
}
return rec;
}
struct link * pop(struct link *rec)
{
if(rec != NULL)
{
struct link *temp;
char x;
x=rec->ch;
if(x!='(')
printf("%c",x);
temp=rec->next;
free(rec);
rec=temp;
}
return rec;
}
struct link * pop1(char z,struct link *rec)
{
struct link *temp;
char x;
x=rec->ch;
if(((x=='+')||(x=='-'))&&((z=='*')||(z=='/')||(z=='%')))
{
}
else
{
if(x!='(')
printf("%c",x);
temp=rec->next;
free(rec);
rec=temp;
}
return rec;
}
struct link *push(char c,struct link *rec)
{
if(c==')')
{
rec=pop2(rec);
}
if((rec!=NULL)&&(c!='('))
{
rec=pop1(c,rec);
}
if(c!=')')
{
struct link *new1;
new1 = (struct link *)malloc(sizeof(struct link));
new1->ch=c;
new1->next = rec;
rec = new1;
}
return rec;
}
void main()
{
struct link *start ;
int i=0;
char ch='a';
clrscr();
start = NULL;
printf("\nEnter the expression:-");
while(ch!='\n')
{
ch=getchar();
if((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='(')||(ch=='%')||(ch==')'))
{
start=push(ch,start);
i++;
}
else
{
if(ch!='\n')
printf("%c",ch);
}
}
while(i>0)
{
start=pop(start);
i--;
}
getch();
}
No comments:
Post a Comment