CSC297.Java, Class 19: Lists, Lists, and Still Lists Admin: * BishopD says "Sam can give Alex and Yvonne grades. They don't need to do any work for my class directly." * Review of work on the assignment. * Check BishopD's latest assignment. * Homework: Finish DoublyLinkedList.java * Yes Alex, that's its name. Some Morals from The Homework * Assignment changes what a variable refers to. * That means that when two variables refer to the same value (object), assignment does not change both. * However, if they both refer to the same object and you change it with an object method (e.g. a.setMyCarTo(5)), both variables will seem to change. * Deleting the last element is much harder than deleting the first element. Two Solutions to Length: * Compute it by stepping through the list. * Keep track of it at all times. * Which is better? * The second strategy separates the code between multiple procedures. * If we call length a lot, the second strategy is better. * If we call length rarely, the first strategy is better. Can we solve the "delete last is slow" problem? * We could store the list backwards. * Consequence: Delete first is now slow. * Alternate idea: Store both a next and a previous reference in each cell Build a list of 100 elements. Alternately delete the first element and the last element * Old style: 1 + 99 + 1 + 97 + 1 + 95 + ... = 100 + 98 + 96 + ... + 2 = 2 * (50 + 49 + 48 + ... + 1) = 2550 * New style: 2 + 2 + 2 + 2 .... = 200 Build a list of n elements * Old style: n/2(n/2 + 1) more than n^2/4 * New style: 2*n * The old style curve grows much more quickly. When N is 1000, it is devastatingly slower. * The new style is worth our programming effort. Doubly-Linked Lists Vs. Arrays * Adding to one end of arrays is usually slow (the other end is fast). * Deleting from one end of arrays is usually slow (the other end is fast). * Finding the nth element of arrays is fast. * Adding anywhere in a doubly-linked list is usually fast. (As long as you're already at that position or it's the beginning or end.) * Deleting anywhere in a doubly-linked list is usually fast. (See above.) * Find the nth element of doubly-linked lists is slow.