Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack Applications Lecture 29 Thu, Apr 1, 2004 4/29/2019 Stacks.

Similar presentations


Presentation on theme: "Stack Applications Lecture 29 Thu, Apr 1, 2004 4/29/2019 Stacks."— Presentation transcript:

1 Stack Applications Lecture 29 Thu, Apr 1, 2004 4/29/2019 Stacks

2 Topics Stack applications Postfix expressions
The Restaurant Triangle Puzzle Solving Mazes Functions calls 4/29/2019 Stacks

3 Stack Application: Postfix Expression Evaluation
A postfix expression with one (binary) operator is written in the order: left-operand, right-operand, operator. For example, the expression is written in postfix notation. 4/29/2019 Stacks

4 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! 4/29/2019 Stacks

5 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. 4/29/2019 Stacks

6 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. 4/29/2019 Stacks

7 Example: 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 4/29/2019 Stacks

8 Example: Postfix Expression Evaluation
arraystack.h arraylist.h PostfixEvaluator.cpp 4/29/2019 Stacks

9 Stack Application: 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. 4/29/2019 Stacks

10 Stack Application: Handling Function Calls
When a function returns, the program Pops (and discards) the values of the local variables. Pops the return address. Branches to the return address. Pops the parameters. The stack has now been returned to its previous state. 4/29/2019 Stacks

11 Stack Application: The Restaurant Triangle Puzzle
While waiting for your order in a restaurant, you pick up and play the Triangle Puzzle. 4/29/2019 Stacks

12 Stack Application: 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. 4/29/2019 Stacks

13 Stack Application: The Restaurant Triangle Puzzle
The goal is to remove all but one peg. 4/29/2019 Stacks

14 Stack Application: The Restaurant Triangle Puzzle
There are 18 possible moves, each in either of 2 directions. 4/29/2019 Stacks

15 Stack Application: The Restaurant Triangle Puzzle
Six moves in each direction along this diagonal. 4/29/2019 Stacks

16 Stack Application: The Restaurant Triangle Puzzle
Six moves in each direction along this diagonal. 4/29/2019 Stacks

17 Stack Application: The Restaurant Triangle Puzzle
Six moves in each direction along this diagonal. 4/29/2019 Stacks

18 Stack Application: The Restaurant Triangle Puzzle
Six moves in each direction along this diagonal. 4/29/2019 Stacks

19 Stack Application: The Restaurant Triangle Puzzle
Six moves in each direction horizontally. 4/29/2019 Stacks

20 Stack Application: The Restaurant Triangle Puzzle
Six moves in each direction horizontally. 4/29/2019 Stacks

21 Stack Application: 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. 4/29/2019 Stacks

22 Stack Application: 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. When a single peg remains, the contents of the stack is a winning sequence of moves. 4/29/2019 Stacks

23 Stack Application: The Restaurant Triangle Puzzle
TrianglePuzzle.cpp 4/29/2019 Stacks

24 Stack Application: The Restaurant Triangle Puzzle
TrianglePuzzle.cpp 4/29/2019 Stacks

25 Stack Application: 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. 4/29/2019 Stacks

26 Stack Application: 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 4/29/2019 Stacks

27 Stack Application: 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. 4/29/2019 Stacks

28 System Error: Server malfunction. Error code 36FF Virus detected.
4/29/2019 Stacks

29 April fool. 4/29/2019 Stacks


Download ppt "Stack Applications Lecture 29 Thu, Apr 1, 2004 4/29/2019 Stacks."

Similar presentations


Ads by Google