Quadratic Probing Program in C

In this article, we will discuss the quadratic probing problem in C.

Description of the problem

Hash tables with quadratic probing are implemented in this C program. An associative array, a structure that can map keys to values, is implemented using a data structure called a hash table. A hash table uses a hash function to create an index into an array of slots or buckets. Linear probing and quadratic probing are comparable. The difference is that we would check 1^1=1 element, 2^2=4 elements, 3^2=9 elements, 4^2=16 elements, etc., if we attempted to insert into a slot that was already filled.

Solution for the problem:

  1. Make a hash table or an array of structures.
  2. Take as input a key and a value that will be saved in a hash table.
  3. An index will be generated that corresponds to the key; each key is put in a specific array index.
  4. Access the data contained in that array index using the produced index.
  5. If there are no data, construct one, add the data item (key and value), and increase the hash table's size.
  6. If the data is present, look for a blank space in the following items (looping back if necessary) to insert the new data item.

Note:

In this case, instead of using Linear Probing, probing will be carried out using the following formula:

(currentPosition + h) % arraySize => Linear Probing

(currentPosition + (h * h)) % arraySize => Quadratic Probing

Where h = 1, 2, 3, 4, and so on.

  1. A for loop is used to access an element at each index to display all of the hash table's elements.
  2. To remove a key from a hash table, we must first determine its index and then delete it if the key matches. If not, we must search through the elements until we discover the key or an empty area where no data has been input, which denotes that the hash table has no data.
  3. Exit.

Program for the quadratic probing:

Let's take an example to understand the use of quadratic probing in C.

Output:

Runtime test cases:

1. Inserting and Displaying Elements
Implementation of Hash Table in C with Quadratic Probing.

MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 5 25

Key (5) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 15 225

Key (15) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 4
Array[0] has no elements
Array[1] has no elements
Array[2] has no elements
Array[3] has no elements
Array[4] has no elements
Array[5] has elements
5 (key) and 25 (value)
Array[6] has no elements
Array[7] has no elements
Array[8] has no elements
Array[9] has elements
15 (key) and 225 (value)

Do you want to continue? (Press 1 for yes): 0
2. removing an element
Implementation of Hash Table in C with Quadratic Probing.

MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 5 25

Key (5) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 2
Deleting in Hash table
Enter the key to delete: 5

Key (5) has been removed

Do you want to continue? (Press 1 for yes): 0

3. Checking Hash Table Size
Implementation of Hash Table in C with Quadratic Probing.

MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 5 25

Key (5) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 15 225

Key (15) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1. Insert item in the Hash table
2. Remove item from the Hash table
3. Check the size of Hash table
4. Display Hash table
5. Exit

Please enter your choice-: 3
Size of Hash table is: 2

Do you want to continue? (Press 1 for yes): 0

Explanation:

  1. In this example, the program defines two structures: struct hashtable_item to represent objects in the hash table and struct item to hold key-value pairs. Array, a pointer to the hash table, size to keep track of the number of entries, and max for hash table size are examples of global variables.
  2. The hashcode function determines the hash index by performing a modulo operation on the key. This index determines the position of an item in the array of the hash table.
  3. The hash table array is initialized by the init_array All flags are set to 0 (which denotes an empty slot), and data pointers are set to NULL.
  4. The insert operation increases the hash table's inventory. The hash function is used to determine the index, and quadratic probing (growing by squared values of h) is used to locate an open slot. A new item is inserted if the slot is vacant. The value is updated if the key already exists. A suitable message is presented if the hash table is full.
  5. The hash table's contents are removed using the remove_element It uses the hash function to determine the index and quadratic probing to determine the location of the key. If the key is discovered, the object is eliminated by altering the size, changing the flag, and freeing up memory. The proper notifications are presented if the table is full or the key doesn't exist.
  6. The display function does print if each slot has elements after iterating through the hash table array. If an element is present, its key and value are printed.
  7. The size_of_hashtable function returns the hash table's current size (the number of currently stored elements).
  8. The hash table is initialized by the main function using the init_array. The user is presented with a menu that includes choices to add and remove items, verify the size, see the hash table, and exit. The user is given the option to continue or quit when the relevant function is run based on their selection. Memory is set aside for items, and the array is released after the user logs out.