answersLogoWhite

0

Algorithm for converting prefix to postfix using stack?

Updated: 8/10/2023
User Avatar

Nabaneetakar

Lvl 1
12y ago

Best Answer

/*Infix to Prefix And Postfix*/

/*Assignment:5*/

/*Roll No:2102*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 15

#define true 1

#define false 0

/*Structure Decvlaration*/

typedef struct

{

char data[MAX];

char top;

}STK;

/*Function Declarations*/

void input(char str[]);

void intopre(char str1[],char pre[]);

void intopost(char str1[],char post[]);

int isoperand(char sym);

int prcd(char sym);

void push(STK *s1,char elem);

int pop(STK *s1);

int empty(STK *s2);

int full(STK *s2);

void dis(char str[]);

void main()

{

STK s;

int cs,ans;

char str[MAX],pre[MAX],post[MAX];

clrscr();

do /*Using Do-while Loop*/

{

clrscr();

printf(" -----Program for Expressions-----");

printf(" Input The String:");

printf(" MENU: ");

printf("1.Infix to Prefix ");

printf("2.Infix to Postfix");

printf(" 3.Exit");

cs=getche();

switch(cs) /*Using Switch Case*/

{

case 1:

intopre(str,pre);

break;

case 2:

intopost(str,post);

break;

case 3:

break;

default:

printf(" Enter a Valid Choise!"); /*Default Case*/

break;

}

printf(" Do you wish to Continue?(y/n)");

ans=getche();

}while(ans=='y'ans=='Y'); /*Condition for Do-while loop*/

getch();

}

/**************************************************/

/*To Input String*/

/**************************************************/

void input(char str)

{

printf("Enter the Infix String:");

scanf("%s",str);

}

/**************************************************/

/*To Covert Infix To Prefix*/

/**************************************************/

void intopre(STK s1,char str1[],char pre[])

{

int len,flag;

len=strlen(str1);

int check=0,cnt=len-1,pos=0;

char elem;

while(cnt>=0) /*while condition*/

{

flag=0;

if(isoperand(str1[cnt])) /*Checking for Operand*/

{

printf("%c",str1[cnt]);

cnt--;

pos++;

}

else

{

check=prcd(str1[cnt]);

while(check==false)

{

pre[pos]=str1[cnt];

flag=1;

pos++;

cnt--;

}

if(flag==0)

{

elem=pop(&s1);

printf("%c",elem);

}

}

}

}

/**************************************************/

/*To Convert Infix To Postfix*/

/**************************************************/

void intopost(STK s1,char str1[],char post[])

{

int len;

len=strlen(str1);

int check=0,cnt=len-1,pos=0;

}

/**************************************************/

/*To Check For Operand*/

/**************************************************/

int isoperand(char sym)

{

if('A'<sym<'Z''a'<sym<'z')

return(true);

return(false);

}

/**************************************************/

/*To Check The Precedence*/

/**************************************************/

int prcd(char sym)

{

}

/**************************************************/

/*To Display String*/

/**************************************************/

void dis(char str[])

{

}

/******************************************/

/*Push Function Definition*/

/******************************************/

void push(STK *s1,char elem)

