Question
please use java thanks. do not need to compile, just write the code. the LinkedHashSet code is: import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedHashSet implements
please use java thanks. do not need to compile, just write the code.the LinkedHashSet code is:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedHashSet
{
private LinkedList
private int size;
@SuppressWarnings("unchecked")
public LinkedHashSet(int capacity)
{
array = new LinkedList[capacity];
for (int i = 0; i
{
array[i] = new LinkedList
}
}
public int size()
{
return size;
}
private LinkedListIterator
{
// You must implement this method.
return null;
}
public T getMatch(T item)
{
LinkedListIterator
if (item.equals(iterator.getValue()))
{
return iterator.getValue();
}
else
{
return null;
}
}
public T add(T item)
{
// You must implement this method.
return null;
}
public T remove(T item)
{
// Implementing this method is optional.
throw new UnsupportedOperationException();
}
public Iterator
{
return new IteratorImpl
}
// Static nested class (see extra material on inner classes)
private static class IteratorImpl
{
private int index = -1;
private LinkedList
private Iterator
private IteratorImpl(LinkedList
{
this.array = array;
findNext();
}
private void findNext()
{
do
{
index++;
}
while(index
llIterator = index
}
public boolean hasNext()
{
return index
}
public T next()
{
if (!hasNext())
{
throw new NoSuchElementException();
}
T result = llIterator.next();
if (!llIterator.hasNext())
{
findNext();
}
return result;
}
}
}
1 Linked-List Hash Tables In lecture, we implemented linear probing as one technique for resolving collisions in a hash table. An alternative is to store an array of linked lists as the hash table. Whereas linear probing searches for an empty array index that is merely close to an element's hash index, a linked-list implementation allows each element to be inserted into a linked list at its exact hash index. The linked list allows us to have multiple elements at each index of the array, so collisions become a non-issue. There is a file on Moodle called LinkedHashSet.java that contains the start of an implementation of a hash table using linked lists. The iterator ) method has been taken care of for you, as has a constructor that initializes an array of linked lists and a size field. getMatch has been partially implemented, but depends on a subroutine called findMatch ) that you must implement. findMatch 0 should take an item as a parameter and do the following: 1. Use the item's hashCode () method to generate an array index. (Refer to the implementation of HashSet from lecture if you're not sure how to map a hashCode to an array index.) 2. Obtain a LinkedListIterator from the linked list at the index obtained in step 1 3. Advance the iterator until a call of getValue to the iterator returns the item you are searching for, or until the iterator has reached the end of the listStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started