Download presentation
Presentation is loading. Please wait.
Published byGavin Foster Modified over 9 years ago
1
Page 1 – Spring 2010Steffen 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
2
Page 2 – Spring 2010Steffen Vissing Andersen collection.stringcollection.IStringList
3
Page 3 – Spring 2010Steffen Vissing Andersen StringListArrayBased In the array implementation we have the following two instance variables: private String[] collection; private int size;
4
Page 4 – Spring 2010Steffen Vissing Andersen Linked List – single linked StringNode valuenext
5
Page 5 – Spring 2010Steffen 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 3 Step 3: Let the node at index 2 point to the new node
6
Page 6 – Spring 2010Steffen Vissing Andersen 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
Page 7 – Spring 2010Steffen Vissing Andersen 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
Page 8 – Spring 2010Steffen Vissing Andersen Insert a node in a linked list (step 3/3) Step 3: Let current 's next point to newNode
9
Page 9 – Spring 2010Steffen Vissing Andersen 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
Page 10 – Spring 2010Steffen Vissing Andersen 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
Page 11 – Spring 2010Steffen Vissing Andersen Linked List – circular singly linked Efficient to add and remove first – and add last Remove last is still time consuming
12
Page 12 – Spring 2010Steffen Vissing Andersen Linked List – circular doubly linked Efficient to add and remove first and last
13
Page 13 – Spring 2010Steffen Vissing Andersen Linked List – doubly linked StringDoublyNode previous value Instance variables of the collection class private StringDoublyNode head; private int size; next
14
Page 14 – Spring 2010Steffen Vissing Andersen Single linked implementation (StringNode)
15
Page 15 – Spring 2010Steffen Vissing Andersen Doubly linked implementation (StringDoublyNode)
16
Page 16 – Spring 2010Steffen 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.