lecture19 stack impl - tiziana ligorio › lectures › lecture19_stack_impl.pdf · void pop(); //...
TRANSCRIPT
![Page 2: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/2.jpg)
Today’s Plan
Recap
Stack Implementations: Array Vector Linked Chain
!2
![Page 3: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/3.jpg)
Announcements
!3
![Page 4: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/4.jpg)
Stack ADT#ifndef STACK_H_ #define STACK_H_ template<class T> class Stack { public:
Stack(); void push(const T& new_entry); // adds an element to top of stack void pop(); // removes element from top of stack T top() const; // returns a copy of element at top of stack int size() const; // returns the number of elements in the stack
bool isEmpty() const; // returns true if no elements on stack false otherwiseprivate: //implementation details here
}; //end Stack #include "Stack.cpp" #endif // STACK_H_`
!4
![Page 5: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/5.jpg)
Choose a Data Structure
Array?
Vector?
Linked chain?
!5
![Page 6: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/6.jpg)
Choose a Data Structure
Inserting and removing from same end (LIFO)
Goal: minimize work - Ideally O(1)
What would you suggest?
!6
![Page 7: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/7.jpg)
Array
!7
0 1 2 3 4 5
Where is the top of the stack?
items_
![Page 8: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/8.jpg)
Array
!8
0 1 2 3 4 5
Top of the stack: items_[item_count_]
item_count_ = 0
max_items_ = 6
items_
![Page 9: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/9.jpg)
Array
!9
O
0 1 2 3 4 5
Top of the stack: items_[item_count_]
items_
push(‘O’)
item_count_ = 1
max_items_ = 6
![Page 10: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/10.jpg)
Array
!10
O Z
0 1 2 3 4 5
Top of the stack: items_[item_count_]
items_
push(‘Z’)
item_count_ = 2
max_items_ = 6
![Page 11: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/11.jpg)
Array
!11
O Z B
0 1 2 3 4 5
Top of the stack: items_[item_count_]
items_
push(‘B’)
item_count_ = 3
max_items_ = 6
![Page 12: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/12.jpg)
Array
!12
O Z
0 1 2 3 4 5
Top of the stack: items_[item_count_]
items_
pop()
item_count_ = 2
max_items_ = 6
Pops items_[item_count_ -1]
![Page 13: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/13.jpg)
Array Analysis
1 assignment + 1 increment/decrement = O(1)
size : O(1)isEmpty: O(1)push: O(1)pop : O(1)top : O(1)
GREAT!!!!
!13
![Page 14: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/14.jpg)
Array Analysis
1 assignment + 1 increment/decrement = O(1)
size : O(1)isEmpty: O(1)push: O(1)pop : O(1)top : O(1)
GREAT???
!14
![Page 15: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/15.jpg)
Array
!15
O Z B Y L P
0 1 2 3 4 5
Top of the stack: items_[item_count_]
items_
push(’T’)
item_count_ = 6
max_items_ = 6
Sorry Stack is Full!!!
![Page 16: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/16.jpg)
Vector
std::vector<T> some_vector;
So what is a vector really?
!16
![Page 17: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/17.jpg)
Vector
std::vector<T> some_vector;
So what is a vector really?
!17
0buffer_ =len_ =capacity_ = 5
Vector (simplified)
O Z
0 1 2 3 4
Push and pop same as with arrays
![Page 18: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/18.jpg)
Vector
std::vector<T> some_vector;
So what is a vector really?
!18
buffer_ =len_ = 0capacity_ = 5
Vector (simplified)
O Z B Y L
0 1 2 3 4
Stack is Full?
![Page 19: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/19.jpg)
Vector
std::vector<T> some_vector;
So what is a vector really?
!19
buffer_ =len_ = 0capacity_ = 5
Vector (simplified)
O Z B Y L
0 1 2 3 4
No, I’ll Grow!!!
O Z B Y L
0 1 2 3 4 5 6 . . .
![Page 20: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/20.jpg)
Lecture Activity
How much should it grow?
Write a short paragraph arguing the pros and cons of growing by the amount you propose
!20
![Page 21: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/21.jpg)
Vector Analysis
1 assignment + 1 increment/decrement = O(1)
size : O(1)isEmpty: O(1)push: O(1)pop : O(1)top : O(1)
GREAT!!!!
!21
![Page 22: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/22.jpg)
Vector Analysis
1 assignment + 1 increment/decrement = O(1)
size : O(1)isEmpty: O(1)push: O(1)pop : O(1)top : O(1)
GREAT???
!22
Except when stack is full must: - allocate new array - copy elements in new array - delete old array
![Page 23: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/23.jpg)
Vector Analysis
1 assignment + 1 increment/decrement = O(1)
size : O(1)isEmpty: O(1)push: O(1)pop : O(1)top : O(1)
GREAT???
!23
Except when stack is full must: - allocate new array O(1) - copy elements in new array O(n) - delete old array O(1)
![Page 24: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/24.jpg)
How should Vector grow?
Sometimes 1 “step”
Sometimes n “steps”
Consider behavior over several pushes
!24
![Page 25: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/25.jpg)
Vector Growth: a naive approach
std::vector<T> some_vector;
So what is a vector really?
!25
buffer_ =len_ = 0capacity_ = 5
Vector (simplified)
O Z B Y L
0 1 2 3 4
I’ll Grow!!! I will add space for the
item to be added
O Z B Y L
0 1 2 3 4 5
![Page 26: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/26.jpg)
Vector Growth: a naive approach
If vector grows by 1 each time, every push costs n “steps”
Cost of pushes: 1 + 2 + 3 + 4 + 5 + . . . + n = n (n+1)/2
!26
![Page 27: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/27.jpg)
Vector Growth: a naive approach
If vector grows by 1 each time, every push costs n “steps”
Cost of n pushes: 1 + 2 + 3 + 4 + 5 + . . . + n = n (n+1)/2 = n2 /2 + n / 2 O(n2)
!27
![Page 28: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/28.jpg)
Vector Growth: a better approach
std::vector<T> some_vector;
So what is a vector really?
!28
buffer_ =len_ = 0capacity_ = 5
Vector (simplified)
O Z B Y L
0 1 2 3 4
I’ll Grow!!! I will add two more slots!
O Z B Y L
0 1 2 3 4 5 6
![Page 29: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/29.jpg)
Vector Growth: a better approach
If vector grows by 2 each time,
Let a “hard push” be one where the whole vector needs to be copied
When vector is not copied we have an “easy push”
Now half our pushes will be easy (1 step) and half will be hard (n steps)
So if reconsider the work over several pushes?
!29
![Page 30: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/30.jpg)
Analysis visualization adapted from Keith Schwarz
!30
![Page 31: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/31.jpg)
Vector Growth: a better approach
!31Easy push Easy push Easy pushHard push Hard push Hard push Hard pushEasy push
![Page 32: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/32.jpg)
Vector Growth: a better approach
!32
Work Saved
Easy push Easy push Easy pushHard push Hard push Hard push Hard push
By simply adding one extra “slot” we roughly cut down the work by half on
average (over several pushes)
Easy push
![Page 33: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/33.jpg)
Vector Growth: a better approach
!33Easy push Easy push Easy pushHard push Hard push Hard push Hard push
Let’s look at it a different way: what happens if I
spread the work over time?
Easy push
![Page 34: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/34.jpg)
Vector Growth: a better approach
!34Easy push Easy push Easy pushHard push Hard push Hard push Hard push
Let’s look at it a different way: what happens if I
spread the work over time?
Easy push
![Page 35: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/35.jpg)
Vector Growth: a better approach
!35Easy push Easy push Easy pushHard push Hard push Hard push Hard push
Let’s look at it a different way: what happens if I
spread the work over time?
Easy push
![Page 36: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/36.jpg)
Vector Growth: a better approach
!36Easy push Easy push Easy pushHard push Hard push Hard push Hard push
Easy push
Now compare with the “naive” approach
![Page 37: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/37.jpg)
Vector Growth: a better approach
!37Easy push Easy push Easy pushHard push Hard push Hard push Hard push
Easy push
Now compare with the “naive” approach
By simply adding one extra “slot” we roughly cut down the work by
half (over several pushes)
![Page 38: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/38.jpg)
Can we do better?
!38
![Page 39: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/39.jpg)
Vector Growth: a much better approach
std::vector<T> some_vector;
So what is a vector really?
!39
buffer_ =len_ = 0capacity_ = 5
Vector (simplified)
O Z B Y L
0 1 2 3 4
I’ll Grow!!! I’ll double my size!
O Z B Y L
0 1 2 3 4 5 6 7 8 9
![Page 40: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/40.jpg)
Vector Growth: a much better approach
!40
![Page 41: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/41.jpg)
Vector Growth: a much better approach
!41
Let’s spread the work over time
![Page 42: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/42.jpg)
Vector Growth: a much better approach
!42
Let’s spread the work over time
![Page 43: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/43.jpg)
Vector Growth: a much better approach
!43
Let’s spread the work over time
![Page 44: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/44.jpg)
Vector Growth: a much better approach
!44
Let’s spread the work over time
![Page 45: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/45.jpg)
Vector Growth: a much better approach
!45
Let’s spread the work over time
![Page 46: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/46.jpg)
Vector Growth: a much better approach
!46
Let’s spread the work over time
Over time I can spread my work so that I have (OVER SEVERAL PUSHES) constant work
![Page 47: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/47.jpg)
Vector Growth: a much better approach
!47
Let’s spread the work over time
Amortized Analysis
Over time I can spread my work so that I have (OVER SEVERAL PUSHES) constant work
![Page 48: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/48.jpg)
Vector Growth summarized
If it grows by 1, O(n2) over time (n pushes - AMORTIZED ANALYSIS)
If it grows by 2, push takes roughly half the“steps” but still O(n2) over time ( n pushes - AMORTIZED ANALYSIS)
If it doubles its size, push takes O(1) over time (n pushes - AMORTIZED ANALYSIS)
!48
![Page 49: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/49.jpg)
A steadily shrinking Stack
Let’s consider this application:
- Push the 524,288th (219)element onto Stack which causes it to double it’s size to 1,048,576 (220) - Reading an input file - pop the elements that match - manipulate input record accordingly - repeat
!49
![Page 50: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/50.jpg)
A steadily shrinking Stack
Let’s consider this application:
- Push the 524,288th (219)element onto Stack which causes it to double it’s size to 1,048,576 (220) - Reading an input file - pop the elements that match - manipulate input record accordingly - repeat
!50
How much I pop will depend on input
![Page 51: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/51.jpg)
A steadily shrinking Stack
Let’s consider this application:
- Push the 524,288th (219)element onto Stack which causes it to double it’s size to 1,048,576 (220) - Reading an input file - pop the elements that match - manipulate input record accordingly - repeat
!51
Assume a few matches at each
iteration -> mostly empty stack but it will be around for a
long time!
Useless memory wasteI will not shrink!
![Page 52: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/52.jpg)
Linked Chain
!52
top_
![Page 53: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/53.jpg)
Linked Chain
!53
top_ new_node_ptr
push
![Page 54: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/54.jpg)
Linked Chain
!54
top_ new_node_ptr
push
![Page 55: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/55.jpg)
Linked Chain
!55
top_ new_node_ptr
push
![Page 56: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/56.jpg)
Linked Chain
!56
top_
![Page 57: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/57.jpg)
Linked Chain
!57
top_
push
new_node_ptr
![Page 58: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/58.jpg)
Linked Chain
!58
top_
push
new_node_ptr
![Page 59: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/59.jpg)
Linked Chain
!59
top_
push
new_node_ptr
![Page 60: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/60.jpg)
Linked Chain
!60
top_
push
new_node_ptr
![Page 61: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/61.jpg)
Linked Chain
!61
top_
![Page 62: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/62.jpg)
Linked Chain
!62
top_
![Page 63: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/63.jpg)
Linked Chain
!63
poptemp_ptrtop_
![Page 64: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/64.jpg)
Linked Chain
!64
poptemp_ptr
top_
![Page 65: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/65.jpg)
Linked Chain
!65
poptemp_ptr
top_
![Page 66: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/66.jpg)
Linked Chain
!66
poptemp_ptr
top_
![Page 67: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/67.jpg)
Linked Chain
!67
top_
![Page 68: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/68.jpg)
Linked-Chain Analysis
1 assignment + 1 increment/decrement = O(1)
size : O(1)isEmpty: O(1)push: O(1)pop : O(1)top : O(1)
GREAT!!!! And there is no “Except” case here, every operation is O(1)!
!68
![Page 69: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/69.jpg)
To summarize
Array: O(1) for push and pop, but size is bounded
Vector: size is unbounded but -Some push operations take O(1), others take O(n) -> O(1) over time (AMORTIZED ANALYSIS) Linked-Chain: O(1) for push and pop and size is unbounded
!69
![Page 70: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/70.jpg)
Implement Stack ADT#ifndef STACK_H_ #define STACK_H_ template<class T> class Stack { public:
Stack(); ~Stack(); // destructor Stack(const Stack<T>& a_stack); //copy constructor void push(const T& newEntry); // adds an element to top of stack void pop(); // removes element from top of stackT top() const; // returns a copy of element at top of stackint size() const; // returns the number of elements in the stack
bool isEmpty() const; // returns true if no elements on stack false otherwiseprivate: Node<T>* top_; // Pointer to top of stack int item_count; // number of items currently on the stack
}; //end Stack #include "Stack.cpp" #endif // STACK_H_
!70
![Page 71: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/71.jpg)
Problem!
What happens if we call top() on empty stack???
T Stack<T>::top() const{ if(isEmpty())
//what do we return??? else return top_->getItem();
}
!71
![Page 72: Lecture19 Stack Impl - Tiziana Ligorio › Lectures › Lecture19_Stack_Impl.pdf · void pop(); // removes element from top of stack T top() const; // returns a copy of element at](https://reader034.vdocuments.net/reader034/viewer/2022042322/5f0be1297e708231d432aabd/html5/thumbnails/72.jpg)
Fix!
What happens if we call top() on empty stack???
T Stack<T>::top() const{ if(isEmpty())
throw (std::out_of_range(“top() called on empty stack”)); else return top_->getItem();
}
!72