Title: Example: LinkedSet<T>
1Example LinkedSetltTgt
- LinkedSet UML Class Diagram
- LinkedSetltTgt Class
- LinkedSet Attributes/Constructor
- Linked Set Methods
- LinkedSet iterator method
- LinkedIteratorltTgt class
- Project 1 Q/A
- Reading LC 15.4
2LinkedSet UML Class Diagram
ltltinterfacegtgt SetADTltTgt
LinkedSetltTgt
- - rand Random
- - count int
- contents LinearNode
- LinkedSet()
add( ) void addAll( ) void removeRandom(
) T remove( ) union( ) contains( )
equals( ) isEmpty( ) size( ) iterator( )
toString( )
LinearNodeltTgt
- next LinearNode - element T
LinearNode(void) LinearNode
(element T) setNext(node ) void
getNext(void) LinearNode
getElement( ) T set Element (element
T) void
LinkedIteratorltTgt
ltltinterfacegtgt IteratorltTgt
ltltinterfacegtgt IterableltTgt
Self referential or Recursive Assoc.
3LinkedSetltTgt Class
- The LinkedSetltTgt class implements the same
SetADTltTgt interface as ArraySetltTgt using a singly
linked list instead of an array - Externally, your code is not much different when
using the LinkedSetltTgt class instead of the
ArraySetltTgt class - SetADTltStringgt mySet new ArraySetltStringgt()
- OR new LinkedSetltStringgt()
- Internally, method code of the LinkedSetltTgt class
is very different due to the difference in the
underlying data structure being used
4LinkedSetltTgt Class
- Again We assume that SetADTltTgt interface extends
Iterable - If not, we would need to add Iterable after
SetADTltTgt in the implements clause of the
LinkedSetltTgt class header
5LinkedSet Attributes/Constructor
- The class definition starts with
- public class LinkedSetltTgt implements SetADTltTgt
-
- private static Random rand new Random()
- private int count
- private LinearNodeltTgt contents
- public LinkedSet() // default constructor
-
- count 0
- contents null // NOTE not an array
-
6LinkedSet Attributes/Constructor
- Variable contents is an object reference
variable not an array - We dont need to define a default capacity or
pass an initial capacity to the constructor - We dont need to instantiate an array object or
any LinearNode objects in the constructor just
set contents to null
7LinkedSet Methods
- Note that because we are not using a fixed size
data structure such as an array, we dont need a
private expandCapacity() method - Again, these lecture notes will do some code
differently from the textbook so that you can see
more than one way of doing it
8LinkedSet Methods
- add O(n)
- public void add (T element)
-
- if (!contains(element))
-
- LinearNodeltTgt node new LinearNodeltTgt(element)
- node.setNext(contents)
- contents node
- count
-
9LinkedSet Methods
- contains O(n)
- public boolean contains (T target)
-
- LinearNodeltTgt next contents
- while (next ! null)
- if (next.getElement().equals(target)
- return true
- next next.getNext()
-
- return false
10LinkedSet Methods
- removeRandom O(n) not O(1) like ArraySet
- public T removeRandom() throws EmptySetException
-
- LinearNodeltTgt current
- T result null
- if (isEmpty)
- throw new EmptySetException()
- int choice rand.nextInt(count) 1
11LinkedSet Methods
- removeRandom (Continued)
- if (choice 1) // remove from beginning
- result contents.getElement()
- contents contents.getNext()
-
- else // remove from middle or end
- current contents
- for (int skip 2 skip lt choice skip)
- current current.getNext()
- result current.getNext().getElement()
- current.setNext(current.getNext().getNext())
-
- count--
- return result
- // bypassed LinearNode becomes garbage
12LinkedSet iterator Method
- iterator O(1)
- public IteratorltTgt iterator
-
- return new LinkedIteratorltTgt(contents)
-
- We will study the LinkedIterator class to
understand how it is implemented
13LinkedIteratorltTgt Class
- We may have several collection classes like the
LinkedSet class that use an underlying singly
linked linear data structure - Again, we would like to reuse one Iterator class
for all of these collection classes - So we write a general purpose Iterator class for
use with linked data structures
14LinkedIteratorltTgt Class
- The iterator method of the LinkedSet class
instantiates and returns a reference to a
LinkedIterator object to its caller - The LinkedIterator constructor needs to get a
reference to the first LinearNode object in the
specific linked structure to be iterated
15LinkedIteratorltTgt Class
- Class / Attribute Definitions and Constructor
- public class LinkedIteratorltTgt
- implements IteratorltTgt
-
- private LinearNodeltTgt current // current
position - public LinkedIterator(LinearNodeltTgt current)
-
- this.current current
-
16LinkedIterator Methods
- hasNext O(1)
- public boolean hasNext()
-
- return current ! null
-
- next O(1)
- public T next()
-
- if (!hasNext())
- throw new NoSuchElementException()
- T result current.getElement()
- current current.getNext()
- return result
- // old LinearNode does not become garbage
(Why?)
17LinkedIterator Methods
- remove O(1)
- We dont need to implement real code for the
remove method, but there is no return value that
we can use to indicate that it is not implemented - If we dont implement it, we indicate that the
code is not implemented by throwing an exception - public void remove() throws
UnsupportedOperationException -
- throw new UnsupportedOperationException()
18LinkedIterator Methods
- Again, if we implement the remove method, notice
that we dont specify the element that is to be
removed and we do not return a reference to the
element being removed - It is assumed that the calling code has been
iterating on condition hasNext() and calling
next() and already has a reference - The last element returned by next() is the
element that will be removed
19LinkedIterator Method Analysis
- All three LinkedIterator methods are O(1)
- However, they are usually called inside an
external while loop or for-each loop - Hence, the process of iterating through a
collection using an Iterator is O(n)
20LinkedIterator Class in Text
- There is an anomaly in the definition of the
LinkedIterator class in the textbook - The class has a count attribute and the
constructor has a parameter to set count - But that attribute is not used anywhere in the
code of the other methods - The code can and does always identify the end of
the list by the null terminator value - Why was count included?
21LinkedSet Alternative
- If the count attribute was of no use in the
LinkedIterator class, is it also of no use in the
LinkedSet class? - It is possible to implement the code of the
LinkedSet class without a count attribute - What is adversely affected by removing it?
- Size method would become O(n) instead of O(1)
- RemoveRandom method would need to use size method
to calculate its choice of node to remove, but it
is already O(n)
22Project 1 Q/A
- Project 1 is due before next class
- Any questions about Project 1?