160 likes | 252 Views
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.
E N D
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
StringListArrayBased • In the array implementation we have the following two instance variables: • private String[] collection; • private int size;
Linked List – single linked value next StringNode
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
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
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
Insert a node in a linked list (step 3/3) • Step 3: Let current's next point to newNode
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)
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
Linked List – circular singly linked Efficient to add and remove first – and add last Remove last is still time consuming
Linked List – circular doubly linked Efficient to add and remove first and last
Linked List – doubly linked previous next value StringDoublyNode Instance variables of the collection class • private StringDoublyNode head; • private int size;
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