Prefix To Infix Conversion: Data Structure
Prefix To Infix Conversion: Data Structure
Prefix To Infix Conversion: Data Structure
com
Search
Data Structure
Linked List
Stack
Stack
Static stack
Dynamic Stack
Application of stack
Queues
Recursion
Searching
Sorting Algorithms
Algorithms
Tree
Interview Question
converted by W eb2PDFConvert.com
This algorithm is a non-tail recursive method.
1.The reversed input string is completely pushed into a stack.
prefixToInfix(stack)
2.IF stack is not empty
a. Temp -->pop the stack
b. IF temp is a operator
Write a opening parenthesis to output
prefixToInfix(stack)
Write temp to output
prefixToInfix(stack)
Write a closing parenthesis to output
c. ELSE IF temp is a space -->prefixToInfix(stack)
d. ELSE
Write temp to output
IF stack.top NOT EQUAL to space -->prefixToInfix(stack)
Expression Stack
*+a-bc/-de+-fgh NuLL
*+a-bc/-de+-fg "h"
"g"
*+a-bc/-de+-f
"h"
"f"
*+a-bc/-de+- "g"
"h"
"f - g"
*+a-bc/-de+
"h"
*+a-bc/-de "f-g+h"
"e"
*+a-bc/-d
"f-g+h"
"d"
*+a-bc/- "e"
"f-g+h"
"d - e"
converted by W eb2PDFConvert.com
*+a-bc/
"f-g+h"
*+a-bc "(d-e)/(f-g+h)"
"c"
*+a-b
"(d-e)/(f-g+h)"
"b"
*+a- "c"
"(d-e)/(f-g+h)"
"b-c"
*+a
"(d-e)/(f-g+h)"
"a"
*+ "b-c"
"(d-e)/(f-g+h)"
"a+b-c"
*
"(d-e)/(f-g+h)"
End "(a+b-c)*(d-e)/(f-g+h)"
Ans = (a+b-c)*(d-e)/(f-g+h)
pushr(char opr)
{
oprs[++topr]=opr;
}
char popr()
{
return(oprs[topr--]);
}
int empty(int t)
{
if( t == 0) return(1);
return(0);
}
converted by W eb2PDFConvert.com
}
void main()
{
char prfx[50],ch,str[50],opnd1[50],opnd2[50],opr[2];
int i=0,k=0,opndcnt=0;
clrscr();
printf("Give an Expression = ");
gets(prfx);
printf(" Given Prefix Expression : %s\n",prfx);
while( (ch=prfx[i++]) != '\0')
{
if(isalnum(ch))
{
str[0]=ch; str[1]='\0';
pushd(str); opndcnt++;
if(opndcnt >= 2)
{
strcpy(opnd2,popd());
strcpy(opnd1,popd());
strcpy(str,"(");
strcat(str,opnd1);
ch=popr();
opr[0]=ch;opr[1]='\0';
strcat(str,opr);
strcat(str,opnd2);
strcat(str,")");
pushd(str);
opndcnt-=1;
}
}
else
{
pushr(ch);
if(opndcnt==1)opndcnt=0; /* operator followed by single operand*/
}
}
if(!empty(topd))
{
strcpy(opnd2,popd());
strcpy(opnd1,popd());
strcpy(str,"(");
strcat(str,opnd1);
ch=popr();
opr[0]=ch;opr[1]='\0';
strcat(str,opr);
strcat(str,opnd2);
strcat(str,")");
pushd(str);
}
printf(" Infix Expression: ");
puts(opnds[topd]);
getch();
}
converted by W eb2PDFConvert.com
Next →
MCQ,Interview,Gk »
scanftree is optimized for learning, testing. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and
examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
converted by W eb2PDFConvert.com