1 / 13

Stack Applications

Stack Applications. Qamar Rehman. Example of Stacks. INFIX, POSTFIX and PREFIX Infix: A+B-C Postfix: AB+C- Prefix: -+ABC Order of Precedence of Operators Exponentiation Multiplication/Division Addition/Subtraction. Infix : ( (A+B)*C-(D-E) ) $ (F+G). Conversion to Postfix Expression

jorn
Download Presentation

Stack Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stack Applications QamarRehman

  2. Example of Stacks INFIX, POSTFIX and PREFIX • Infix: A+B-C • Postfix: AB+C- • Prefix: -+ABC Order of Precedence of Operators • Exponentiation • Multiplication/Division • Addition/Subtraction

  3. Infix: ( (A+B)*C-(D-E) ) $ (F+G) Conversion to Postfix Expression • ( (AB+)*C-(DE-) ) $ (FG+) • ( (AB+C*)-(DE-) ) $ (FG+) • (AB+C*DE--) $ (FG+) • AB+C*DE- -FG+$ Exercise: Convert the following to Postfix • ( A + B ) * ( C – D) • A $ B * C – D + E / F / (G + H)

  4. Conversion to Prefix Expression The precedence rules for converting an expression from infix to prefix are identical. The only change from postfix is that the operator is placed before the operands rather than after them. Evaluating a Postfix Expression Each operator in a postfix string refers to the previous two operands in the string.

  5. Algorithm to Evaluate a Postfix Expression Example: Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 + opndstk = the empty stack /* scan the input string reading one element */ /* at a time into symb */ while (not end of input) { symb = next input character; if (symb is an operand) push(opndstk, symb) else { /* symb is an operator */ op2 = pop(opndstk); op1 = pop(opndstk); value = result of applying symb to op1 & op2 push(opndstk, value); } /* end else */ } /* end while */ return (pop(opndstk));

  6. Conversion of Infix Expression to postfix A+B*C = ABC*+ (A+B)*C = AB+C* There must be a precedence function. prcd(op1, op2), where op1 and op2 are characters representing operators. This function returns TRUE if op1 has precedence over op2 when op1 appears to the left of op2 in an infix expression without parenthesis. prcd(op1,op2) returns FALSE otherwise. For example prcd(‘*’,’+’) and prcd(‘+’,’+’) are TRUE whereas prcd(‘+’,’*’) is FALSE. prcd(‘$’,’$’) = FALSE prcd( ‘(‘ , op) = FALSE for any operator op prcd( op, ‘(‘ ) = FALSE for any operator op other than ‘)’ prcd( op, ‘)‘ ) = TRUE for any operator op other than ‘(‘ prcd( ‘)‘ ,op ) = undefined for any operator op (an error)

  7. Algorithm to Convert Infix to Postfix opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ push(opstk, symb); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ Example-1: A+B*C

  8. Algorithm to Convert Infix to Postfix opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ push(opstk, symb); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ Example-2: (A+B)*C

  9. Algorithm to Convert Infix to Postfix opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ If (empty(opstk) || symb != ‘)’ ) push(opstk, symb); else topsymb = pop(opstk); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ Example-2: (A+B)*C

  10. Algorithm to Convert Infix to Postfix opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ If (empty(opstk) || symb != ‘)’ ) push(opstk, symb); else topsymb = pop(opstk); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ Example-3: ( (A-(B+C) ) *D ) $ (E+F)

  11. Algorithm to Convert Infix to Postfix Example-3: ( (A-(B+C) ) *D ) $ (E+F) opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ If (empty(opstk) || symb != ‘)’ ) push(opstk, symb); else topsymb = pop(opstk); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */

  12. Algorithm to Convert Infix to Postfix opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ If (empty(opstk) || symb != ‘)’ ) push(opstk, symb); else topsymb = pop(opstk); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ Example-4: ( A + B ) * ( C – D)

  13. Assignment # 2 • Develop an expression evaluator that read input from file and perform following operations: • Infix to postfix and prefix • Prefix to infix and Postfix • Postfix to infix and Prefix Due Date : 28/10/13

More Related