Ejemplo n.º 1
0
        public StudentRecord Delete(int key)
        {
            int h        = hash(key);
            int location = h;

            for (int i = 0; i < length; i++)
            {
                if (Records[location] == null)
                {
                    return(null);
                }

                if (Records[location].StudentId == key)
                {
                    StudentRecord temp = Records[location];
                    Records[location].StudentId = -1;
                    NumOfRecords--;
                    return(temp);
                }
                location = (h + i) % length;
            }
            return(null);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            int    id, choice;
            string name;

            Console.Write("Enter initial size of the 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. Break");

                Console.WriteLine("Enter a  choice");
                choice = Convert.ToInt32(Console.ReadLine());

                switch (choice)
                {
                case 1:
                    Console.WriteLine("Enter  Student ID");
                    id = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter Student Name");
                    name = Console.ReadLine();

                    StudentRecord rec = new StudentRecord(id, name);
                    table.Insert(rec);
                    break;

                case 2:
                    Console.WriteLine("Enter a  key to be searcehd");
                    id  = Convert.ToInt32(Console.ReadLine());
                    rec = table.SearchStudent(id);
                    if (rec == null)
                    {
                        Console.WriteLine("Key not found");
                    }
                    else
                    {
                        Console.WriteLine(rec.ToString());
                    }
                    break;

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

                case 4:
                    table.DisplayTable();
                    break;

                case 5:
                default:
                    break;
                }
            }
        }