This is the code below:
package hashset;
import java.util.Iterator;
public class MySet {
// implements a set using a separate chaining hash table
privateclass Node {
private Integer element;
private Node next;
private Node(Integer e, Node n) {
element = e;
next = n;
}
}
private Node table[]; //an array of linked list
privateinttableSize; //current number of lists in the table
privateintnumElements; //number of elements in the set
privatefinalintprimes[] = {7, 23, 59, 131, 271, 563, 1171,
2083, 4441, 8839, 16319, 32467,
65701, 131413, 263983, 528991};
privateintprimeIndex; //last prime used
privateint nextPrime(intp) {
//finds the next prime from the list above
//used for resizing and the initial size
while (primes[primeIndex] <= p)
primeIndex++;
returnprimes[primeIndex];
}
public MySet(ints) {
//s is a hint for the initial size
primeIndex = 0;
tableSize = nextPrime(s);
table = new Node[tableSize];
numElements = 0;
}
//return the hash function value for k
privateint hash(Integer k) {
return Math.abs(k.hashCode() % tableSize);
}
//"double" the table size and reinsert the values stored in the
//current table. the table size should remain prime
privatevoid resize()
{
// Double the table size
intnewSize = nextPrime(tableSize * 2);
Node[] newTable = new Node[newSize];
for (inti = 0; i < tableSize; i++) {
Node current = table[i];
while (current != null) {
Node next = current.next;
intindex = hash(current.element);
current.next = newTable[index];
newTable[index] = current;
current = next;
}
}
table = newTable;
tableSize = newSize;
}
//returns true when e is in the set, otherwise returns false
publicboolean find(Integer e)
{
intindex = hash(e);
Node current = table[index];
while (current != null) {
if (current.element.equals(e)) {
returntrue;
}
current = current.next;
}
returnfalse;
}
//if e is not in the set add e to the set otherwise the set does not change
//if after adding the new element numElements > 2*tableSize then call resize
publicvoid addElement(Integer e)
{
intindex = hash(e);
Node current = table[index];
while (current != null) {
if (current.element.equals(e)) {
return; // already in the set
}
current = current.next;
}
// not found, add to the set
Node newNode = new Node(e, table[index]);
table[index] = newNode;
numElements++;
if (numElements > 2 * tableSize) {
resize();
}
}
//returns a string representation for the set
//the string representation of the set is { followed by a comma delimiter list of set
//elements followed by a }. The string for the empty set is {}
//For example, {1,2,3}.
//Note that you SHOULD NOT have any spaces in your String
/*
* Example:
* table[0]: 2 -> 4
* table[1]: 1 -> 3
*
* The string representation of this set is {2,4,1,3}
*/
//toString method
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
MySetIterator iter = new MySetIterator();
while (iter.hasNext()) {
sb.append(iter.next());
if (iter.hasNext()) {
sb.append(",");
}
}
sb.append("}");
returnsb.toString();
}
publicclass MySetIterator implements Iterator<Integer> {
//implements an iterator for the set
privateintcurrentList;
private Node currentNode;
//helper method that finds the next non empty bucket
privatevoid nextList() {
while (currentList < tableSize && table[currentList] == null) {
currentList++;
}
currentNode = (currentList < tableSize) ? table[currentList] : null;
}
public MySetIterator() {
currentList = 0;
nextList();
}
//returns true if the iteration has more elements.
publicboolean hasNext() {
returncurrentNode != null;
}
//Returns the next element in the iteration.
public Integer next() {
Integer rVal = currentNode.element;
if (currentNode.next != null) {
//what should the currentNode be?
currentNode = currentNode.next;
}
else {
//No more elements in the current bucket
//I need to get the next bucket
currentList++;
nextList();
}
returnrVal;
}
}
public Iterator<Integer> iterator() {
//returns an iterator for the set
returnnew MySetIterator();
}
class Main {
publicstaticvoid main(String[] args) {
MySet mySet = new MySet(10);
// Add elements to the set
mySet.addElement(1);
mySet.addElement(2);
mySet.addElement(3);
mySet.addElement(4);
mySet.addElement(5);
// Print the set
System.out.println("Set: " + mySet.toString());
// Find elements in the set
System.out.println("Find 3: " + mySet.find(3)); // Should return true
System.out.println("Find 6: " + mySet.find(6)); // Should return false
// Add more elements to trigger resize
mySet.addElement(6);
mySet.addElement(7);
mySet.addElement(8);
mySet.addElement(9);
mySet.addElement(10);
mySet.addElement(11);
// Print the set after resizing
System.out.println("Set after resizing: " + mySet.toString());
}
}
Based on the given code, Please help me to answer the (attached Picture) question.
Consider the lines of code below.
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps
- What does the below code do? public class Node<T> { private T data; private Node<T> node; public Node(T data) { this.data = data; } public Node<T> getNode() { return node; } public void setNode(Node<T> node) { this.node = node; } public T getData() { return data; } public void setData(T data) { this.data = data; } @Override public String toString() { return data+" "; } @Override public int hCode() { final int pm = 31; int res = 1; res = pm * res + ((data == null) ? 0 : data.hCode()); res = pm * res + ((node == null) ? 0 : node.hCode()); return res; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null) return false; if(!(obj instanceof Node)) return false; Node other = (Node) obj; if(data == null) { if(other.data != null) return false; } else if(!data .equals(other.data)) return false; if(node == null) { if(other.node != null) return…arrow_forwardImplement class “LinkedList” which has two private data members head: A pointer to the Node class length: length of the linked listImplement the following private method:1. Node* GetNode(int index) const;A private function which is only accessible to the class methods. . For example, index 0 corresponds to the head and index length-1 corresponds to end node of the linked list. The function returns NULL if the index is out of bound.Implement the following public methods:2. LinkedList();Constructor that sets head to NULL and length equal to zero.3. bool InsertAt(int data, int index);Insert a new node at the index. Return true if successful, otherwise,return false. The new node should be at the position “index” in the linked list after inserting it. You might have to use GetNode private function. these 3 partsarrow_forwardCreate a SameItem Interface: The SameItem interface will be a generic interface.SameItem will have a single method: isSameAs(T) that returns a booleanarrow_forward
- 1- A new static method with one parameter which is a head node for a linked list of integers, the method should create a new linked list which is equivalent to the original list of integers but with all repetitions removed, the method's return value is a head reference for the new list. this is what I have but it's not removing repetitions. public static IntLinkedBag removeRepetition (IntLinkedBag b1) { IntNode next; IntNode head = null; LinkedList<Integer> s = new LinkedList<>(); IntNode new_list = new IntNode(0, null); IntNode now = head; IntNode prev = new_list; while (now!= null) { int x = now.data; if (s.contains(x)) { new_list = new_list.next; } else { s.add(x); new_list.next = new IntNode(x, now); new_list = new_list.next; } now = now.next; } return b1; } }…arrow_forward2. Consider a recursive version of insertion sort that goes like this: To sort a list of size n, sort the last n 1 elements recursively, then deal with the first element. - (a) Write down the algorithm. (b) Write down the recurrence relation for the running time of your algorithm. (c) Solve the recurrence relation with full details and thus give the running time of your algorithm in notation.arrow_forwardIn Java. The following is a class definition of a linked list Node:class Node{int info;Node next;}Show the instructions required to create a linked list that is referenced by head and stores in order, the int values 13, 6 and 2. Assume that Node's constructor receives no parameters.arrow_forward
- public class HashIntSet { private class Node{ public int data; public Node next; public Node(int value) { data = value; next = null; } public Node(int value, Node next) { data = value; this.next = next; } } private Node[] elements; private int size; public HashIntSet() { elements = new Node[10]; size = 0; } public int hash(int i) { return (Math.abs(i) % elements.length); } public void add(int value) { if(!contains(value)) { int h = hash(value); Node newNode = new Node(value); newNode.next = elements[h]; elements[h] = newNode; size++; } } public boolean contains(int value) { Node current = elements[hash(value)]; while(current != null) { if(current.data == value) { return true; } current = current.next; } return false; } public String toString() { String s = ""; for(Node n:elements) { Node current = n; while(current != null) { s += current.data + " "; current = current.next; }…arrow_forwardJAVA plese Implement the indexOf method in the LinkedIntegerList class public int indexOf(int value); /** * Returns whether the given value exists in the list. * @param value - value to be searched. * @return true if specified value is present in the list, false otherwise. */ } public static void main(String[] args) { // TODO Auto-generated method stub SimpleIntegerListADT myList = null; System.out.println(myList); for(int i=2; i<8; i+=2) { myList.add(i); } System.out.println(myList.indexOf(44));arrow_forwardcomplete all the implementation of the member functions listed in the class interface for the following header file. #ifndef _LINKEDSTACK#define _LINKEDSTACK #includeusing namespace std;templateclass LinkedStack{private:Node *top; Node *getCurrentTop() { return top; }public: Stack(); bool isEmpty(); bool push(ItemType newItem); bool pop(); ItemType peek(); void clean(); bool display();};#endifarrow_forward