19 - 03/06/09 19:03:10 CS 61B: Lecture 19 Friday, March 6,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
03/06/09 19:03:10 1 19 CS 61B: Lecture 19 Friday, March 6, 2009 Today’s reading: ENCAPSULATED LISTS (a case study in encapsulation) ================== Homeworks 3, 4, and 5 introduced you to three different implementations of linked lists, each fundamentally different. With the Homework 3 lists, if an application writer wants to query the identity of every item in the list without modifying the list, it takes time proportional to the square of n, the number of items in the list (i.e., Theta(n^2) time), because you have to use nth(i) to identify each item in time proportional to i. The lists in Homeworks 4 and 5 allow an application to directly hold a node in a list. By alternating between the next() method and the item field or method, you can query all the list’s items in Theta(n) time. Similarly, if an application holds a node in the middle of a list, it can insert or delete c items there in time proportional to c, no matter how long the list is. The Homework 5 lists (SList and DList) are well-encapsulated, whereas the Homework 4 DList has flaws. I will discuss these flaws today to illustrate why designing the really good list ADTs of Homework 5 was tricky. Let’s ask some questions about how lists should behave. (1) What happens if we invoke l.remove(n)--but the node n is in a different list than l? In Homework 4, Part II asks whether it is possible for an application to break the DList invariants. One way to do this is to mismatch nodes and lists in method calls. When an application does this, the "size" field of the wrong list is updated, thereby breaking the invariant that a list’s size field should be correct. How can we fix this? ADT interface answer: The methods remove(), insertAfter(), etc. should always update the right list’s "size" field. Implementation answer: It’s unacceptably slow to walk through a whole list just to see if the node n is really in the list l. Instead, every node should keep a reference to the list that contains it. In Homework 5, each ListNode has a "myList" field. (2) Should insertAfter(), remove(), etc. be methods of List or ListNode? Normally, we expect the methods that modify a data structure (like a List)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/21/2010 for the course CS 61B taught by Professor Canny during the Spring '01 term at University of California, Berkeley.

Page1 / 2

19 - 03/06/09 19:03:10 CS 61B: Lecture 19 Friday, March 6,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online