CS 261- Winter 2009 Dynamic Array Queue and Deque

advertisement
CS 261- Winter 2009
Dynamic Array Queue and Deque
Dynamic Array Review
• Last lecture we introduced the dynamic
array
• Benefits: Can be randomly accessed
just like an array, grows as needed
• End user unaware of memory
management issues (buffer reallocation
when more space is needed)
you can build a Stack with a
dynamic array
• Stack is easy with a dynamic array (add
and remove elements from top end)
• Only complication is occasional buffer
increase
• So O(1) remove, O(n) worst case
insertion, but O(1) expected
• Latter often written as O(1)+
You can build a Bag with a
dynamic array
• Building a bag is only slightly more
complicated. Since order is unimportant,
adding to end is easy
• Remove requires moving everything
above the location down
• Add O(1)+, test: O(n), remove: O(n)
Picture of removal
What about building a queue?
• Recall queue is a collection where
elements are inserted at one end,
removed from another (think of queue of
people waiting at a door)
• Which end makes most sense for
insertion? For removal?
• What would be the O(?) ?
Removing from front
Inserting to front
What about a Deque
• Actually, easier to generalize the
structure in this case to a Deque
• Deque - Double Ended Queue (or
maybe Double Ended Staque…)
• Allows (efficient) insertions at both front
and back
• Lets see how we can do this
Key idea
• Key idea: Don’t tie “front” to location
zero
• Instead, allow both “front” and “back” to
float around the array
Picture of array
Adding to either end
• Add to front - back off starting point by
one
• Add to back - increase size by one
• Remove just the opposite
• There is just one small problem, what if
elements wrap around?
Picture of wrapping around
How to handle wrapping
• When making new index position, if less
than zero add capacity to number
• If larger than capacity, subtract capacity
from number
• If size reaches capacity, reallocate new
buffer as before (I’ve done this for you
in the worksheet).
Your turn
• Write the code for Dynamic Array
Deque
Download