2.1 Hashing With Chaining We search/insert/delete in a hashtable in the following way. First use the getHashValue method to get the hash value. Now use this hash value to get hold of a hash table entry, which is a linked list. These functions have been written and you do not need to modify them: • gethashValue: Uses the hash function (37 ∗ val + 61)%T ABLE SIZE. • getList: The hashtable is an array of linked list. So, this method simply returns the linked list at a particular index of the hash table. We will use the inbuilt linked-list of C++ or Java. This is an implementation of a doubly- linked-list (with links going both forward and backward). It supports all the standard operations (inserting at front or end, deleting head or tail, traversing the list, etc.) In the next sections (Java), I’ll highlight some of the usages (not all may be required). Since we will deal with integers, I will only discuss integer linked lists, but lists of any type can be created. 2.3 Java • Syntax to create an integer list: LinkedList nameOfList = new LinkedList(); • To get the size of the list, the syntax is: nameOfList.size(); • To add a number at the end, the syntax is: nameOfList.addLast(15); To add a number at the beginning, the syntax is: nameOfList.addFirst(15); • To remove the last number, the syntax is: nameOfList.removeLast(); To remove the first number, the syntax is: nameOfList.removeFirst(); To traverse the list (say for retrieving a value or searching or printing or deleting), one can use an iterator as shown next. Here, print prints the list and remove removes the node at index. static void print(LinkedList numbers) { // printing the content Iterator it = numbers.iterator(); while (it.hasNext()) System.out.print(it.next() + " "); } static void remove(LinkedList numbers, int index) { // remove node at index if (numbers.size() == 0) // get the size of the list return; // nothing to remove if (index == 0) numbers.removeFirst(); // remove node at index 0 else if (index == numbers.size() - 1) numbers.removeLast(); // remove node at last index else { int i = 0; Iterator it = numbers.iterator(); while (it.hasNext()) {it.next(); if (i == index) { // if i = index, we are at desired node it.remove(); // delete current node return; } i++; } } } • Iterator it = numbers.iterator(); declares an iterator it on the list numbers and the iterator points to the first number on the list. • while (it.hasNext()) ensures that the iterator traverses until the last node. • it.next() retrieves the value of the node at which the iterator is pointing as well as moves the iterator to the next node. Thus System.out.print(it.next()) prints the value of the node at which the iterator is pointing and moves the iterator to the next node. • it.remove() deletes the current node, i.e., the node at index. 2.4 Few things that you should know but not use for this assignment Both in C++ and Java, there are inbuilt functions that allow you to search a list for a particular number or remove an occurrence of a particular number. However, these are riddled with issues or totally necessary for our purposes because of the overhead involved. For example, to remove an number in Java, one has to create an Integer object for the number and then use it as argument to the remove method. In C++, one has to use the find method in the algorithms header to get an iterator to the occurrence of a number, and then remove the iterator. Remember that over here we want to learn the usage of iterator; so, you are prohibited to use in-built methods other than the ones mentioned previously. 2.5 Pseudo-code search • First obtain the hash value for the key using getHashValue function. • Use getList to get the linked list from the hashT able[ ] for this hash value. • Now, iterate through this linked list using an iterator. If the iterator’s value equals key, then the list contains the key; so, return true. • Once the iteration completes, return f alse. insert • Remember that your hash table should contain a number only once. So, first use search to check if the hash table already contains val. If it does, then return f alse. • Obtain the hash value for val using getHashValue function. • Use getList to get the linked list from the hashT able[ ] for this hash value. • Now, insert the value at the end of the linked list, and return true. remove • First obtain the hash value for val using getHashValue function. • Use getList to get the linked list from the hashT able[ ] for this hash value. • Now, iterate through this linked list using an iterator. If the iterator’s value equals val, then the list contains val; so, delete using the iterator and return true. • Once the iteration completes, return false. What I have for Code so far is in image that is attached.
2.1 Hashing With Chaining
We search/insert/delete in a hashtable in the following way. First use the getHashValue method
to get the hash value. Now use this hash value to get hold of a hash table entry, which is a linked
list. These functions have been written and you do not need to modify them:
• gethashValue: Uses the hash function (37 ∗ val + 61)%T ABLE SIZE.
• getList: The hashtable is an array of linked list. So, this method simply returns the linked
list at a particular index of the hash table.
We will use the inbuilt linked-list of C++ or Java. This is an implementation of a doubly-
linked-list (with links going both forward and backward). It supports all the standard operations
(inserting at front or end, deleting head or tail, traversing the list, etc.) In the next sections (Java), I’ll highlight some of the usages (not all may be required). Since we will deal with integers,
I will only discuss integer linked lists, but lists of any type can be created.
2.3 Java
• Syntax to create an integer list: LinkedList<Integer> nameOfList = new LinkedList<Integer>();
• To get the size of the list, the syntax is: nameOfList.size();
• To add a number at the end, the syntax is: nameOfList.addLast(15); To add a number at
the beginning, the syntax is: nameOfList.addFirst(15);
• To remove the last number, the syntax is: nameOfList.removeLast(); To remove the first
number, the syntax is: nameOfList.removeFirst();
To traverse the list (say for retrieving a value or searching or printing or deleting), one can use
an iterator as shown next. Here, print prints the list and remove removes the node at index.
static void print(LinkedList<Integer> numbers) { // printing the content
Iterator<Integer> it = numbers.iterator();
while (it.hasNext())
System.out.print(it.next() + " ");
}
static void remove(LinkedList<Integer> numbers,
int index) { // remove node at index
if (numbers.size() == 0) // get the size of the list
return; // nothing to remove
if (index == 0)
numbers.removeFirst(); // remove node at index 0
else if (index == numbers.size() - 1)
numbers.removeLast(); // remove node at last index
else {
int i = 0;
Iterator<Integer> it = numbers.iterator();
while (it.hasNext()) {it.next();
if (i == index) { // if i = index, we are at desired node
it.remove(); // delete current node
return;
}
i++;
}
}
}
• Iterator<Integer> it = numbers.iterator(); declares an iterator it on the list numbers
and the iterator points to the first number on the list.
• while (it.hasNext()) ensures that the iterator traverses until the last node.
• it.next() retrieves the value of the node at which the iterator is pointing as well as moves
the iterator to the next node. Thus System.out.print(it.next()) prints the value of the
node at which the iterator is pointing and moves the iterator to the next node.
• it.remove() deletes the current node, i.e., the node at index.
2.4 Few things that you should know but not use for this assignment
Both in C++ and Java, there are inbuilt functions that allow you to search a list for a particular
number or remove an occurrence of a particular number. However, these are riddled with issues
or totally necessary for our purposes because of the overhead involved. For example, to remove an
number in Java, one has to create an Integer object for the number and then use it as argument to
the remove method. In C++, one has to use the find method in the
iterator to the occurrence of a number, and then remove the iterator.
Remember that over here we want to learn the usage of iterator; so, you are prohibited to use
in-built methods other than the ones mentioned previously.
2.5 Pseudo-code
search
• First obtain the hash value for the key using getHashValue function.
• Use getList to get the linked list from the hashT able[ ] for this hash value.
• Now, iterate through this linked list using an iterator.
If the iterator’s value equals key, then the list contains the key; so, return true.
• Once the iteration completes, return f alse.
insert
• Remember that your hash table should contain a number only once. So, first use
search to check if the hash table already contains val. If it does, then return f alse.
• Obtain the hash value for val using getHashValue function.
• Use getList to get the linked list from the hashT able[ ] for this hash value.
• Now, insert the value at the end of the linked list, and return true.
remove
• First obtain the hash value for val using getHashValue function.
• Use getList to get the linked list from the hashT able[ ] for this hash value.
• Now, iterate through this linked list using an iterator. If the iterator’s value equals
val, then the list contains val; so, delete using the iterator and return true.
• Once the iteration completes, return false.
What I have for Code so far is in image that is attached.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 5 images