Stack Applications Lecture 25 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Mon, Mar 30, 2015 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 1 / 34
1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 2 / 34
Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 3 / 34
The Restaurant Triangle While waiting for your order in a restaurant, you pick up and play the Triangle Puzzle. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 4 / 34
The 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. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 5 / 34
The Restaurant Triangle Puzzle The goal is to remove all but one peg. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34
The Restaurant Triangle Puzzle There are 36 possible moves 6 in each of 6 directions). Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34
The Restaurant Triangle Puzzle Six moves up this diagonal. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34
The Restaurant Triangle Puzzle Six moves down this diagonal. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34
The Restaurant Triangle Puzzle Six moves up this diagonal. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34
The Restaurant Triangle Puzzle Six moves down this diagonal. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34
The Restaurant Triangle Puzzle Six moves right horizontally. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34
The Restaurant Triangle Puzzle Six moves left horizontally. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34
The Restaurant Triangle Puzzle List the 36 possible moves in some any) order. The program will systematically consider the moves until it finds a valid move. Then it will Make that move. Push that move onto the stack for future reference. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 7 / 34
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. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 8 / 34
Postfix Expression Evaluation Run the program TrianglePuzzle.cpp. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 9 / 34
Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 10 / 34
Infix Notation 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 3 + 4. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 11 / 34
Postfix Expressions In a postfix expression, the operator is written after the operands. For example, to add 3 and 4, we write 3 4 +. The infix expression 2 3 + 4 5 would be written as 2 3 4 5 + in postfix notation. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 12 / 34
Prefix Expressions In a prefix expression, the operator is written before the operands. For example, to add 3 and 4, we write + 3 4. The infix expression 2 3 + 4 5 would be written as + 2 3 4 5 in prefix notation. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 13 / 34
Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 14 / 34
Fully Parenthesized Infix Expressions With infix expressions, the operations are not necessarily performed from left to right. Infix expressions may require parentheses to specify the order of operation. Precedence and associativity rules allow us to omit some of the parentheses. A fully parenthesized expression assumes no precedence or associativity rules. In a fully parenthesized expression, there is a pair of parentheses for every operator. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 15 / 34
Examples The expression 1 + 2 3 would be fully parenthesized as 1 + 2 3)). The expression 2 3 + 4/5 6 would be fully parenthesized as 2 3) + 4/5)) 6). Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 16 / 34
Infix Expression Evaluation We may use a pair of stacks to evaluate a fully parenthesized infix expression. The expression contains four types of token: Left parenthesis Right parenthesis ) Number, e.g., 123 Operator +,,, / Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 17 / 34
Infix Expression Evaluation To evaluate the expression we need a stack of numbers and a stack of operators. Read the tokens from left to right and process them as follows: Token Left parenthesis Number Operator Right Parenthesis Action No action Push the number onto the number stack Push the operator onto the operator stack 1. Pop two numbers off the number stack 2. Pop one operator off the operator stack 3. Perform the operation on the numbers 4. Push the result onto the number stack Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 18 / 34
Example Use the algorithm to evaluate the expression 2 5) + 6/3)) 8) Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 19 / 34
Example Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Token Number Stack Operator Stack Begin with an empty stack
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + ) 12 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + ) 12 12 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + ) 12 12 8 12 8 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + ) 12 12 8 12 8 ) 4 2 5) + 6/3)) 8)
Infix Expression Evaluation Run the program InfixEvalFullParen.cpp. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 21 / 34
Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 22 / 34
Postfix Expression Evaluation Example Postfix Expressions) Expression: 3 4 + 5 6 +. Left operand of is 3 4 +. Right operand of is 5 6 +. In postfix expressions, parentheses are never needed! Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 23 / 34
Postfix Expression Evaluation To evaluate a postfix expression we need a stack of numbers. Read the tokens from left to right and process them as follows: Token Number Operator Action Push the number onto the number stack 1. Pop two numbers off the number stack 2. Pop one operator off the operator stack 3. Perform the operation on the numbers 4. Push the result onto the number stack Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 24 / 34
Postfix Expression Evaluation Example Postfix Expressions) The fully parenthesized infix expression 2 5) + 6/3)) 8) can be written as 2 5 + 6/3 8 As a postfix expression, it is 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 25 / 34
Example Token 2 2 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Example Token 2 2 5 2 5 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Example Token 2 2 5 2 5 10 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Example Token 2 2 5 2 5 10 6 10 6 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 / 10 2 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 / 10 2 + 12 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 / 10 2 + 12 8 12 8 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 / 10 2 + 12 8 12 8 4 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34
Postfix Expression Evaluation Run the program PostfixEvaluator.cpp. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 27 / 34
Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 28 / 34
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. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 29 / 34
Handling Function Calls The Stack Other Stuff Begin with the current stack Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 30 / 34
Handling Function Calls The Stack Other Stuff Function Parameters Push the function parameters Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 30 / 34
Handling Function Calls The Stack Other Stuff Function Parameters Return Address Push the return address Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 30 / 34
Handling Function Calls The Stack Other Stuff Function Parameters Return Address Local Variables Push the local variables Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 30 / 34
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. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 31 / 34
Handling Function Calls The Stack Other Stuff Function Parameters Return Address Pop the local variables Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 32 / 34
Handling Function Calls The Stack Other Stuff Function Parameters Pop the return address Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 32 / 34
Handling Function Calls The Stack Other Stuff Pop the function parameters Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 32 / 34
Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 33 / 34
Assignment Assignment Read Sections 18.7-18.8. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 34 / 34