Linear Data Structures

Stack

Topics Covered in This Lecture
Applications of stack
– Reversing the string
– Balancing symbols
– Postfix expression evaluation
– Translating infix expression to postfix expression

Applications of Stack Reversing the string – push each character on to a stack as it is read. . – When the line is finished. we then pop characters off the stack. and they will come off in the reverse order.

parenthesis. 5. 3. . Read the characters until end of file. At the end of the file. then report an error. The simple algorithm for this purpose is: 1. push it onto the stack. Make an empty stack. If the popped symbol is not the corresponding opening symbol. if the stack is not empty report an error.Applications of Stack Balancing Symbols Compilers use a program that checks whether every symbol (like braces. brackets. 4. then if the stack is empty report an error. Otherwise Pop the Stack. If it is a close any thing. etc) in a program is balanced. 2. If the character is an open any thing.

b/c d*e a – a1 t2 + a2 /a1 = b/c / / t2 = a – b/c / /a2 = d*e/ . 3. 2.Polish Notation a–b/c+d*e Precedence? 1. 4.

a * b 2. +a1 c +*abc Suffix = ab* . a1c+ ab*c+ . Prefix Infix = a * b + c ((a*b) +c) Priority: 1. Suffix.Infix. a1 + c / a1 = a * b / Prefix = * a b .

/*bcd + e / f a . Suffix.+ ef/ abc*d/-ef/+ prefix =a . Prefix infix = a.*bc / d + e / f a ./*bcd + /ef -a/*bcd + /ef +-a/*bcd/ef .b * c / d + e / f suffix =a – bc* / d + e / f a – bc*d/ + e / f a – bc*d/ + ef/ abc*d/.Infix.

Infix. Prefix Infix: a+b*c–d/f+e Suffix: abc*+df/-e+ Prefix: +-+a*bc/dfe . Suffix.

Now we evaluate the following postfix expression. The resulting stack is 2 5 6 stack . it is pushed onto the stack – When an operator is seen. The first four are placed on the stack.Applications of Stack Postfix Expression Evaluation – When a number is seen. 3 6523+8*+3+* 1. then pop two elements from stack and push the result onto the stack.

so 3 and 2 are popped from the stack and their sum 5 is 5 pushed. Next a + is read. 6523+8*+3+* 3 2 5 6 stack 2.Applications of Stack evaluating the following postfix expression. 5 6 stack .

8 5 5 6 stack .Applications of Stack evaluating the following postfix expression. Next 8 is read and pushed. 6523+8*+3+* 5 5 6 stack 3.

6523+8*+3+* 8 5 5 6 stack 4.Applications of Stack evaluating the following postfix expression. Next a * is seen so 8 and 5 are popped as 8 * 5 = 40 is pushed 40 5 6 stack .

45 6 stack . Next a + is read so 40 and 5 are popped and 40 + 5 = 45 is pushed.Applications of Stack evaluating the following postfix expression. 6523+8*+3+* 40 5 6 stack 5.

6523+8*+3+* 45 6 stack 6.Applications of Stack evaluating the following postfix expression. Now 3 is pushed 3 45 6 stack .

Next symbol is + so pops 3 and 45 and pushes 45 + 3 = 48. 6523+8*+3+* 3 45 6 stack 7. 48 6 stack .Applications of Stack evaluating the following postfix expression. so push 48 in stack.

288 stack . 8. so pop the stack and we get the result. the result 6 * 48 = 288 is pushed. Finally a * is seen and 48 and 6 are popped. As there is no input.Applications of Stack evaluating the following postfix expression. 6523+8*+3+* 48 6 stack 7.

etc) then we pop entries form the stack until we find an entry of lower priority. writing symbols onto the output. writing symbols until we encounter a (corresponding) left parenthesis. then we pop the stack. ‘*’. – If we see a right parenthesis. if we read the end of input. we push the operand onto the stack. which is popped but not output. we pop the stack until it is empty. ‘(‘. it is immediately placed onto the output. – Finally. . One exception is that we never remove a ‘(‘ from the stack except when processing a ‘)’.Applications of Stack Translating infix expressions to postfix expression – When an operand is read. – If we see any other symbol (‘+’. – When an operator or left parenthesis comes then save it in the stack initially stack is empty. – When the popping is done.

Applications of Stack Translating infix expressions to postfix expression Convert the following infix expression to postfix expression. so it is passed through to the output 2. 4. a+b*c+(d*e+f)*g 1. ab output Next a * is read. a Then + is read and pushed onto the stack. First the symbol a is read. output + stack 3. so nothing is output and * is put on the . * + stack . The top entry on the operator stack has lower precedence than *. Next b is read and passed through to the output.

Checking the stack. So we pop a * and place it on the output. c is read and output. which is not of lower but equal priority. a+b*c+(d*e+f)*g abc output 5. * + stack abc*+ output + stack .Applications of Stack Converting the following infix expression to postfix expression. and then push +. Next. we find that priority of stack top symbol * is higher than + . 6. The next symbol is a +. Pop the other +.

( + stack 8. abc*+d output . The next symbol read is an ‘(‘. which. is placed on the stack. a+b*c+(d*e+f)*g 7. being of highest precedence.Applications of Stack Converting the following infix expression to postfix expression. Then d is read and output.

Next. there is no output and we push * in stack * ( + stack 10. Since open parenthesis do not get removed except when a closed parenthesis is being processed. a+b*c+(d*e+f)*g 9. abc*+de output . e is read and output. We continue by reading a *.Applications of Stack Converting the following infix expression to postfix expression.

since priority of stack top value is higher so we pop * and push +.Applications of Stack Converting the following infix expression to postfix expression. a+b*c+(d*e+f)*g 11. Now we read f and output f. abc*+de*f output . The next symbol read is a +. + ( + abc*+de* output stack 12.

* + stack 15. so the stack is emptied back to the ‘(‘. we output a +. it is pushed onto the stack. g is read and output. We read a * next. Now.Applications of Stack Converting the following infix expression to postfix expression. abc*+de*f+g output . + stack abc*+de*f+ output 14. Now we read a ‘)’. a+b*c+(d*e+f)*g 13.

a+b*c+(d*e+f)*g * + stack 16. so pop output symbols from the stack until it is empty. The input is now empty. abc*+de*f+g*+ stack output .Applications of Stack Converting the following infix expression to postfix expression.