Question: package application; import java.util.LinkedList; import java.util.List; / / Hash Table Class public class Table { int size; / / Number of vertices. LinkedList [ ]

package application;
import java.util.LinkedList;
import java.util.List;
// Hash Table Class
public class Table {
int size; // Number of vertices.
LinkedList[] table; // This table contains the vertices Not adjacent.
// Non-Argument Constructor.
public Table(){
}
// Argument Constructor.
public Table(int size){
this.size = size; // Table Size.
table = new LinkedList[size]; // Main Table
for (int i =0; i < size; i++){
table[i]= new LinkedList<>();// Each Cell [Vertex] has a linkedList
}
}
// Aim: Add the vertices inside the Main Table.
public void addVertex(Vertex vertex){
int index = hashFunction(vertex.name); // Where is the Vertex should store ?
table[index].add(vertex); // Vertex not adjacent.
}
// Search on specific vertex using its name.
public Vertex SearchAndgetVertex(String name){
int index = hashFunction(name);
for (int i =0; i < table[index].size(); i++){
if (table[index].get(i).name.equals(name)){
return table[index].get(i);
}
}
return null; // This Vertex not found inside the table.
}
// Adding the adjacent
public void addAdjacent(String vertexName, Edge adjacentVertex){
Vertex vertex = SearchAndgetVertex(vertexName);
if (vertex != null){
vertex.addAdjacent(adjacentVertex);
}else {
System.out.println("This Vertex not found!!");
}
}
// Return all vertices
public LinkedList getAllVertices(){// as copy.
return table;
}
// Adjacent of specific Vertex.
public List getAdjacent(String vertexName){
Vertex vertex = SearchAndgetVertex(vertexName);
if (vertex != null){
return vertex.getAdjacentEdges(); // From vertex class.
}else {
return new LinkedList<>();
}
}
// Aim: Produce a fixed size hash value. Unique representation of the input
private int hashFunction(String name){
return name.length()% size;
}
}// I want array of Vertex the hash table not array of linked list

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!