Page 1 – Spring 2010 Steffen Vissing Andersen 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
16
Embed
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. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1 – Spring 2010 Steffen Vissing Andersen
Software Development with UML and Java 2 SDJ I2, Spring 2010
Agenda – Week 8
• Linked List (a reference based list implementation)
In the array implementation we have the following two instance variables:
private String[] collection; private int size;
Page 4 – Spring 2010 Steffen Vissing Andersen
Linked List – single linked
”A” ”B” ”C” ”D” ”E”
head Index=0 Index=1 Index=2 Index=3 Index=4
size5
head
size0
StringNode
value
next
Page 5 – Spring 2010 Steffen Vissing Andersen
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 3Step 3: Let the node at index 2 point to the new node
”A” ”B” ”C” ”D” ”E”
head Index=0 Index=1 Index=2 Index=3 Index=4
size5
Page 6 – Spring 2010 Steffen Vissing Andersen
Insert a node in a linked list (step 1/3)
”A” ”B” ”C” ”D” ”E”
head
size5
current
current
current
(step 1.1)
(step 1.2)
(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
Page 7 – Spring 2010 Steffen Vissing Andersen
Insert a node in a linked list (step 2/3)
”A” ”B” ”C” ”D” ”E”
head
size5
current
newNode
”X”(Step 2)
Step 2: Create a new node with value = "X" and pointing to current's next
Page 8 – Spring 2010 Steffen Vissing Andersen
Insert a node in a linked list (step 3/3)
”A” ”B” ”C” ”D” ”E”
head
size5
current
newNode
”X”
(Step 3)
Step 3: Let current's next point to newNode
Page 9 – Spring 2010 Steffen Vissing Andersen
Linked List – single linked
”A” ”B” ”C” ”D” ”E”
head
head
size 0
head
(Dummy node)
? ”A” ”B” ”C” ”D”
head
”E”
size5
size5
size0
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)
Page 10 – Spring 2010 Steffen Vissing Andersen
Linked List – singly linked, head or tail reference
• 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.StringNode or collection.stringcollection.StringDoublyNode