Preliminaries Attendance sheets –I remembered! HW1 due tonight –progress report Stage 1 due on Friday –progress report
Today Finish off discussion Bag’s iterator class –For-each loop: syntactic sugar Software design issues –In context of list definition Recursion –In context of list definition
Lists We’ve seen an array-based implementation of a Collection, the Bag. ArrayList is very much the same. –We’ll take a short tour of the code
Advantage of an array-based implementation: –fast access to a specific index –typically less space usage than other options Disadvantage of an array-based implementation: –can be expensive to insert items –resizing is an expensive operation
Amortization Resizing of array-based data structures involves a tradeoff: –many insertions (as in our Bag) are very efficient since no allocation of space is required (an array is allocated as a big block of memory) –some insertions (as in our Bag) are very expensive since resizing must take place
Linked List A linked list is a list implementation which spreads out the cost of space allocation evenly to all insertions. Each insertion involves allocation of space
Advantages/Disadvantages Advantages –predictable cost of insertion –efficient insertion at any point in structure Disadvantages –extra space required to store links –inefficient indexing
Comparison of storage A pair has a first and a second element When pairs are used to construct lists, the first is called the “head” or the “car” the second is called the “tail”, “rest” or “cdr” the pair is called a “cons cell” or simply a “cons”.