1 / 27

Stacks

Stacks. Stack. ADT where we can only work with "top" Top() : get value on top Pop() : remove value on top Push(value) : put new value on top. Stack. LIFO : Last in First Out. Stack Implementation. Implement with array or linked list:. Implementation Options.

takara
Download Presentation

Stacks

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. Stacks

  2. Stack • ADT where we can only work with "top" • Top() : get value on top • Pop() : remove value on top • Push(value) : put new value on top

  3. Stack • LIFO : Last in First Out

  4. Stack Implementation • Implement with array or linked list:

  5. Implementation Options • Option 1 : Reinvent the wheel

  6. Implementation Options • Option 2 : Inheritance • Private inheritance hides members from outside world

  7. Inheritance Gotchas • Inheritance and templates don't play nice… • Templates not instantiated in time for compiler to verify functions

  8. Inheritance Gotchas • Workarounds:Use this->Specify instantiation

  9. Implementation Options • Option 3 : Composition • Have member ArrayList/vectorAsk it to do hard work

  10. Implemenetation Note • Inheritance/Composition don't manage memory • Don't need custom copy constructor, assignment operator, destructor

  11. Stack Problems • Stack problems focus on most recent data… • Parsing : Paren matching( ( 1 + 2) * 3 )[1 – 2] + (3 – [2 * 6])(((1 – 2) * 3(1 – 2 * [2 +3) – 1]

  12. Stack Problems • Look at each char • If open paren, push to stack • If close paren, pop stack and make sure it matches • At end : if everything had match and stack is empty, success[1 – 2] + (3 – [2 * 6])

  13. Stack Problems • Look at each char • If open paren, push to stack • If close paren, pop stack and make sure it matches • At end : if everything had match and stack is empty, success[1 – 2] + (3 – [2 * 6])

  14. Stack Problems • Look at each char • If open paren, push to stack • If close paren, pop stack and make sure it matches • At end : if everything had match and stack is empty, success[1 – 2] + (3 – [2 * 6])

  15. Stack Problems • Look at each char • If open paren, push to stack • If close paren, pop stack and make sure it matches • At end : if everything had match and stack is empty, success[1 – 2] + (3 – [2 * 6])

  16. Stack Problems • Look at each char • If open paren, push to stack • If close paren, pop stack and make sure it matches • At end : if everything had match and stack is empty, success[1 – 2] + (3 – [2 * 6])

  17. Stack Problems • Look at each char • If open paren, push to stack • If close paren, pop stack and make sure it matches • At end : if everything had match and stack is empty, success[1 – 2] + (3 – [2 * 6])

  18. Stack Problems • Look at each char • If open paren, push to stack • If close paren, pop stack and make sure it matches • At end : if everything had match and stack is empty, success[1 – 2] + (3 – [2 * 6])

  19. Post Fix • Infix notation: • Operator between operands:3 – 2(2 + 3 + 1) * 4 • Postfix notation: • Operator after operands3 2 -2 3 1 + + 4 *

  20. Post Fix Evaluation • While items left • Read next item • If number, push to stack • If operator, pop top two numbers, evaluate, push answer • Pop answer • 2 3 1 + + 4 *

  21. Post Fix Evaluation • While items left • Read next item • If number, push to stack • If operator, pop top two numbers, evaluate, push answer • Pop answer • 2 3 1 + + 4 *

  22. Post Fix Evaluation • While items left • Read next item • If number, push to stack • If operator, pop top two numbers, evaluate, push answer • Pop answer • 231 + + 4 *

  23. Post Fix Evaluation • While items left • Read next item • If number, push to stack • If operator, pop top two numbers, evaluate, push answer • Pop answer • 231 + + 4 *

  24. Post Fix Evaluation • While items left • Read next item • If number, push to stack • If operator, pop top two numbers, evaluate, push answer • Pop answer • 231+ + 4 *

  25. Post Fix Evaluation • While items left • Read next item • If number, push to stack • If operator, pop top two numbers, evaluate, push answer • Pop answer • 231++ 4 *

  26. Post Fix Evaluation • While items left • Read next item • If number, push to stack • If operator, pop top two numbers, evaluate, push answer • Pop answer • 231++4 *

  27. Post Fix Evaluation • While items left • Read next item • If number, push to stack • If operator, pop top two numbers, evaluate, push answer • Pop answer • 231++4*

More Related