Sei sulla pagina 1di 2

Convert the infix to postfix expression E=A+(B*C-(D/E^F)*G)*H AnswerkeyABC*DEF^/G*-H*+

Algorithm for push and pop operation Push Operation PUSH(Stack, Top, MAX Stack, Item) 1.Stack is already filled? If top=MAX STK Print :Over flow and Return 2.Set top=top+1 3.Set stack[top]=item 4.return Pop Operation POP(Stack,top,Item) 1.Stack has an item to be removed. If top=0 Print :underflow and return 2.Set item =stack[top] 3.Set top=top-1 Return

Application of the stack 1. Tower of Hanoi problem 2. Evaluation of arithmetic expression 3. Eight queen problem 1.Tower of Hanoi problem Aim of game Move n disk from peg A to peg C using peg B as an auxiliary . Rules of tower of Hanoi problem 1.only one disk moved at a time. 2.A larger disk cannot placed on a smaller disk 3.Only top disk on any peg may be moved to any other peg . Solution 1.Move (n-1)disk from peg A to peg B 2.Move n disk from Peg A to peg c

3.Move (n-1)disk from Peg B to peg c n=3, A->C,A->B,C->B,B->A,B->C,A->C

Evaluation of arithmetic expression 1.convert the infix to postfix expression. 2.evaluation of postfix expression. Tree traversals Pre order Inorder Post order Pre order 1.process the root R 2.Traverse the left sub tree of R in Pre order 3.Traverst the right sub tree of R in Pre order Inorder 1. Traverse the left sub tree of R in inorder 2. process the root R 3. Traverse the right sub tree of R in inorder Post order Traverse the left sub tree of R in Post order Traverse the right sub tree of R in Post order process the root R

Potrebbero piacerti anche