I am working on an assignment where integers are fed into the app, the integers are hashed, then placed into an array. Each position in the array is a Linked List which I have written. My issue is that I cannot seem to specify which position in the array the integers should go. At present, my output is the last five integers to be placed in for every location in the array except position 10, which is null. Any help would be greatly appreciated. Thank you.
public class MyHashTab {
private static MyList last;
private static MyList first;
public MyHashTab(int initialCapacity, MyList[] anArray) {
}
public static void insert(int searchKey, MyList[] anArray) {
int hash = searchKey % anArray.length;
MyList current = new MyList(searchKey);
current.next = null;
if (anArray[hash] == null) {
current.next = null;
first = current;
last = current;
anArray[hash] = current;
} else {
last.next = current;
last = current;
}
}
public static void printHash(MyList[] anArray) {
System.out.println("The generated hash table with separate chaining is: ");
for (int i = 0; i < anArray.length; i++) {
if (anArray[i] == null) {
System.out.println("\nThe items for index[" + i + "]: ");
i++;
}
System.out.print("\nThe items for index[" + i + "]: ");
MyList temp = first;
while (temp != null) {
System.out.print(temp.iData + "\t");
temp = temp.next;
}
}
}
}
public class MyList {
int iData; // This integer is used as a key value, and as a way to see the actual node instead of it's memory address.
MyList next; // This is a pointer to a nodes right child.
public MyList(int searchKey) {
this.iData = searchKey;
}
}
I believe the issue lies in the else statement starting at line 26. I am not assigning which linked list to make the new integer part of. Is there a correct way to write,
anArray[hash].last.next = current;
anArray[hash].last = current;
I added print statements to both the if and the else statements and I am using both. Thank you.
Output
The generated hash table with separate chaining is:
The items for index[0]: 366 976 312 244 655
The items for index[1]: 366 976 312 244 655
The items for index[2]: 366 976 312 244 655
The items for index[3]: 366 976 312 244 655
The items for index[4]: 366 976 312 244 655
The items for index[5]: 366 976 312 244 655
The items for index[6]: 366 976 312 244 655
The items for index[7]: 366 976 312 244 655
The items for index[8]: 366 976 312 244 655
The items for index[9]: 366 976 312 244 655
The items for index[10]:
The items for index[11]: 366 976 312 244 655
The expected output should be something like this. The generated hash table with separate chaining is:
The items for index [0]: 36 60 108 312
The items for index [1]: 85
The items for index [2]: 290 422
The items for index [3]: 99 135
The items for index [4]: 76 148 244 568 976
The items for index [5]: 29 173 245
The items for index [6]: 366
The items for index [7]: 619 655 703
The items for index [8]: 56
The items for index [9]: 345
The items for index [10]:
The items for index [11]: 23 47
Each integer placed in the arraylist at the proper position after being hashed.