Example #1
0
        /// <summary>
        ///
        /// </summary>
        static void HashSearchTest()
        {
            //哈希表的设计函数——除法取余法
            int hashLength = 13;
            //原数据
            List <int> list = new List <int>()
            {
                13, 29, 27, 28, 26, 30, 38
            };

            //哈希表长度
            int[] hash = new int[hashLength];
            //创建hash
            for (int i = 0; i < list.Count; i++)
            {
                HashSearch.InsertHash(hash, hashLength, list[i]);
            }

            Console.WriteLine("Hash数据:" + string.Join(",", hash));

            while (true)
            {
                Console.WriteLine("\n请输入要查找数字:");
                int result = int.Parse(Console.ReadLine());
                var index  = HashSearch.SearchHash(hash, hashLength, result);

                if (index != -1)
                {
                    Console.WriteLine("数字" + result + "在索引的位置是:" + index);
                }
                else
                {
                    Console.WriteLine("呜呜," + result + " 在hash中没有找到!");
                }
            }
        }