In Class: Week 14


We will continue with the linked list classes from last week (in linkedlist.py)

You can copy over code I wrote in class on Tuesday, from:

$ cp ~newhall/public/cs21/week12/tias* .

You should work on the following, in the given order:
  1. Implement insertAtTail and test.

  2. Implement insertSorted and test.
    insertSorted(elm):
    (0) Create a new node with elm as it data field
    (1) If the list is empty, add this as the new head and tail node 
    (2) Else:
        (a) Find the insertion spot
            we need to traverse with curr and prev pointers
        (b) Insert it into the linked list.  3 cases:
            (1) insert before current head node 
            (2) insert after current tail node 
            (3) insert between two linked list nodes:
                need to change the new node's next field to point to the node 
                after the insertion spot, and change the next field of the node 
                immediately before the insertion spot to point to the new node
    
  3. Answer these questions:
    1. How many steps (what is the big-O of) does it take to search for an element in a linked list?
    2. How many steps to insert at head? How many steps to insert n elements (all adding by insert at head)?
    3. How many steps to insert 1 element in sorted order into a linked list? How many steps to insert n elements in sorted order into a linked list? Remember that big-O is worst case analysis not best-case.
    4. How many steps to access the ith element in a python list? How many steps to access the ith element in a linked list?
    5. Can you do binary search on a linked list? If so, how many steps does it take?