![Page 1: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/1.jpg)
CS240Fall 2014
Mike Lam, Professor
Stacks
The Little Boat
The storm tossed the little boat like a cheap sneaker in an old washing machine. The three drunken fishermen were used to such treatment, of course, but not the tree salesman, who even as a stowaway now felt that he had overpaid for the voyage.
1. Will the salesman die?
2. What color is the boat?
3. And what about Naomi?
![Page 2: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/2.jpg)
Stacks
● Last in, first out (LIFO) sequence data structure● Basic operations
– S.push(e): add element e to top
– S.pop(): remove and return top element
– S.top(): return (but do not remove) top element
– S.is_empty(): return True if no elements
– len(S): return number of elements
![Page 3: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/3.jpg)
Stacks
● s = Stack()
top
s:
![Page 4: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/4.jpg)
Stacks
● s.push(5)
5
top
s:
![Page 5: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/5.jpg)
Stacks
● s.push(3)
5
top
s: 3
![Page 6: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/6.jpg)
Stacks
● len(s) == 2
5
top
s: 3
![Page 7: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/7.jpg)
Stacks
● s.pop() == 3
5
top
s:
![Page 8: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/8.jpg)
Stacks
● s.is_empty() == False
5
top
s:
![Page 9: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/9.jpg)
Stacks
● s.pop() == 5
top
s:
![Page 10: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/10.jpg)
Stacks
● s.is_empty() == True
s:
top
![Page 11: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/11.jpg)
Stack Implementation
● Using Array from PA2– from t_array import Array
– Creation: a = Array(<capacity>)
– Get length: len(a)
– Access: a[i]
– Modify: a[i] = x
– Clean up: a.free()
![Page 12: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/12.jpg)
Stack Analysis
Operation Running Time
S.push(x)
S.pop()
S.top()
S.is_empty()
len(S)
* = amortized
![Page 13: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/13.jpg)
Stack Analysis
Operation Running Time
S.push(x) O(1)*
S.pop() O(1)*
S.top() O(1)
S.is_empty() O(1)
len(S) O(1)
* = amortized
![Page 14: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/14.jpg)
Stacks
● Applications– Reversing a list– Storing browser history– Storing undo actions– Recursion (calling stack)
● Grows "downward" in memory!
![Page 15: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/15.jpg)
Application: Bracket Matching
● Problem: Check for matching parentheses "()", brackets "[]", and braces "{}"
● Hard to do with simple iteration– How to keep track of what we're trying to match?
● Use a stack!
![Page 16: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/16.jpg)
Application: Bracket Matching
● Problem: Check for matching parentheses "()", brackets "[]", and braces "{}"
● Algorithm:– for each letter in text
● if letter in LEFT_OPS– stack.push(letter)
● if letter in RIGHT_OPS– if stack.is_empty() or letter != stack.pop()
● return False
– return stack.is_empty()
![Page 17: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/17.jpg)
Application: Postfix Notation
● Postfix notation– Also referred to as "Reverse Polish Notation" (RPN)
● Normal "infix" notation: 2 + 3● Postfix notation: 2 3 +● Prefix notation: + 2 3● Why is postfix notation interesting?
![Page 18: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/18.jpg)
Application: Postfix Notation
● Infix notation is hard to evaluate● Consider: 2 + 3 * 4
– (2 + 3) * 4 = 20
– 2 + (3 * 4) = 14
● Need to evaluate the "*" first– How to tell this without looking ahead?– Use postfix notation: 3 4 * 2 +
![Page 19: The Little Boat CS240 · 2014. 9. 26. · CS240 Fall 2014 Mike Lam, Professor Stacks The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine](https://reader036.vdocuments.us/reader036/viewer/2022081411/60a7935c9b7829059438d7e6/html5/thumbnails/19.jpg)
Application: Postfix Notation
● Evaluate: 3 4 * 2 +● Algorithm:
– for each word in expression● if word is a number
– stack.push(word)● if word is an operator
– op1 = stack.pop()– op2 = stack.pop()– stack.push(op1 <op> op2)
– return stack.pop()