270 likes | 422 Views
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.
E N D
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 • Option 1 : Reinvent the wheel
Implementation Options • Option 2 : Inheritance • Private inheritance hides members from outside world
Inheritance Gotchas • Inheritance and templates don't play nice… • Templates not instantiated in time for compiler to verify functions
Inheritance Gotchas • Workarounds:Use this->Specify instantiation
Implementation Options • Option 3 : Composition • Have member ArrayList/vectorAsk it to do hard work
Implemenetation Note • Inheritance/Composition don't manage memory • Don't need custom copy constructor, assignment operator, destructor
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]
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])
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])
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])
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])
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])
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])
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])
Post Fix • Infix notation: • Operator between operands:3 – 2(2 + 3 + 1) * 4 • Postfix notation: • Operator after operands3 2 -2 3 1 + + 4 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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*