示例#1
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 ");
        }
示例#2
0
        public studentRecord Delete(int key)
        {
            int h        = hash(key);
            int location = h;

            for (int i = 1; i < m; i++)
            {
                if (array[location] == null)
                {
                    return(null);
                }
                if (array[location].getstudentId() == key)
                {
                    studentRecord temp = array[location];
                    array[location].setstudentId(-1);
                    n--;
                    return(temp);
                }
                location = (h + i) % m;
            }
            return(null);
        }
示例#3
0
        static void Main(string[] args)
        {
            int    id, choice;
            String name;

            Console.Write("Enter initial 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.WriteLine("Enter your choice : ");
                choice = Convert.ToInt32(Console.ReadLine());

                if (choice == 5)
                {
                    break;
                }

                switch (choice)
                {
                case 1:
                    Console.WriteLine("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\n");
                    }
                    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;
                }
            }
        }