1 / 16

Software Development with UML and Java 2 SDJ I2, Spring 2010

Software Development with UML and Java 2 SDJ I2, Spring 2010. Agenda – Week 8 Linked List (a reference based list implementation) IStringList implementations StringListArrayBased StringListReferenceBased. collection.stringcollection.IStringList. StringListArrayBased.

jarvis
Télécharger la présentation

Software Development with UML and Java 2 SDJ I2, Spring 2010

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. Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 • Linked List (a reference based list implementation) • IStringList implementations • StringListArrayBased • StringListReferenceBased

  2. collection.stringcollection.IStringList

  3. StringListArrayBased • In the array implementation we have the following two instance variables: • private String[] collection; • private int size;

  4. Linked List – single linked value next StringNode

  5. Insert a node in a linked list • Insert a node with value = "X" at index 3, i.e. between "C" and "D": • myList.add("X", 3); • Step 1: Find the node at index 2 (node just before index 3) • Step 2: Create a node with value = "X" and next pointing to the node at index 3 • Step 3: Let the node at index 2 point to the new node

  6. Insert a node in a linked list (step 1/3) • Step 1: find the node just before where to insert (insert at index 0 is a special case!) • step 1.0: create a node reference current • step 1.1: let current reference what head is referencing (the first node) • step 1.2: let current reference the next node(the second node) • step 1.3: let current reference the next node(the third node) • ... until current references the node just before where to insert

  7. Insert a node in a linked list (step 2/3) • Step 2: Create a new node with value = "X" and pointing to current's next

  8. Insert a node in a linked list (step 3/3) • Step 3: Let current's next point to newNode

  9. Linked List – single linked Insert and remove at index 0 are special cases Insert and remove at index 0 are now treated just like any other cases (no special cases)

  10. Linked List – singly linked, head or tail reference add(String) adds at the end of the list  time consuming Efficient to add and remove last Add and remove first are now time consuming

  11. Linked List – circular singly linked Efficient to add and remove first – and add last Remove last is still time consuming

  12. Linked List – circular doubly linked Efficient to add and remove first and last

  13. Linked List – doubly linked previous next value StringDoublyNode Instance variables of the collection class • private StringDoublyNode head; • private int size;

  14. Single linked implementation (StringNode)

  15. Doubly linked implementation (StringDoublyNode)

  16. Exercises • StringListReferenceBased • collection.stringtcollection.IStringList (interface already given) • collection.stringcollection.StringListReferenceBased • Version1: use a single linked list with a head reference and size • Version2: use a single linked list with a tail reference and size Note: if you use methods with an index then the internal representation is reversed, e.g. Index i is stored in index size-i-1 • Version3: use a single linked list with a dummy node, a head reference and size • Version4: use a circular single linked list with a tail reference and size • Version5: use a circular doubly linked list with a head reference and size (it is optional if it has a dummy node or not) • collection.stringcollection.StringNodeor collection.stringcollection.StringDoublyNode

More Related