semi-semester review
example: C/C++ (dense) int(any type, through C++ template) array
- 1 #card=math&code=%5CTheta%20%28N%29) storage(small)
- 2 indexed (random) access R/W in #card=math&code=%5CTheta%20%281%29)
- 3 iterative (sequential access) —->(ptr, index)
- 4 fixed maxlen
- 5 #card=math&code=%5Cmathcal%7BO%7D%20%28N%29) insertion/removal
This type of data structure can’t satisfy our command:
faster #card=math&code=%5Cmathcal%7BO%7D%20%281%29) insertion/removal & dynamic maxlen doubly-linked list(better for removal)
- 1 #card=math&code=%5CTheta%20%28N%29) storage, W/R
-2 often slow- 3 iterative access (ptr) (restricted access)
- 4 dynamic maxlen
- 5 #card=math&code=%5Cmathcal%7BO%7D%20%281%29) insertion/ removal
sparse array “ordered by index” in array/ linked list
- #card=math&code=%5CTheta%20%28NZ%29) storage
- indexed access in #card=math&code=%5Cmathcal%7BO%7D%20%28log%20NZ%29) by binary search
- iterative access with all non-zero terms
- fixed maxlen
- #card=math&code=%5Cmathcal%7BO%7D%28NZ%29) insertion/removal for array, #card=math&code=%5Cmathcal%7BO%7D%281%29) for linked list
doubly-linked list deque (stack + queue)
vector
abstraction “essense”
- save implementation efforts (template: type abstraction)
- “easy” change of data structure
functionality abstraction
dense array & sparse array
indexed (random) access: vector
C++ vector
C++ vector: extendable array
- at(i)
- set(i)
- insert
- erase
extendable array
if array A “overflow” grow the array
How to grow?
- allocate new array B #card=math&code=%5Cmathcal%7BO%7D%20%281%29)
- copy contents from A to B #card=math&code=%5Cmathcal%7BO%7D%20%28N%29)
- remove A and assign B to A #card=math&code=%5Cmathcal%7BO%7D%20%281%29)
Vector
list(iterative, sequential, postional)
- insert(ptr, e)
- remove(ptr,e)
- get(ptr)
- begin() : head
- isEnd(): ==NULL
- next()
iterator
abstraction of ptr (“address”)
dense array | linked list | |
---|---|---|
begin | &arr[0] | head |
end | &arr[N] | NULL |
next | p++ | p->next |
for (iterator<vector<int>> p = c.begin();p!=c.end();p = p.next()){
sum += elem(p);
}
elem(p) ===> (*p)
iterator : an abstract of the element position