Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),

Similar presentations


Presentation on theme: "Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),"— Presentation transcript:

1 Applications of Stack Maitrayee Mukerji

2 Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(), Pop(), Top(), IsEmpty() IsFull() Implementation ◦ Arrays ◦ Linked Lists (SLL/DLL)

3 Stacks Define a Stack ADT Define a Class Stack that can store any type of basic data – integer, character etc.

4 Applications of Stack Reversing a String Parentheses Matching Arithmetic Expressions ◦ Infix to Postfix conversion ◦ Infix to Prefix conversion ◦ Evaluation of Postfix arithmetic expressions Matching HTML tags Number conversions from ◦ Decimal to other base (2,8,16)

5 Reversing a String main() { string x = "hello"; stack s; int n = x.length(); //Put characters from x onto the stack for(int i=0; i<n; ++i) s.push(x[i]); //take characters off of stack and put them back into x for(int i=0; !s.empty(); ++i, s.pop()) x[i]=s.top(); cout << x << endl; }

6 Matching Parentheses Balanced parentheses and brackets ◦ every open parenthesis/bracket is matched with a corresponding close parenthesis ◦ Parenthesis/bracket are properly nested Example (x(y)(z))- balanced a( {(b)}c)- balanced w)(x)- not balanced p({(q)r)}- not balanced

7 Matching Parentheses Scan expression from left to right, one character at a time If left/opening parenthesis, push corresponding closing parenthesis onto the stack If right/closing parenthesis then ◦ If stack empty then  error, exit ◦ Else  If it matches with the topmost element on the stack then  pop stack and continue  else  report error and exit

8 Matching Parenthesis Draw the status of the stack for following cases: ◦ a ( b { d e [ f ] g { h } I } j k ) l m ◦ ( a b { c } d ( [ e [ f ] g ] ) ( j ) )

9 Matching Parenthesis ◦ Reading the string, from Left to Right, the status of stack is: ) a ( b { d e [ f ] g { h } I } j k ) l m ) } ) } ] ) } ) } } ) } )

10 Arithmetic Expressions A + B : Infix +AB : Prefix AB+ : Postfix Stack Applications ◦ Conversions ◦ Evaluation

11 Evaluation of Arithmetic Expressions Precedence rules ◦ Exponentiation ◦ Multiplication/Division ◦ Addition / Subtraction Use parentheses to overide precedence rules When operators of same precedence are scanned (without brackets) ◦ the order is assumed to be left to right, the order in which they are read

12 Example: Infix to Postfix Example 1 A + B * C => (A + (B*C)) => (A + (BC*)) => A (BC*) + => ABC * + Example 2 (A + B) * C => (A + B) *C) => (AB+) * C) => (AB+) C* => AB + C *

13 Example A + B – C ◦ AB+C- (Postfix) ◦ - + ABC (Prefix) (A+B) * (C-D) ◦ AB+ CD - * (Postfix) ◦ *+AB – CD (Prefix)

14 Conversion: infix to postfix A + B * C – D / E Output: A + B Pre (*) > Pre (+) * C Pre (-) < Pre (*) * Pre (-) == Pre (+) + - D Pre (/) > Pre (-) / E /-

15 Conversion: infix to postfix Scan the input string from left to right one character at a time (token) If the token is:  Left “(“  Push on the stack  Operand (number/letter)  Write directly to output string  Right “)”  Pop stack till corresponding “(” is found  If Stack Empty then “error”  Else while stack not empty  pop stack and write to output string

16 Conversion: infix to postfix Operator: ◦ If stack empty or sTop = “(“ then  push token/ operator onto stack ◦ If precedence (token) > precedence (sTop) then  push token/ operator onto stack ◦ If precedence (token) <= precedence (sTop) then  While stack not empty  Pop stack and write to output  Push token onto stack ◦ If sTop = “)” then  Pop and write till corresponding “(“  If stack empty then “error”  else while stack not empty  pop and write

17 Conversion: infix to postfix A * B – (C+D) + E ◦ Push (*) ◦ Pre (-) Pop (*) & Push (-) ◦ Push “(“ ◦ Push (+) since STop = “(“ ◦ Since “)” => Pop (+), Pop “(” ◦ Pre (+) == Pre (-) => Pop (-), Push (+) ◦ Since end pop till stack empty AB * CD+ - E+ * - - ( - (+ - +

18 Evaluating a postfix expression Each operator in a postfix string refers to the previous two operands in the string One of the operand may itself be the result of applying a previous operator

19 Evaluating a postfix expression using a Stack Read the input string from Left to Right Each time an operand is read-> ◦ Push it on the stack When an operator is read ◦ Pop the two operands from the top of the stack ◦ Perform the indicated operation on the two operands ◦ Push the result back onto the stack, so that it is available as an operand for the next operator

20 Evaluating a postfix expression using a Stack – Example 6 3 + 2 * ◦ Push (6) ◦ Push (3) + ◦ Pop (3) ◦ Pop (6) ◦ Calculate ( 6+3 = 9) ◦ Push (9) ◦ Push (2) * ◦ Pop (2) ◦ Pop (9) ◦ Calculate (9 * 2 = 18) ◦ Push (18)

21 Conversion: Infix to Prefix Reverse the input string Scan the input string from left to right one character at a time (token) Call modified infix to postfix algorithm Reverse the output string to get the prefix expression

22 Conversion: Infix to Prefix If token is Left “(“ Push on the stack Operand (number/letter) Write directly to output string Right “)” Pop stack till corresponding “(” is found If Stack Empty then “error” Else while stack not empty pop stack and write to output string

23 Conversion: Infix to Prefix Operator If stack empty or sTop = “(“ then push token/ operator onto stack If precedence (token) >= precedence (sTop) then push token/ operator onto stack If precedence (token) < precedence (sTop) then while stack not empty Pop stack and write to output Push token onto stack If sTop = “)” then Pop and write till corresponding “(“ If stack empty then “error” else while stack not empty pop and write

24 Conversion: Infix to Prefix A – (B/C) * D- Infix A – (/BC) * D A – [(/BC) * D] A - */BCD -A*/BCD- Prefix

25 Conversion: Infix to Prefix Infix: A – (B/C) * D Reverse:D * (C/B) – A Convert to postfix: ◦D◦D ◦ Push (*) ◦ Push “(“ ◦ DC ◦ Push (/) since sTop == “(“ ◦ DCB ◦ Pop (/), Pop “(“  => DCB/ ◦ Pre (-) Pop (*)  => DCB/*,  Push (-) ◦ DCB/*A ◦ Pop (-) => DCB/*A- Reverse:- A * / B C D * * ( * (/ * -

26 Evaluating Prefix Read the input string from Right to Left Each time an operand is read-> ◦ Push it on the stack When an operator is read ◦ Pop the two operands from the top of the stack ◦ Perform the indicated operation on the two operands ◦ Push the result back onto the stack, so that it is available as an operand for the next operator

27 Assignments Matching HTML tags Converting numbers from Decimal to Binary


Download ppt "Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),"

Similar presentations


Ads by Google