16
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

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

  • Upload
    jarvis

  • View
    21

  • Download
    0

Embed Size (px)

DESCRIPTION

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

Citation preview

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

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

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

Page 2 – Spring 2010 Steffen Vissing Andersen

collection.stringcollection.IStringList

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

Page 3 – Spring 2010 Steffen Vissing Andersen

StringListArrayBased

In the array implementation we have the following two instance variables:

private String[] collection; private int size;

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

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: Software Development with UML and  Java 2  SDJ I2,  Spring 2010

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: Software Development with UML and  Java 2  SDJ I2,  Spring 2010

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: Software Development with UML and  Java 2  SDJ I2,  Spring 2010

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: Software Development with UML and  Java 2  SDJ I2,  Spring 2010

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: Software Development with UML and  Java 2  SDJ I2,  Spring 2010

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: Software Development with UML and  Java 2  SDJ I2,  Spring 2010

Page 10 – Spring 2010 Steffen Vissing Andersen

Linked List – singly linked, head or tail reference

”A” ”B” ”C” ”D” ”E”

tail size

5

Index=4Index=3Index=2Index=1Index=0Internal count=0Internal count=1Internal count=2Internal count=3Internal count=4

”A” ”B” ”C” ”D” ”E”

head Index=0 Index=1 Index=2 Index=3 Index=4

size5

add(String) adds at the end of the list time consuming

Efficient to add and remove lastAdd and remove first are now time consuming

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

Page 11 – Spring 2010 Steffen Vissing Andersen

”A” ”B” ”C” ”D” ”E”

tail size

5

Linked List – circular singly linked

tail

?

tail

(Dummy node)size

0

size

0

Efficient to add and remove first – and add lastRemove last is still time consuming

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

Page 12 – Spring 2010 Steffen Vissing Andersen

Linked List – circular doubly linked

?

head

(Dummy node)

head

”A” ”B” ”C”

head

”D”

size

4

size

0

size

0

Efficient to add and remove first and last

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

Page 13 – Spring 2010 Steffen Vissing Andersen

Linked List – doubly linked

”A” ”B” ”C”

head

”D”

size

4

StringDoublyNode

prev

ious

valu

e

Instance variables of the collection class • private StringDoublyNode head;

• private int size;

next

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

Page 14 – Spring 2010 Steffen Vissing Andersen

Single linked implementation (StringNode)

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

Page 15 – Spring 2010 Steffen Vissing Andersen

Doubly linked implementation (StringDoublyNode)

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

Page 16 – Spring 2010 Steffen Vissing Andersen

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.StringNode or collection.stringcollection.StringDoublyNode