Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation."— Presentation transcript:

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

2 CS-2851 Dr. Mark L. Hornick 2 The JCF LinkedList class… implements the List interface along with Cloneable, Iterable, Collection … extends AbstractList & AbstractCollection Just like ArrayList So what’s different???

3 CS-2851 Dr. Mark L. Hornick 3 Analysis: SinglyLinkedList class Read the text! The SinglyLinkedList 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

4 CS-2851 Dr. Mark L. Hornick 4 SinglyLinkedList structural implementation Each E element is contained in an Entry object Entry is a nested class contained by SinglyLinkedList class Each Entry is an object that: includes a reference, called a link, to another Entry object Entry contains a field of type E

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

6 CS-2851 Dr. Mark L. Hornick 6 Singly Linked List with 1 entry head SinglyLinkedList Element ref Entry next null 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 size =1

7 CS-2851 Dr. Mark L. Hornick 7 Singly Linked List with 4 entries head SinglyLinkedList Element ref Entry next Element ref Entry next Element ref Entry next Element ref Entry next null 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 size =4

8 CS-2851 Dr. 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 is a Doubly-linked list The Java Collections Framework’s implementation of the LinkedList class stores the elements in a circular, doubly-linked structure of Entry objects.

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

10 CS-2851 Dr. Mark L. Hornick 10 Doubly Linked List with 1 entry tail head DoublyLinkedList prev Element ref Entry next null size =1

11 CS-2851 Dr. Mark L. Hornick 11 Doubly Linked List with 4 entries tail head DoublyLinkedList prev Element ref Entry next prev Element ref Entry next prev Element ref Entry next prev Element ref Entry next null size =4

12 CS-2851 Dr. Mark L. Hornick 12 Group Activity/Demo Implement SinglyLinkedList SinglyLinkedList () 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/

13 CS-2851 Dr. Mark L. Hornick 13 LinkedList vs. 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


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

Similar presentations


Ads by Google