public void findDrink(string name) { int index = Hash(name); bool foundName = false; string drink = ""; HT_Item p = hashTable[index]; while (p != null) { if (p.name == name) { drink = p.drink; foundName = true; break; } p = p.next; } if (foundName) { Console.WriteLine("{0}'s favorite drink = {1}", name, drink); } else { Console.WriteLine("{0}'s info was not found in the Hash Table"); } }
public HashTable(int size) { tableSize = size >= 0 ? size : size * -1; hashTable = new HT_Item[tableSize]; for (int i = 0; i < tableSize; ++i) { hashTable[i] = new HT_Item(); hashTable[i].name = ""; hashTable[i].drink = ""; hashTable[i].next = null; } }
public int numberOfItemsInIndex(int index) { int count = 0; if (hashTable[index].name.Length == 0 && hashTable[index].next == null) { return(count); } else { ++count; HT_Item p = hashTable[index]; while (p.next != null) { p = p.next; ++count; } } return(count); }
public void printItemsInIndex(int index) { HT_Item p = hashTable[index]; if (p.name.Length == 0 && p.next == null) { Console.WriteLine("Index {0} is empty", index); } else { Console.WriteLine("Index {0} contains the following items", index); while (p != null) { Console.WriteLine("--------------"); Console.WriteLine(p.name); Console.WriteLine(p.drink); Console.WriteLine("--------------"); p = p.next; } } }
public void addItem(string name, string drink) { int index = Hash(name); if (hashTable[index].name.Length == 0) { hashTable[index].name = name; hashTable[index].drink = drink; } else { HT_Item p = hashTable[index]; HT_Item n = new HT_Item(); n.name = name; n.drink = drink; n.next = null; while (p.next != null) { p = p.next; } p.next = n; } }