{

if(!full(s1))

{

s1->top++; /*Incrementing top*/

s1->data[s1->top]=elem; /*Storing element*/

}

else

printf("

Stack is Full!");

}

/******************************************/

/*Full Function Definition*/

/******************************************/

int full(STK *s2)

{

if(s2->top==MAX) /*Condition for Full*/

return(true);

return(false);

}

/******************************************/

/*Pop Function Definition*/

/******************************************/

int pop(STK *s1)

{

char elem;

if(!empty(s1))

{

elem=s1->data[s1->top]; /*Storing top stack element in elem*/

s1->top--; /*Decrementing top*/

return(elem);

}

return(false);

}

/******************************************/

/*Empty Function Definition*/

/******************************************/

int empty(STK *s2)

{

if(s2->top==-1) /*Condition For Empty*/

return(true);

return(false);

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 20

char stack[MAX];

int top = -1;

char pop();

void push(char item);

int prcd(char symbol) {

switch(symbol) {

case '+':

case '-':

return 2;

case '*':

case '/':

return 4;

case '^':

case '$':

return 6;

case '(':

case ')':

case '#':

return 1;

}

}

int isoperator(char symbol) {

switch(symbol) {

case '+':

case '-':

case '*':

case '/':

case '^':

case '$':

case '(':

case ')':

return 1;

default:

return 0;

}

}

void convertip(char infix[],char prefix[]) {

int i,symbol,j=0;

char test[MAX];

infix=strrev(infix);

stack[++top]='#';

for(i=0;i<strlen(infix);i++) {

symbol=infix[i];

if(isoperator(symbol)==0) {

prefix[j]=symbol;

j++;

}else {

if(symbol==')') {

push(symbol);

}else if(symbol=='(') {

while(stack[top]!=')') {

prefix[j]=pop();

j++;

}

pop();//pop out (.

}else {

if(prcd(symbol)>prcd(stack[top])) {

push(symbol);

}else {

while(prcd(symbol)<=prcd(stack[top])) {

prefix[j]=pop();

j++;

}

push(symbol);

}//end of else.

}//end of else.

}//end of else.

}//end of for.

while(stack[top]!='#') {

prefix[j]=pop();

j++;

}

prefix[j]='\0';//null terminate string.

prefix=strrev(prefix);

}

int main() {

char infix[20],prefix[20];

clrscr();

printf("Enter the valid infix string:\n");

gets(infix);

convertip(infix,prefix);

printf("The corresponding prefix string is:\n");

puts(prefix);

getch();

return 0;

}

void push(char item) {

top++;

stack[top]=item;

}

char pop() {

char a;

a=stack[top];

top--;

return a;

}

//This Program is Perfectly Working - NO!

Its not perfect without bracketing the infix form:

Take the infix expression :

1-2+3-1 , which equals one.

The code is giving :

-1+2-31 prefix form, which is incorrect, because converted back:

-1 +2 (3-1) -> -1 +2 2 -> -1 (2+2) -> -1 4 -> 1-4 -> -3

-3 is not 1

the problem arrises when multiple equal priority operators are used.

Correct me if I am wrong, but this stack solution is circling around the net.

The program works if given the infix form as ((1-2)+3)-1

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<string.h>

void push(char item[],int *top,char s[][20])

{

*top=*top+1;

strcpy(s[*top],item);

}

void *pop(int *top,char s[][20])

{

char *item;

item=s[*top];

*top=*top-1;

return item;

}

void pre_post(char prefix[],char postfix[])

{

char s[20][20];

int top,i;

char symbol,temp[2];

char *op1,*op2;

top=-1;

strrev(prefix);

for(i=0;i<strlen(prefix);i++)

{

symbol=prefix[i];

temp[0]=symbol;

temp[1]='\0';

switch (symbol)

{

case '+':

case '-':

case '*':

case '/':

case '^':

op1=pop(&top,s);

op2=pop(&top,s);

strcpy(postfix,op1);

strcat(postfix,op2);

strcat(postfix,temp);

push(postfix,&top,s);

break;

default:

push(temp,&top,s);

}

}

}

void main()

{

char prefix[20];

char postfix[20];

printf("\n\n Enter the prefix expression \n\n");

scanf("%s",prefix);

pre_post(prefix,postfix);

printf("\n\n The postfix expression is %s \n\n",postfix);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include
#include
#include

#define oper(x) (x=='+' x=='-' x=='*' x=='/')

char in[30], post[30], stack[30];
int top=-1;

void push(char x)
{
stack[++top]=x;
}

char pop()
{
return stack[top--];
}

int precedence(char c)
{
if (c=='+' c=='-')
return 1;
if (c=='*' c=='/')
return 2;
if (c=='(')
return 3;
}

main()
{
char c;
int l,i,j=0,st1[20],k,h,f,eval,s,N;
printf("Enter the infix expression : ");
scanf("%s",&in);
l=strlen(in);
for(i=0;i<=l;i++)
{
if(oper(in[i]))
{
post[j++]=' ';
while(precedence(in[i]){
post[j++]=stack[top];
pop();
post[j++]=' ';

}
push(in[i]);
}
else if(in[i]=='\0')
{
while(top!=-1)
{
post[j++]=' ';
post[j++]=stack[top];
pop();
}
}
else
post[j++]=in[i];
}
post[j]='\0';
printf("Postfix Expression : %s\n",post);
i=0;top=-1;f=0;k=0;
while(i{
if(oper(post[i]))
{
f=1;
c=post[i];
eval=0;
switch(c)
{
case '+':
eval=st1[top-1]+st1[top];
break;
case '-':
eval=st1[top-1]-st1[top];
break;
case '*':
eval=st1[top-1]*st1[top];
break;
case '/':
eval=st1[top-1]/st1[top];
break;
}
top--;
st1[top]=eval;
}
else if(post[i]==' ')
{
if(f==0)
{
h=i-k;
s=0;
while(post[h]!=' ')
{
N=(int)post[h];
N=N-48;
s=s+N*(pow(10,(k-1)));
k--;
h++;
}
st1[++top]=s;
}
k=0;
}
else
{
k++;
f=0;
}
i++;
}
printf("Value : %d\n",st1[top]);
}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

public static boolean isOperand(String s)

{

double a=0;

try

{

a=Double.parseDouble(s);

}

catch (Exception ignore)

{

return false;

}

return true;

}

public static boolean isOperator(String s)

{

String operatorList="+-*/";

return operatorList.indexOf(s)>=0;

}

public static int precedence(String operator)

{

int precedence=0;

if (operator.equals("+"))

{

precedence=1;

}

else if (operator.equals("-"))

{

precedence=1;

}

else if (operator.equals("*"))

{

precedence=2;

}

else if (operator.equals("/"))

{

precedence=2;

}

return precedence;

}

public static String convert(String infix)

{

java.util.Stack<String> stack=new java.util.Stack<String>();

String postfix="";

String space=" ";

java.util.StringTokenizer st=new java.util.StringTokenizer(infix);

while (st.hasMoreTokens())

{

String token=st.nextToken();

if (isOperand(token))

{

postfix += token + space;

}

else if (token.equals("("))

{

stack.push(token);

}

else if (isOperator(token))

{

while (!stack.empty() && precedence(stack.peek())>=precedence(token))

{

postfix += stack.pop() + space;

}

stack.push(token);

}

else if (token.equals(")"))

{

while (!stack.peek().equals("("))

{

postfix += stack.pop() + space;

}

stack.pop(); // pop the left parenthesis

}

}

while (!stack.empty())

{

postfix += stack.pop() + space;

}

return postfix;

}

public static void main(String[] args) throws Exception

{

String infix="( 6 + 2 ) * 5 - 8 / 4"; // change this line as needed

String postfix=convert(infix);

System.out.println("infix="+infix);

System.out.println("postfix="+postfix);

System.out.println("evaluation="+PostfixEvaluator.evaluate(postfix));

}

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The logic is use a switch statement and capture plus, minus, multiplication, division and default if any pass to the postfix function in the java code.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Polish and Reverse Polish notations

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Algorithm for converting prefix to postfix using stack?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you convert postfix expression to prefix expression using c plus plus?

Let Q be an arithmetic expression written in infix notation. Besides operands and operators, Q may also contain left and right parentheses. We assume that the operators in Q consist only of exponentiations (&uarr;), multiplications (*), divisions (/), additions (+) and subtractions (-), and that they have the usual three levels of precedence as given above. We also assume that operators on the same level, including exponentiations, are performed from left to right unless otherwise indicated by parentheses. (This is not standard, since expressions may contain unary operators and some languages perform the exponentiations from right to left. However, these assumptions simplify our algorithm.) The following algorithm transforms the infix expression Q into its equivalent postfix expression P. The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using the operands from Q and the operators, which are removed from STACK. We begin by pushing a left parenthesis onto STACK and adding a right parenthesis at the end of Q. The algorithm is completed when STACK is empty. 69 Algorithm: POLISH(Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P. 1. Push "(" onto STACK, and add ")" to the end of Q. 2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the STACK is empty: 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator &otimes; is encountered, then: (a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) which has the same precedence as or higher precedence than &otimes;. (b) Add &otimes; to STACK. [End of If structure.] 6. If a right parenthesis is encountered, then: (a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add the left parenthesis to P.] [End of If structure.] [End of Step 2 loop.] 7. Exit. The terminology sometimes used for Step 5 is that &otimes; will "sink" to its own level. . 70 EXAMPLE Consider the following arithmetic infix expression Q: Q: A+(B*C- (D/E&uarr;F)*G)*H We simulate the previous algorithm to transform Q into its equivalent postfix expression P. First we push "(" onto STACK, and then we add ")" to the end of Q to obtain: A + ( B * C - ( D / E &uarr; F ) * G ) * H ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13)(14) (15) (16)(17) (18)(19) (20) The elements of Q have now been labeled from left to right for easy reference. Table below shows the status of STACK and of the string P as each element of Q is scanned. Observe that (1) Each operand is simply added to P and does not change STACK. (2) The subtraction operator (-) in row 7 sends * from STACK to P before it (-) is pushed onto STACK. (3) The right parenthesis in row 14 sends j and then I from STACK to P, and then removes the left parenthesis from the top of STACK. (4) The right parenthesis in row 20 sends * and then + from STACK to P, and then removes-the left parenthesis from the top of STACK. After Step 20 is executed, the STACK is empty and P: A B C * D E F &uarr; / G * - H * + 71 which is the required postfix equivalent of Q.


Infix to postfix C?

Infix Expression :Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.Postfix Expression :The Postfix(Postorder) form of the above expression is "23*45/-".Infix to Postfix Conversion :In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows :Scan the Infix string from left to right.Initialise an empty stack.If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.Repeat this step till all the characters are scanned.(After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.Return the Postfix string.Example :Let us see how the above algorithm will be imlemented using an example.Infix String : a+b*c-dInitially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack.StackPostfix StringNext character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.StackPostfix StringThe next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.StackPostfix StringNext character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be :StackPostfix StringEnd result :Infix String : a+b*c-dPostfix String : abc*+d-


Algorithm for infix to prefix conversion?

Algorithm to Convert Infix to Prefix FormSuppose A is an arithmetic expression written in infix form. The algorithm finds equivalent prefix expression B.Step 1. Push ")" onto STACK, and add "(" to end of the AStep 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is emptyStep 3. If an operand is encountered add it to BStep 4. If a right parenthesis is encountered push it onto STACKStep 5. If an operator is encountered then:a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator.b. Add operator to STACKStep 6. If left parenthesis is encontered thena. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd)b. Remove the left parenthesisStep 7. Exit


How do infix notation and postfix notation differ?

It's simply a matter of where the operators are placed in relation to their operands: infix: X + Y prefix: + X Y postfix: X Y + All of the above are equivalent. Prefix notation is also known as Polish notation, hence postfix is also known as reverse Polish notation. Given the infix equation A * B + C / D, the order of evaluation is always parenthesis, orders, divide/multiply, add/subtract (PODMAS), thus we must multiply A by B first, then divide C by D, and finally add the two results together. If we wish to perform the addition first, then we must re-write the equation with parenthesis: A * (B + C) / D. With postfix and prefix notation, operator precedence becomes superfluous because we always evaluate these expressions in left-to-right order: Infix A * B + C / D becomes postfix A B * C D / + or prefix / * A + B C D Infix A * (B + C) / D becomes postfix A B C + * D / or prefix + * A B / C D When we eliminate operator precedence with postfix or prefix notation, we greatly simplify the algorithm required to evaluate complex expressions. For example, given the postfix expression A B C + * D /, we simply read the symbols one at a time, placing them on a stack, until we encounter an operator. We then pop the first two elements off the stack, perform the operation, and then pop the result back on the stack. We repeat this process until there are no more symbols left, at which point the stack holds just one value: the result. With prefix notation, we place the operators on the stack instead of the operands. When we read the first operand we simply store it in an accumulator. We continue pushing operators onto the stack until we encounter the second operand, at which point we can pop the first operator off the stack, perform the operation and update the accumulator. We repeat this process until there are no symbols left, at which point the accumulator holds the final result. Note that when presented with an infix expression, a machine has to convert the expression to the equivalent prefix or postfix expression before it can be evaluated. By eliminating this conversion process, computation by machine can be performed with much greater speed.


Design an algorithm to show the different operations on a stack?

Design an algorithm to show the different operations on a stack?

Related questions

How do you convert postfix expression to prefix expression using c plus plus?

Let Q be an arithmetic expression written in infix notation. Besides operands and operators, Q may also contain left and right parentheses. We assume that the operators in Q consist only of exponentiations (&uarr;), multiplications (*), divisions (/), additions (+) and subtractions (-), and that they have the usual three levels of precedence as given above. We also assume that operators on the same level, including exponentiations, are performed from left to right unless otherwise indicated by parentheses. (This is not standard, since expressions may contain unary operators and some languages perform the exponentiations from right to left. However, these assumptions simplify our algorithm.) The following algorithm transforms the infix expression Q into its equivalent postfix expression P. The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using the operands from Q and the operators, which are removed from STACK. We begin by pushing a left parenthesis onto STACK and adding a right parenthesis at the end of Q. The algorithm is completed when STACK is empty. 69 Algorithm: POLISH(Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P. 1. Push "(" onto STACK, and add ")" to the end of Q. 2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the STACK is empty: 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator &otimes; is encountered, then: (a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) which has the same precedence as or higher precedence than &otimes;. (b) Add &otimes; to STACK. [End of If structure.] 6. If a right parenthesis is encountered, then: (a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add the left parenthesis to P.] [End of If structure.] [End of Step 2 loop.] 7. Exit. The terminology sometimes used for Step 5 is that &otimes; will "sink" to its own level. . 70 EXAMPLE Consider the following arithmetic infix expression Q: Q: A+(B*C- (D/E&uarr;F)*G)*H We simulate the previous algorithm to transform Q into its equivalent postfix expression P. First we push "(" onto STACK, and then we add ")" to the end of Q to obtain: A + ( B * C - ( D / E &uarr; F ) * G ) * H ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13)(14) (15) (16)(17) (18)(19) (20) The elements of Q have now been labeled from left to right for easy reference. Table below shows the status of STACK and of the string P as each element of Q is scanned. Observe that (1) Each operand is simply added to P and does not change STACK. (2) The subtraction operator (-) in row 7 sends * from STACK to P before it (-) is pushed onto STACK. (3) The right parenthesis in row 14 sends j and then I from STACK to P, and then removes the left parenthesis from the top of STACK. (4) The right parenthesis in row 20 sends * and then + from STACK to P, and then removes-the left parenthesis from the top of STACK. After Step 20 is executed, the STACK is empty and P: A B C * D E F &uarr; / G * - H * + 71 which is the required postfix equivalent of Q.


Infix to postfix C?

Infix Expression :Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.Postfix Expression :The Postfix(Postorder) form of the above expression is "23*45/-".Infix to Postfix Conversion :In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows :Scan the Infix string from left to right.Initialise an empty stack.If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.Repeat this step till all the characters are scanned.(After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.Return the Postfix string.Example :Let us see how the above algorithm will be imlemented using an example.Infix String : a+b*c-dInitially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack.StackPostfix StringNext character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.StackPostfix StringThe next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.StackPostfix StringNext character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be :StackPostfix StringEnd result :Infix String : a+b*c-dPostfix String : abc*+d-


Algorithm for infix to prefix conversion?

Algorithm to Convert Infix to Prefix FormSuppose A is an arithmetic expression written in infix form. The algorithm finds equivalent prefix expression B.Step 1. Push ")" onto STACK, and add "(" to end of the AStep 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is emptyStep 3. If an operand is encountered add it to BStep 4. If a right parenthesis is encountered push it onto STACKStep 5. If an operator is encountered then:a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator.b. Add operator to STACKStep 6. If left parenthesis is encontered thena. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd)b. Remove the left parenthesisStep 7. Exit


Why you will not prefer infix to prefix operation in stack?

Postfix expressions are the simplest to evaluate with a stack, for example: 2 3 4 + * 2 (stack: 2) 3 (2 3) 4 (2 3 4) + (2 12) * (14)


How do infix notation and postfix notation differ?

It's simply a matter of where the operators are placed in relation to their operands: infix: X + Y prefix: + X Y postfix: X Y + All of the above are equivalent. Prefix notation is also known as Polish notation, hence postfix is also known as reverse Polish notation. Given the infix equation A * B + C / D, the order of evaluation is always parenthesis, orders, divide/multiply, add/subtract (PODMAS), thus we must multiply A by B first, then divide C by D, and finally add the two results together. If we wish to perform the addition first, then we must re-write the equation with parenthesis: A * (B + C) / D. With postfix and prefix notation, operator precedence becomes superfluous because we always evaluate these expressions in left-to-right order: Infix A * B + C / D becomes postfix A B * C D / + or prefix / * A + B C D Infix A * (B + C) / D becomes postfix A B C + * D / or prefix + * A B / C D When we eliminate operator precedence with postfix or prefix notation, we greatly simplify the algorithm required to evaluate complex expressions. For example, given the postfix expression A B C + * D /, we simply read the symbols one at a time, placing them on a stack, until we encounter an operator. We then pop the first two elements off the stack, perform the operation, and then pop the result back on the stack. We repeat this process until there are no more symbols left, at which point the stack holds just one value: the result. With prefix notation, we place the operators on the stack instead of the operands. When we read the first operand we simply store it in an accumulator. We continue pushing operators onto the stack until we encounter the second operand, at which point we can pop the first operator off the stack, perform the operation and update the accumulator. We repeat this process until there are no symbols left, at which point the accumulator holds the final result. Note that when presented with an infix expression, a machine has to convert the expression to the equivalent prefix or postfix expression before it can be evaluated. By eliminating this conversion process, computation by machine can be performed with much greater speed.


Design an algorithm to show the different operations on a stack?

Design an algorithm to show the different operations on a stack?


Which data structure convert logical to physical address?

Linear data structure is used to convert the logical address to physical address .Stack is used in this and the various conversion such as postfix,prefix and infix notation are come in this


Example program of how to convert infix notation to postfix notation and prefix notation?

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;iprcd(stack[top]))push(symbol);else{while(prcd(symbol)


Algorithm to convert postfix notation into infix notation?

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;iprcd(stack[top]))push(symbol);else{while(prcd(symbol)


How do you convert a prefix expression to postfix using recursion?

struct stack { char ele; struct stack *next; }; void push(int); int pop(); int precedence(char); struct stack *top = NULL; int main() { char infix[20], postfix[20]; int i=0,j=0; printf("ENTER INFIX EXPRESSION: "); gets(infix); while(infix[i]!='\0') { if(isalnum(infix[i])) postfix[j++]=infix[i]; else { if(top==NULL) push(infix[i]); else { while(top!=NULL &amp;&amp; (precedence(top-&gt;ele)&gt;=precedence(infix[i]))) postfix[j++]=pop(); push(infix[i]); } } ++i; } while(top!=NULL) postfix[j++]=pop(); postfix[j]='\0'; puts(postfix); getchar(); return 0; } int precedence(char x) { switch(x) { case '^': return 4; case '*': case '/': return 3; case '+': case '-': return 2; default: return 0; } } void push(int x) { int item; struct stack *tmp; if(top==NULL) { top=(struct stack *)malloc(sizeof(struct stack)); top-&gt;ele=x; top-&gt;next=NULL; } else { tmp=top; top-&gt;ele=x; top-&gt;next=tmp; } } int pop() { struct stack *tmp; int item; if(top==NULL) puts("EMPTY STACK"); else if(top-&gt;next==NULL) { tmp=top; item=top-&gt;ele; top=NULL; free(tmp); } else { tmp=top; item=top-&gt;ele; top=top-&gt;next; free(tmp); } return item; }


Write a java program that implements stack ADT converts infix expression to postfix form evaluates the postfix expression?

// made by vijay NITJ #include&lt;iostream.h&gt; #include&lt;string.h&gt; #include&lt;stdio.h&gt; #include&lt;math.h&gt; int precedence(char); void main() { char q[20]; char p[20]; char stack[20]; int tp=-1,ts=0; stack[0]='('; cout&lt;&lt;stack; int i=0, f=1; cout&lt;&lt;"enter the prefix expression "; gets(q); int l=strlen(q); q[l]=')'; while(ts!=-1) { if(q[i]&gt;=47 &amp;&amp; q[i]&lt;=58 q[i]&lt;=65 &amp;&amp; q[i]&lt;=90 q[i]&lt;=97 &amp;&amp; q[i]&gt;=122) { p[++tp]=q[i]; } else if(q[i]=='(') stack[++ts]=q[i]; else if(q[i]=='+' q[i]=='-' q[i]=='*' q[i]=='/') { if(stack[ts]=='(') stack[++ts]=q[i]; else { while(stack[ts]!='(') { int p1=precedence(q[i]); int p2=precedence(stack[ts]); if(p2&gt;=p1) p[++tp]=stack[ts--]; else { f=0; break; } } if(f==0) stack[++ts]=q[i]; } } else if(q[i]==')') { while(stack[ts]!='(') { p[++tp]=stack[ts--]; } ts--; } i++; } for(int j=0; j&lt;=tp;j++) { cout&lt;&lt;p[j]; } //precedence program int precedence(char a) { if(a=='+' a=='-') return 1; else if(a=='*' a=='/') return 2; }


What is the importance of stack algorithm in your program?

Stack implementations allow us to easily implement backtracking algorithms.