Esempio n. 1
0
 public void Insert1(studentRecord newRecord)
 {
     if (n >= m / 2)
     {
         rehash(nextPrime(2 * m));
         Console.WriteLine("New hash table size is : " + m);
     }
     Insert(newRecord);
 }
Esempio n. 2
0
        public void Insert(studentRecord newRecord)
        {
            int key      = newRecord.getstudentId();
            int h        = hash(key);
            int location = h;

            for (int i = 1; i < m; i++)
            {
                if (array[location] == null || array[location].getstudentId() == -1)
                {
                    array[location] = newRecord;
                    n++;
                    return;
                }
                if (array[location].getstudentId() == key)
                {
                    throw new System.InvalidOperationException("Duplicate key");
                }
                location = (h + i) % m;
            }
            Console.WriteLine("Table is full: Record Can't be inserted ");
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            int    id, choice;
            string name;

            Console.Write("Enter intial size of table : ");
            int size = Convert.ToInt32(Console.ReadLine());

            HashTable table = new HashTable(size);

            while (true)
            {
                Console.WriteLine("1.Insert a record");
                Console.WriteLine("2.Search a record");
                Console.WriteLine("3.Delete a record");
                Console.WriteLine("4.Display table");
                Console.WriteLine("5.Exit");

                Console.Write("Enter your choice : ");
                choice = Convert.ToInt32(Console.ReadLine());

                if (choice == 5)
                {
                    break;
                }
                switch (choice)
                {
                case 1:
                    Console.Write("Enter student id : ");
                    id = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter student name : ");
                    name = Console.ReadLine();
                    studentRecord aRecord = new studentRecord(id, name);
                    table.Insert(aRecord);
                    break;

                case 2:
                    Console.Write("Enter a key to be searched : ");
                    id      = Convert.ToInt32(Console.ReadLine());
                    aRecord = table.Search(id);
                    if (aRecord == null)
                    {
                        Console.WriteLine("Key not found");
                    }
                    else
                    {
                        Console.WriteLine(aRecord.tostring());
                    }
                    break;

                case 3:
                    Console.Write("Enter a key to be deleted : ");
                    id = Convert.ToInt32(Console.ReadLine());
                    table.Delete(id);
                    break;

                case 4:
                    table.DisplayTable();
                    break;
                }
            }
        }