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 nowThis is a popular solution!
Step by stepSolved in 4 steps with 5 images
- Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the "right partition"; it does not need to appear between the left and right partitions.EXAMPLEInput:Output:3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [partition= 5]3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8arrow_forwardAll of these statements are related to hash function, hash table.arrow_forwardOur hash map data is saved as a LinkedList due to improper map building. This makes hash maps less helpful.arrow_forward
- In java please help with the following:arrow_forwardWe may utilise two methods without implementing them: countand unambiguous The DictionaryEntry object's count is returned via the Count method. whereas Clear removes all DictionaryEntry from the inner hash tableitems from the internal hash tableWrite a program that utilizes these methods:arrow_forwardThis is the code below: package hashset; import java.util.Iterator; public class MySet { // implements a set using a separate chaining hash table privateclass Node { private Integer element; private Node next; private Node(Integer e, Node n) { element = e; next = n; } } private Node table[]; //an array of linked list privateinttableSize; //current number of lists in the table privateintnumElements; //number of elements in the set privatefinalintprimes[] = {7, 23, 59, 131, 271, 563, 1171, 2083, 4441, 8839, 16319, 32467, 65701, 131413, 263983, 528991}; privateintprimeIndex; //last prime used privateint nextPrime(intp) { //finds the next prime from the list above //used for resizing and the initial size while (primes[primeIndex] <= p) primeIndex++; returnprimes[primeIndex]; } public MySet(ints) { //s is a hint for the initial size primeIndex = 0; tableSize = nextPrime(s); table = new Node[tableSize]; numElements = 0; } //return the hash function value for k privateint…arrow_forward
- We're dealing with a bad hash map in which all of the values are stored in the same container (that is, they are all in the same LinkedList). The purpose of a hash map has been completed.arrow_forwardJava Code: For Lexer.java Make a HashMap of <String, TokenType> in your Lex class. Below is a list of the keywords that you need. Make token types and populate the hash map in your constructor (I would make a helper method that the constructor calls). while, if, do, for, break, continue, else, return, BEGIN, END, print, printf, next, in, delete, getline, exit, nextfile, function Modify “ProcessWord” so that it checks the hash map for known words and makes a token specific to the word with no value if the word is in the hash map, but WORD otherwise. For example, Input: for while hello do Output: FOR WHILE WORD(hello) DO Make a token type for string literals. In Lex, when you encounter a “, call a new method (I called it HandleStringLiteral() ) that reads up through the matching “ and creates a string literal token ( STRINGLITERAL(hello world) ). Be careful of two things: make sure that an empty string literal ( “” ) works and make sure to deal with escaped “ (String quote = “She…arrow_forwardI am trying to implement a hash table of key-value pairs, where the key determines the location of the pair in the hash table, the key is what the hash function is used on. The hash table that I am trying to implement will use separate chaining (with unordered linked lists) for collision resolution. I need help changing the code where: 1. The "search" method of the code returns the node that was found or "None" 2. The "Node" implementation has a "key" and "value" member This is what I have so far class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class UnorderedList: def __init__(self): self.head = None def isEmpty(self): return self.head == None def add(self,item): temp = Node(item) temp.setNext(self.head)…arrow_forward
- Given the MileageTrackerNode class, complete main() to insert nodes into a linked list (using the InsertAfter() function). The first user-input value is the number of nodes in the linked list. Use the PrintNodeData() function to print the entire linked list. DO NOT print the dummy head node. Ex. If the input is: 3 2.2 7/2/18 3.2 7/7/18 4.5 7/16/18 the output is: 2.2, 7/2/18 3.2, 7/7/18 4.5, 7/16/18 #include "MileageTrackerNode.h"#include <string>#include <iostream>using namespace std; int main (int argc, char* argv[]) {// References for MileageTrackerNode objectsMileageTrackerNode* headNode;MileageTrackerNode* currNode;MileageTrackerNode* lastNode; double miles;string date;int i; // Front of nodes listheadNode = new MileageTrackerNode();lastNode = headNode; // TODO: Read in the number of nodes // TODO: For the read in number of nodes, read// in data and insert into the linked list // TODO: Call the PrintNodeData() method// to print the entire linked list //…arrow_forwardGiven the MileageTrackerNode class, complete main() to insert nodes into a linked list (using the InsertAfter() function). The first user- input value is the number of nodes in the linked list. Use the PrintNodeData() function to print the entire linked list. DO NOT print the dummy head node. Ex. If the input is: 3 2.2 7/2/18 3.2 7/7/18 4.5 7/16/18 the output is: 2.2, 7/2/18 3.2, 7/7/18 7/16/18 5arrow_forward- In class HashTable implement a hash table and consider the following:(i) Keys are integers (therefore also negative!) and should be stored in the tableint[] data.(ii) As a hash function take h(x) = (x · 701) mod 2000. The size of the table istherefore 2000. Be careful when computing the index of a negative key. Forexample, the index of the key x = −10 ish(−10) = (−7010) mod 2000 = (2000(−4) + 990) mod 2000 = 990.Hence, indices should be non-negative integers between 0 and 1999!(iii) Implement insert, which takes an integer and inserts it into a table. Themethod returns true, if the insertion is successful. If an element is already inthe table, the function insert should return false.(iv) Implement search, which takes an integer and finds it in the table. The methodreturns true, if the search is successful and false otherwise.(v) Implement delete, which takes an integer and deletes it form the table. Themethod returns true, if the deletion is successful and false otherwise.(vi)…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education