Comp 271 Week 7

Midterm: Tuesday, October 20

java.util.LinkedList

Linked List structures in the Java library: ArrayList v LinkedList, etc
Same interface, but different times.
Since Java doesn't allow pointers into the middle of linked lists, it's hard to find solid examples where linked is faster. Inserting at the head, though, is one good example.

See http://java.sun.com/docs/books/tutorial/collections/implementations/list.html.

Linked and DoublyLinked lists are very popular in OS design, where kernel memory is at a relative premium.

Linked Lists in Bailey

See Node class in Bailey, bottom of page 189; this is the same as our Cell. My version was more compact, but they are just about exactly the same.

What is involved in adding a count field to the LinkedList class, to make size() faster?
Principle: if none of the member functions can desynchronize count, assuming it is right when they are called, then count will always be right. This is a class invariant.

"consistent state" should also be a class invariant. See Principle 11 on the bottom of page 191.

Consistent state cannot be guaranteed if data members are protected, not private!

Why is this? How could consistency fail?

Also note that addFirst() and removeFirst() are much easier for Linked Lists than adds/removes in the middle!

How about insertInOrder? Here we have to assume that the class T extends Comparable; note how I did this. Note that I also had to tweak the private iterator class.

Discuss remove(). Kind of involved, to deal with the special case of the head cell.

Discuss insertAfter vi set, and how they are similar!

Garbage collection and Cells

------

Note that some linked-list operations seem more involved than we'd expect. Life gets moderately simpler for the programmer if we make the list doubly-linked. See diagram on p. 202. However, I wouldn't necessarily obsess about this.

9.6: Circular lists. Skip?

9.7: Observation that the List interface is met by Vectors. Actually, in the Standard Java library, ArrayList and LinkedList both implement List.

Demo: create a linkedlist and see it in action with the inspector. Note that the head cell is dataless! (Also note that I have to do this in a separate project.)
Separate demo: iterator + concurrent list modification = kablooey!
Also note I have List<E> li = new LinkedList<E>()

Stacks and Queues

Bailey has these both implement Linear<E>. I think this is misleading: stacks and queues are supposed to be very spare, and not have extra methods.

Basic stack: push, pop, empty

Bailey adds add(), same as pop(), and remove, same as push(), in order to meet the linear interface. Also peek(), which is not necessary but often convenient.

Stacks are LIFO.

classic lunch-tray model v picture on page 225

Stacks can be implemented as arraylists, or anything else. See java.util.Stack.

Queue: FIFO: add() and remove() are now most commonly known as enqueue() and dequeue(). But note the semantics differ! Usually, queues are FIFO. Also, note that a direct implementation is just a wee easier with, say, doubly-linked lists; with an array, the data creeps along. Or else you do performance-robbing inserts or deletes at the front of the array.

Note that java.util.Queue() is an interface. That's because, in the real world, there are lots of non-FIFO queues. A queue is really a place for things to wait, and waiting occurs only when you have concurrent programs. So there aren't a lot of simple problems solved with queues.

Recursion simulation with a stack.