Quadratic Probing Program in CIn this article, we will discuss the quadratic probing problem in C. Description of the problemHash 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:
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.
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:
Next TopicC Programming Test |