CS 100, Summer 2001 Thursday, 8/2 Lecture 21 ----------------------------------------------------------------------- Announcements: + Final: Tuesday, 8/7 from 8-10 a.m., in B17 + reading: none! But you do have homework... (Exercise 15) ----------------------------------------------------------------------- Topics: + Project 2 + Linked Lists! ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Note: there are in fact a bunch of overloaded versions of System.out.println - one for each base type, one for char[], one for String, and one for Object. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Project 2 - Null pointer exceptions, anyone? If you lived through one (or more) of these, you've accomplished something good. - Analyzing different sorting algorithms: graphs for these are quadratic (y = x^2). It'd be niiiice to find a linear sorting algorithm (y = x). ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Linked lists - Motivation: remember Insertion Sort? Moving all the array elements down each time is a real pain. It would be nice to be able to stick the element into its proper place and have everything else move to make room for it. - Hey! That's exactly what linked lists let you do. + Builtin LinkedList class. But let's make our own, so we understand how it works.. Components: - reference that points to the head of the list (not technically part of the list, but we need a reference so we can access the list object) [ CardLinkedList class ] - a series of nodes. Each node has some data inside of it and a reference that points to the next node. [ CardNode class ] - what should the last node's "next" reference point to? NULL. CardNode class: + Default Constructor: creates an empty node + Constructor: creates a node with a Cardinside of it Note: we'll make CardNode a "private inner class" of CardLinkedList, so that no one else can use getLink to get a reference to our list and mess around with it. This also means that we can manipulate its private data at will. CardLinkedList class: + Constructor: creates an empty list + length: returns the number of CardNodes in the list + add: insert a CardNode to the start of the list + remove: remove the starting CardNode (and return it) + inList: returns true if the specified Card is in the list + toString: displays the list of CardNodes + This is actually referred to as a "stack", in computer science parlance, because you can only add to the beginning of the list, and only take off things from the beginning of the list. + Let's make it more array-like by supporting - addAt(Card c, index i) - removeFrom(index i) method. + Approach: group programming - First, we develop a detailed (commented) algorithm for how we'll approach the problem. - Then, we go around the room. Each person can suggest a line of code, modify/delete a line of code, or Pass. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Savitch discusses some other interesting stuff, including: - Iterators (interface!) - Handling exceptions - Doubly-linked lists ---------------------------------------------------------------------- ---------------------------------------------------------------------- + The A la carte Review, tomorrow - You get a full list of questions - You have to answer one from this list - See you tomorrow! ---------------------------------------------------------------------- ---------------------------------------------------------------------- PICK UP handed out Exercise 15