0% found this document useful (0 votes)
17 views

Stack Part7

Uploaded by

Sidhu Worldwide
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views

Stack Part7

Uploaded by

Sidhu Worldwide
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 22
Algorithm for Infix to Prefix Initialize the Prefix String to blank Reverse the given infix string For each symbol x in the reversed infix string do > If the character x is an operand — Append the character to the Prefix String » If the character x is a left or right bracket — If the character is ) + Push it into the stack — if the character is ( + Repeatedly pop and append to the Prefix String all the operators/characters until ) is popped from the stack. + Discard the two brackets Algorithm for Infix to Prefix contd.. > If the character x is an operator — While operator stack not empty and precedence(top(S)) > precedence(x) and top(S) != ) do * Pop operator + Append it to the Prefix String — Push x into the stack When all characters in infix expression are processed repeatedly pop the character(s) from the stack and append them to the Prefix String until the stack is empty. Reverse the Prefix String, to get the final expression in Prefix notation. Infix to prefix conversion Stack Infix Expression Reverse Infix Expression | Comments Reverse the Infix Expression. Rest of operations will be performed on this string. Infix to prefix conversion Stack Reverse Infix Expression Prefix Expression Comments ) is pushed onto the operator stack Infix to prefix conversion Stack Reverse Infix Expression Prefix Expression(PE) | Comments The operand f is appended to PE. Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The operator + is pushed onto the stack Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The operand e is appended to Prefix Expression. Infix to prefix conversion Stack Reverse Infix Expression al Prefix Expression Comments ( causes all the stack elements to be popped till ) is encountered. Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The popped elements, except ) are appended to postfix expression. Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The operator - is pushed onto the stack Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The operand d is appended to Prefix Expression. Infix to prefix conversion Stack Reverse Infix Expression al Prefix Expression Comments The precedence of * is greater than — and so it is pushed onto the stack Infix to prefix conversion Stack Reverse Infix Expression al Prefix Expression Comments ) is pushed onto the operator stack Infix to prefix conversion Stack =| Reverse Infix Expression Prefix Expression Comments The operand c is appended to Prefix Expression. Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The operator - is pushed onto the stack, as top is ) Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The operand b is appended to Prefix Expression. Infix to prefix conversion Stack Reverse Infix Expression Prefix Expression Comments The operator + has same precedence as -, and so + is pushed onto the stack Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The operand a is appended to Prefix Expression. Infix to prefix conversion Stack Reverse Infix Expression al Prefix Expression Comments ( causes all the stack elements to be popped till ) is encountered. Infix to prefix conversion Stack = Reverse Infix Expression Prefix Expression Comments The popped elements, except ) are appended to Prefix Expression. Infix to prefix conversion Stack Reverse Infix Expression al Prefix Expression Comments End of string reached. All elements of stack are popped and appended to Prefix Expression Infix to prefix conversion Stack Reverse Prefix Expression al Prefix Expression Comments Reverse the Prefix Expression, to get the Prefix Expression

You might also like