[A5] Doubly-extensible array (2 pts) [00T9]
This question is also courtesy of Jeff Erickson.
A doubly extensible array is a data structure that stores a sequence of items and supports the following operations:
- \mathsf {AddToFront}(x) adds x to the beginning of the sequence.
- \mathsf {AddToEnd}(x) adds x to the end of the sequence.
- \mathsf {Lookup}(k) returns the kth item in the sequence (0-indexed), or \mathsf {Null} if the current length of the sequence is less than or equal to k.
Describe and analyze a simple data structure that implements a doubly extensible array. Your \mathsf {AddToFront} and \mathsf {AddToBack} algorithms should take O(1) amortized time, and your \mathsf {Lookup} algorithm should take O(1) worst-case time. The data structure should use O(n) space, where n is the current length of the sequence.