Design a primary hash table data structure. a. Each hash table has an array of n
ID: 3810812 • Letter: D
Question
Design a primary hash table data structure. a. Each hash table has an array of node pointers and can point to a head of a linked list. Suppose the node class is given to you. The data type of each node is int. b. Override the default constructor with a constructor that has one default parameter called capacity. What happens to your default constructor? Create some instances of the sequence class using the new constructor in multiple ways. c. Design the insert function that accept a value and inserts the value in the index of value%(size of table). Try to be efficient. d. Design a search function that accepts a value and checks to see if it is inside the table, if it is there it return true. e. Design a remove function to remove a value from the table. f. Overload + and - operators for hash table such that you may insert or remove a new integer to the hash table using this operator. g. Overload >> operator for hash table such that it accepts a hash table and it inserts the element from the input into the table. h. Explain why the value semantics of your hash table class is not safe to use? Make the value semantics safe to use. I. Explain what is the running time of each function.Explanation / Answer
public class HashTable {
public static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
private int TABLE_SIZE;
private Node[] table;
// constructor
//Note default constructor will not be valid anymore
public HashTable(int capacity){
TABLE_SIZE = capacity;
table = new Node[TABLE_SIZE];
for(int i=0;i<TABLE_SIZE;i++)
table[i] = null;
}
public void insert(int data){
int index = data%TABLE_SIZE;
Node node = new Node(data);
node.next = null;
if(table[index]==null){
table[index] = node;
}else{
Node root = table[index];
Node previous = root;
while(root!=null){
previous = root;
root = root.next;
}
previous.next = node;
}
}
public boolean contains(int data){
int index = data % TABLE_SIZE;
Node root = table[index];
while(root!=null){
if(root.data == data)
return true;
root = root.next;
}
return false;
}
}
// Above is the code for first four subpart of given question
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.