public void put(string keyString, int value) { int key = keyString.GetHashCode(); int hash = (key % TABLE_SIZE); if (hash < 0) //if the has value is negative, make it positive { hash *= -1; } if (table[hash] == null) { table[hash] = new LinkedHashEntry(key, value); } else { LinkedHashEntry entry = table[hash]; while (entry.getNext() != null && entry.getKey() != key) { entry = entry.getNext(); } if (entry.getKey() == key) { entry.setValue(value); } else { entry.setNext(new LinkedHashEntry(key, value)); } } }
public void remove(string userKey) { int key = userKey.GetHashCode(); int hash = (key % TABLE_SIZE); if (table[hash] != null) { LinkedHashEntry prevEntry = null; LinkedHashEntry entry = table[hash]; while (entry.getNext() != null && entry.getKey() != key) { prevEntry = entry; entry = entry.getNext(); } if (entry.getKey() == key) { if (prevEntry == null) { table[hash] = entry.getNext(); } else { prevEntry.setNext(entry.getNext()); } } } }
//key will be the name of the food converted to ASCII, value will be quanitity of food as an int public int get(string keyString) { int key = keyString.GetHashCode(); //hash fuction using int hash = (key % TABLE_SIZE); if (table[hash] == null) { return(-1); } else { LinkedHashEntry entry = table[hash]; while (entry != null && entry.getKey() != key) { entry = entry.getNext(); } if (entry == null) { return(-1); } else { return(entry.getValue()); } } }
//add print method that prints all values in hash table, unsorted public void print() { LinkedHashEntry entry = table[0]; while (entry.getNext() != null) { entry = entry.getNext(); Console.WriteLine(entry); } }
static void Main(string[] args) { LinkedHashEntry lhe = new LinkedHashEntry(); //why does this exist here HashMap hm = new HashMap(); //add to hashtable hm.put("cereal", 10); hm.put("applesauce", 3); hm.put("juice box", 12); //hm.printSorted(); //hm.print(); //for testing User_Interface ui = new User_Interface(); ui.program(); }
//find method will find the key and will help determine if we should increment the value of a key or add a new item public bool find(string userKey) { int key = userKey.GetHashCode(); int hash = (key % TABLE_SIZE); if (hash < 0) //if the has value is negative, make it positive { hash *= -1; } if (table[hash] != null) { LinkedHashEntry entry = table[hash]; while (entry.getNext() != null && entry.getKey() != key) { entry = entry.getNext(); } if (entry.getKey() == key) { //increments value by one int value = entry.getValue(); entry.setValue(value++); return(true); } else { return(false); } } else { return(false); } }
//method puts has table elements into an array and then calls mergesort public void printSorted() { LinkedHashEntry entry = table[0]; int[] toBeSorted = new int[TABLE_SIZE]; //array to store hashtable values int count = 0; //keeps track of array placement //adds all elements into an array while (entry != null && entry.getNext() != null) { toBeSorted[count] = entry.getValue(); entry = entry.getNext(); count++; } //sorts array Mergesort m = new Mergesort(); int left = 0; int right = toBeSorted.Length; int middle = (left + right) / 2; m.MergeSortMain(toBeSorted, left, right, middle); //prints entries for (int i = 0; i < toBeSorted.Length; i++) { Console.WriteLine(toBeSorted[i]); } /* while (entry.getNext() != null) * { * entry = entry.getNext(); * Console.WriteLine(entry); * } */ }
public void setNext(LinkedHashEntry next) { this.next = next; }
LinkedHashEntry(string key, int value) { this.key = key; this.value = value; this.next = null; }