GVSU CIS 263
Week 3 / Day 1
List ADT
- Walk through
List.h source
- Point out inner class
Node
- Skip over iterators.
- Look at instance variables.
- Go back to constructor, then to
init
- Walk through creating list object. (Show stack diagram)
- Look at
size and empty
- Review at a high level what an insert or two looks like. (Include stack diagram)
Iterators
- An abstraction of a place in a data structure.
- Write code that walks through a linked list or vector using an iterator.
- Walk through list iterator code.
- One instance variable:
current. Points to current node
- Look at
List::begin (sets current to head->next)
- Look at
operator*
- Look at
operator++ and operator++(int)
- What’s up with the
*this?
- Why is one constructor
protected?
- Why is there no copy constructor?
- Look at
end
- Look at
operator==
- Look at
insert
- Why have an iterator class? Why not just return
Node?
Week 3 / Day 2
More lists/iterators
- What is a vector’s iterator?
- Show template code that works for
vector, list, and set.
- Show
erase and highlight delete.
- Show the
list destructor and highlight delete.
Lists vs. Vectors
- What is the big-O run-time for
List#insert_at_end? O(1)
- What is the big-O run-time for
Vector#insert_at_end?
- What if the vector is full?
- How much bigger should the new vector be than the old?
- Look at
Vector#resize
- Why does size double?
- Assume you have a vector of initial size 1.
- Count the number of “extra” setps needed to insert element
x (start x at 0)
- 0 => 0; 1 => 1; 2 => 2; 3 => 0; 4 => 4; 5 => 0; 6 => 0; 7 => 0; 8=> 8, etc.
- Notice that value is 2^n for powers of two and 0 otherwise.
- This averages out to one extra step per iteration.
- Thus, overall average is
O(1)
- What would average be if we only added 1 extra slot?
- What would average be if we only added a constant # of slots?
- This type of analysis is called amortized analysis.
- Key idea: Some operations may be slow; but, if you make them rare enough, your
solution may be considered good overall.
- What are the advantages of a Vector over a List?
- relative simplicity
- less overhead (no Node objects / pointers to maintain)
- More cache friendly (no jumping around the heap)
- What are the advantages of a List over a Vector?
- Insert and delete can be
O(1); but,
- Can we make a
O(1) delete for a vector?
- Lazy delete with a “rebuild” after every
cN deletes
Valgrind
- Show
vector#reserve and highlight delete.
- Demonstrate
valgrind;
- See
ValgrindDemo directory in sample code.
- Not yet available for MacOS 10.14. Need to run in EOS lab.