Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack Applications Lecture 29 Thu, Apr 5, /23/2019

Similar presentations


Presentation on theme: "Stack Applications Lecture 29 Thu, Apr 5, /23/2019"— Presentation transcript:

1 Stack Applications Lecture 29 Thu, Apr 5, 2007 2/23/2019

2 Infix Expressions An infix expression is an arithmetic expression in which the binary operators are written in between the operands. For example, to add 3 and 4, we write 2/23/2019 Stack Applications

3 Postfix Expressions A postfix expression with one (binary) operator is written in the order: left-operand, right-operand, operator. For example, to add 3 and 4, we write 2/23/2019 Stack Applications

4 Prefix Expressions A prefix expression with one binary operator is written in the order: operator, left-operand, right-operand. For example, to add 3 and 4, we write 2/23/2019 Stack Applications

5 Postfix Expression Evaluation
In expression with several operators, each operand is a subexpression Example: * Left operand of * is 3 4 + Right operand of * is 5 6 + Parentheses are never needed! 2/23/2019 Stack Applications

6 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 2/23/2019 Stack Applications

7 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 2/23/2019 Stack Applications

8 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. * 2/23/2019 Stack Applications

9 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. * 4 2/23/2019 Stack Applications

10 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. * 2/23/2019 Stack Applications

11 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. * * 2/23/2019 Stack Applications

12 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. * * – 2/23/2019 Stack Applications

13 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. * * – / 2/23/2019 Stack Applications

14 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. * * – / – 2/23/2019 Stack Applications

15 Postfix Expression Evaluation
Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. * * – / – 9 * 2/23/2019 Stack Applications

16 Postfix Expression Evaluation
Begin with an empty stack. Each number or operator is a token. Process the tokens in the postfix expression from left to right. 2/23/2019 Stack Applications

17 Postfix Expression Evaluation
If the token is a number, Push it onto the stack. If the token is a binary operator, Pop two numbers off the stack. Combine them under the operator. Push the result onto the stack. The single remaining value on the stack is the value of the expression. 2/23/2019 Stack Applications

18 Postfix Expression Evaluation
Evaluate * Read 3: Push 3 Read 4: Push 4 Read +: Pop 4, pop 3, compute = 7, push 7 Read 5: Push 5 Read 6: Push 6 Read +: Pop 6, pop 5, compute = 11, push 11 Read *: Pop 11, pop 7, compute 7 * 11 = 77, push 77 2/23/2019 Stack Applications

19 PostfixEvaluator.cpp PostfixEvaluator.cpp 2/23/2019 Stack Applications

20 Handling Function Calls
When a function is called, the program Pushes the values of the parameters. Pushes the address of the next instruction (to which the function should return later). Allocates space on the stack for the local variables. Branches to the first line in the function. 2/23/2019 Stack Applications

21 Handling Function Calls
When a function returns, the program Pops (and discards) the values of the local variables. Pops the return address (and stores it in the IP register). Branches to the return address. Pops the parameters. The stack has now been returned to its previous state. 2/23/2019 Stack Applications

22 The Restaurant Triangle Puzzle
While waiting for your order in a restaurant, you pick up and play the Triangle Puzzle. 2/23/2019 Stack Applications

23 The Restaurant Triangle Puzzle
There are fifteen holes. One hole is empty. The other 14 holes have pegs in them. You make moves by jumping, as in checkers. One peg jumps an adjacent peg, landing in an empty hole. Remove the jumped peg. 2/23/2019 Stack Applications

24 The Restaurant Triangle Puzzle
The goal is to remove all but one peg. 2/23/2019 Stack Applications

25 The Restaurant Triangle Puzzle
There are 18 possible moves, each in either of 2 directions. 2/23/2019 Stack Applications

26 The Restaurant Triangle Puzzle
Six moves in each direction along this diagonal. 2/23/2019 Stack Applications

27 The Restaurant Triangle Puzzle
Six moves in each direction along this diagonal. 2/23/2019 Stack Applications

28 The Restaurant Triangle Puzzle
Six moves in each direction along this diagonal. 2/23/2019 Stack Applications

29 The Restaurant Triangle Puzzle
Six moves in each direction along this diagonal. 2/23/2019 Stack Applications

30 The Restaurant Triangle Puzzle
Six moves in each direction horizontally. 2/23/2019 Stack Applications

31 The Restaurant Triangle Puzzle
Six moves in each direction horizontally. 2/23/2019 Stack Applications

32 The Restaurant Triangle Puzzle
List the 36 possible moves in a particular order. The program will systematically try the moves until it finds a valid move. Then it will Make that move. Push that move onto the stack. 2/23/2019 Stack Applications

33 The Restaurant Triangle Puzzle
If no move if possible, then it will Pop the last move off the stack. Continue through the list of possible moves until it finds a legal move. Make that move and continue as before. When a single peg remains, the contents of the stack is a winning sequence of moves. 2/23/2019 Stack Applications

34 The Restaurant Triangle Puzzle
TrianglePuzzle.cpp 2/23/2019 Stack Applications

35 Solving a Maze A computer program can solve a maze by using trial and error. Systematically try each open path. When a dead-end is reached, back up to the most recent alternative path. 2/23/2019 Stack Applications

36 Solving a Maze Push the starting square onto the stack.
For the square on top of the stack, attempt to move to an adjacent square. Test the adjacent squares in the following order. Up Down Left Right 2/23/2019 Stack Applications

37 Solving a Maze When a move is possible,
Push the new square onto the stack. When no move is possible (dead-end), Repeatedly pop squares off the stack until an untried alternate move is available. When the final square is reached, the stack contains the path (in reverse order) from start to finish. 2/23/2019 Stack Applications


Download ppt "Stack Applications Lecture 29 Thu, Apr 5, /23/2019"

Similar presentations


Ads by Google