implement the Method public void concat(ChainingHashTable hashTable) {} to concat the HashTable and test it public class

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

implement the Method public void concat(ChainingHashTable hashTable) {} to concat the HashTable and test it public class

Post by answerhappygod »

implement the Method
public void concat(ChainingHashTable hashTable) {}
to concat the HashTable and test it
public class ChainingHashTable
{
private SortedList[] hashArray; // array
of lists
private int arraySize;

/*
* public void
concat(ChainingHashTable hashTable) {}
*/
//
-------------------------------------------------------------
public ChainingHashTable(int size)
// constructor
{
arraySize = size;
hashArray = new
SortedList[arraySize]; // create array
for(int j=0; j<arraySize;
j++) // fill array

hashArray[j] = new SortedList(); // with
lists
}
//
-------------------------------------------------------------
public double loadFactor()
{
double c=0;
for (int
i=0;i<arraySize;i++)

c=c+hashArray.size();
return c/arraySize;
}
//
-------------------------------------------------------------
public void rehash()
{
SortedList
oldArray[]=hashArray;
arraySize*=2;
hashArray=new
SortedList[arraySize];
for (int
i=0;i<arraySize;i++)

hashArray=new SortedList();
for(int
i=0;i<arraySize/2;i++)
{
Student
t=oldArray.removeFirst();
while
(t!=null)
{

insert(t);

t=oldArray.removeFirst();
}
}
}
//
-------------------------------------------------------------
public void displayTable()
{
for(int j=0; j<arraySize;
j++) // for each cell,
{

System.out.print(j + ".\n"); // display cell number

hashArray[j].displayList(); // display list

System.out.println("");
}
}
//
-------------------------------------------------------------
public int hashFunc(int key)
// hash function
{
return key % arraySize;
}
//
-------------------------------------------------------------
public void insert(Student s)
{
int key = s.getId();
int hashVal = hashFunc(key);
// hash the key
hashArray[hashVal].insert(s);
// insert at hashVal
} // end insert()
//
-------------------------------------------------------------
public void delete(int key)
// delete a node
{
int hashVal = hashFunc(key);
// hash the key

hashArray[hashVal].delete(key); // delete the node
} // end delete()
//
-------------------------------------------------------------
public Student find(int key)

{
int hashVal = hashFunc(key);

Student s =
hashArray[hashVal].find(key);
return s;

}
}
public class Node{
private Student element;
private Node next;
public Node(Student e, Node n)
{
element=e;
next=n;
}
public Student getElement(){
return element;
}
public Node getNext() {
return next;
}
public void setNext(Node n)
{
next=n;
}
}
public class SortedList {
private Node head=null;
private Node tail=null;

public int size()
{
int c=0;
Node current=head;
while (current!=null)
{
c++;

current=current.getNext();
}
return c;
}
public Student removeFirst()
{
if (head!=null)
{
Student
tmp=head.getElement();

head=head.getNext();
return
tmp;
}
else
return
null;
}
public void insert(Student s) //insert in
ordered list.
{
int key = s.getId();
Node previous =
null;
Node current = head;;
while(current != null
&& key > current.getElement().getId())
{
previous =
current;
current =
current.getNext();
}

if(previous==null)
head = new
Node(s,head);
else

previous.setNext(new Node(s,current));
}
public Student find(int key)
{
Node current=head;
while (current!=null
&& current.getElement().getId()<=key)
{
if
(current.getElement().getId()==key)

return current.getElement();

current=current.getNext();
}
return null;
}
public void delete(int key)
{
Node previous =
null;
Node current = head;
while(current != null
&& key != current.getElement().getId())
{
previous =
current;
current =
current.getNext();
}
if(previous==null) // if
beginning of list
head =
head.getNext(); // delete first node
else // not at
beginning

previous.setNext(current.getNext());
}
public void displayList()
{
Node current=head;
while (current!=null)
{

System.out.println(current.getElement());

current=current.getNext();
}
}
}
public class Student {
private int id;
private String name;
private double GPA;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
public Student(int id, String name, double gPA)
{
super();
this.id = id;
this.name = name;
GPA = gPA;
}
public String toString()
{
return "ID: "+ id+ ", Name:
"+name;
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply