90 likes | 228 Views
Data Structure . MSc.IT Ali Abdul Karem Habib. Stack Applications.
E N D
Data Structure MSc.IT Ali Abdul KaremHabib
Stack Applications • Web browser: stores the addresses of recently visited sites on a stack. Each time a user visits a new site, the address of the site is pushed into the stack of addresses. Use the back button the user can pop pack to previously visited sites! . • Text editors: powerful text editors keep text changes in a stack. The user can use the undo mechanism to cancel recent editing operations .
Arithmetic Expression • An arithmetic expression is one which is evaluated by performing a sequence of arithmetic operations to obtain a numeric value. • Levels of Precedence for the usual five binary operations on arithmetic operation Q . • Highest: Exponentiation (^) • Next Highest: Multiplication (*)and division (/) • Lowest: Addition (+) and subtraction (-)
Prefix, Postfix & Infix Notation • Infix : the operator is placed between operands. Example: (A+B)*C parentheses necessary • Prefix: the operator is placed before the operands Example: *+ABC • Postfix ( Reveres polish notation): the operator is placed after the operands . Example: AB+C*
Transforming Infix Expression into Prefix Expression • (A+B) – (C*D) • Step 1 : +AB • Step2 : *CD • +AB-*CB • Prefix :- -+AB*CB • (A+B) – (C*D) • Step 1 : AB+ • Step 2 : CD* AB+-CD* Postfix : AB+CD*- Transforming Infix Expression into Postfix Expression
Transform PREFIX To INFIX : • +/*A B C D • Note : We will Start the tack operator from the right and the operands from the left . • Step 1 : First 2 Operands from the left and first operator from the right . • +/(A*B)CD • Step2 : +((A*B)/C)D • Step3 : ((A*B)/C)+D Infix Expression
Transform Postfix to Infix • A B C + – • Note :- We will tack the operator from the left and 2 operands from the left also . • Step 1 : (A+B)C- • Step2 : (A+B)- C
Example :- Infix : (7+8)-((6*3)/2)) Transformed to Postfix (78+63*2/-)
Assignment -4- • 1)Convert these INFIX to PREFIX and POSTFIX : • A / B – C / D • (A + B) ^ 3 – C * D • A ^ (B + C) • 2)Convert these PREFIX to INFIX and POSTFIX : • + – / A B C ^ D E • – + D E / X Y • ^ + 2 3 – C D • 3)Convert these POSTFIX to INFIX and PREFIX : • A B C + – • G H + I J / * • A B ^ C D + –