1 / 14

COMP 121

COMP 121. Week 13. Outcomes. List the typical operations and properties of stacks as distinct from other collections. Implement operations on stacks and justify design decisions. Analyze the stack implementation to determine algorithmic efficiency.

wyome
Download Presentation

COMP 121

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 121 Week 13

  2. Outcomes • List the typical operations and properties of stacks as distinct from other collections. • Implement operations on stacks and justify design decisions. • Analyze the stack implementation to determine algorithmic efficiency. • Use a stack data structure to solve a problem.

  3. Learning Activities Activity 13-1 Outcome: List the typical operations and properties of stacks as distinct from other collections. Activity 13-2 Outcome: Implement operations on stacks and justify design decisions.

  4. Learning Activities Activity 13-3 Outcome: Analyze the stack implementation to determine algorithmic efficiency.

  5. Converting Infix to Postfix Example (a + b) * d + e / (f + a * d) + c a b + d * e f a d * + / + c +

  6. Converting Infix to Postfix • Add operands directly to the output string • Left parenthesis: Push onto the stack • Right parenthesis: Pop from the stack all operators up to and including the first left parenthesis, adding operators to the output string. • Operator: Pop all operators of greater or equal precedence, adding them to the output string. The operator just read is pushed onto the stack. • End: Pop the remaining operators and add them in turn to the end of the output string.

  7. Conversion of

  8. Conversion of (cont’d)

  9. Evaluating Postfix Expressions Example 2 3 4 + * 14 6 2 / 3 – 4 2 * + 8

  10. Evaluating Postfix Expressions • Push operands onto the stack • When an operator is read, it is applied to the top two operands on the stack after they have been popped, and the result is pushed onto the stack. If the operator is unary, only one operand is popped, the operator is applied, and the result is pushed back onto the stack. • When all characters in the postfix expression have been scanned, the result can be found on top of the stack.

  11. 4 7 4 28 20 28 8 Expression Action Stack 4 7 * 20 - Push 4 4 7 * 20 - Push 7 4 7 * 20 - Pop 7 and 4 Evaluate 4 * 7 Push 28 4 7 * 20 - Push 20 4 7 * 20 - Pop 20 and 28 Evaluate 28 – 20 Push 8 4 7 * 20 - Pop 8 Stack is empty Result is 8

  12. Learning Activities Activity 13-4 Outcome: Use a stack data structure to solve a problem.

  13. Homework Assignments • Due this week • SubListHW • Due next week • Lab 5 • StackHW

  14. Question and Answer Session

More Related