1 / 13

Linked-List collections

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???.

daisy
Download Presentation

Linked-List collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked-List collections Structure and Implementation CS-2851Dr. Mark L. Hornick

  2. 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

  3. 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

  4. 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

  5. SinglyLinkedList head SinglyLinkedList<E>with 0 entries null size =0 CS-2851Dr. Mark L. Hornick

  6. 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

  7. 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

  8. 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

  9. DoublyLinkedList head tail Doubly Linked Listwith no entries null size =0 null CS-2851Dr. Mark L. Hornick

  10. DoublyLinkedList head tail Doubly Linked Listwith 1 entry Entry Elementref size =1 null next prev null CS-2851Dr. Mark L. Hornick

  11. 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

  12. 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

  13. 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

More Related