130 likes | 225 Views
Linked-List collections. Structure and Implementation. The JCF LinkedList<E> class…. implements the List<E> interface along with Cloneable<E>, Iterable<E>, Collection<E>… extends AbstractList<E> & AbstractCollection<E> Just like ArrayList<E> So what’s different???.
E N D
Linked-List collections Structure and Implementation CS-2851Dr. Mark L. Hornick
The JCF LinkedList<E> class… implements the List<E> interface along with Cloneable<E>, Iterable<E>, Collection<E>… extendsAbstractList<E> & AbstractCollection<E> Just like ArrayList<E> So what’s different??? CS-2851Dr. Mark L. Hornick
Analysis: SinglyLinkedList<E> class • Read the text! • The SinglyLinkedList<E> class (partially) implements a singly linked list • An example class – not the real JCF implementation • Purpose in book: to introduce the mechanisms of links • Without the full complexity of JCF’s LinkedList<E> CS-2851Dr. Mark L. Hornick
SinglyLinkedList<E> structural implementation Each E element is contained in an Entry<E> object • Entry<E> is a nested class contained by SinglyLinkedList<E> class Each Entry<E> is an object that: • includes a reference, called a link, to another Entry<E> object • Entry<E> contains a field of type E CS-2851Dr. Mark L. Hornick
SinglyLinkedList head SinglyLinkedList<E>with 0 entries null size =0 CS-2851Dr. Mark L. Hornick
SinglyLinkedList Entry Elementref head next Singly Linked Listwith 1 entry null size =1 For each Entry object, the link is to the Entry object that contains the next element in the collection… …except the one that holds the last element in the collection, which contains a null reference CS-2851Dr. Mark L. Hornick
SinglyLinkedList Entry Entry Elementref Elementref head next next Singly Linked Listwith 4 entries For each Entry object, the link is to the Entry object that contains the next element in the collection… …except the one that holds the last element in the collection, which contains a null reference Entry Elementref size =4 next Entry Elementref null next CS-2851Dr. Mark L. Hornick
Doubly-Linked Lists Each Entry object except the first also includes a link to the Entry object that contains the previous element. JCF LinkedList<E> is a Doubly-linked list • The Java Collections Framework’s implementation of the LinkedList<E> class stores the elements in a circular, doubly-linked structure of Entry objects. CS-2851Dr. Mark L. Hornick
DoublyLinkedList head tail Doubly Linked Listwith no entries null size =0 null CS-2851Dr. Mark L. Hornick
DoublyLinkedList head tail Doubly Linked Listwith 1 entry Entry Elementref size =1 null next prev null CS-2851Dr. Mark L. Hornick
DoublyLinkedList Entry Elementref head next tail prev Doubly Linked Listwith 4 entries Entry Entry Elementref Elementref size =4 next next prev prev null Entry Elementref null next prev CS-2851Dr. Mark L. Hornick
Group Activity/Demo • Implement SinglyLinkedList • SinglyLinkedList<E>() • add( E o ) • clear() • get( int index ) • remove( int index ) • size() • Read LinkedList javadoc for description of behavior at http://java.sun.com/j2se/1.5.0/docs/api/ CS-2851Dr. Mark L. Hornick
LinkedListvs.ArrayList Advantages • Flexible storage • Grows/shrinks easier (faster) Disadvantages • Access time • Loss of (true) indexing, thus more expensive to implement indexing • Because storage is non-contiguous • LinkedList objects lack the (fast) random-access ability of ArrayList objects (due to slow indexing) • Access must be done via iteration from the end of the list (either front or back) to the target – thus slower • Storage overhead is higher due to Entry objects CS-2851Dr. Mark L. Hornick