예제 #1
0
        public bool AddItem(string key, string value)
        {
            int hashIndex = Hash(key, tableSize);

            Console.Write($"Key {key} hashes to {hashIndex,2}.  ");
            if (HashLinkedList[hashIndex] == null)  // null value means this slot is empty, so we can write our data (now a string) here.
            {
                HashLinkedList[hashIndex] = new LinkedList <KeyAndValue>();
            }
            else
            {
                Console.WriteLine($"<<< COLLISION! But don't worry, we'll take care of it!"); // else this spot was used and we will lose this data!
                collisions++;
            }
            var newValue = new KeyAndValue(key, value);

            HashLinkedList[hashIndex].AddLast(newValue);
            Console.WriteLine($"Key {key} value {value} saved.");
            return(true);
        }
예제 #2
0
        public bool AddItem(string key, string value)
        {
            int         hashIndex = Hash(key, tableSize);
            KeyAndValue kvp;

            Console.Write($"Key {key} hashes to {hashIndex,2}.  ");
            if (hashTable[hashIndex] == null)  // null value means this slot is empty, so we can write our data (now a string) here.
            {
                hashTable[hashIndex] = new LinkedList <KeyAndValue>();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine($"<<< COLLISION! But we'll take care of it."); // else this spot was used
                Console.ResetColor();
                collisions++;
            }
            kvp = new KeyAndValue(key, value);
            hashTable[hashIndex].AddLast(kvp);
            Console.WriteLine($"key {key}, Value {value} stored.");
            return(true);
        }