0% found this document useful (0 votes)
6 views3 pages

Include

This C program implements a simple hash table with basic operations such as insertion, deletion, searching, and printing the table. It uses an array of fixed size and handles collisions by indicating when an insertion fails due to a collision. The main function provides a menu-driven interface for users to interact with the hash table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Include

This C program implements a simple hash table with basic operations such as insertion, deletion, searching, and printing the table. It uses an array of fixed size and handles collisions by indicating when an insertion fails due to a collision. The main function provides a menu-driven interface for users to interact with the hash table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>

#define size 7

int arr[size];

void init()
{
int i;
for(i = 0; i < size; i++)
arr[i] = -1;
}

void insert(int value)


{
int key = value % size;

if(arr[key] == -1)
{
arr[key] = value;
printf("%d inserted at arr[%d]\n", value,key);
}
else
{
printf("Collision : arr[%d] has element %d already!\n",key,arr[key]);
printf("Unable to insert %d\n",value);
}
}

void del(int value)


{
int key = value % size;
if(arr[key] == value)
arr[key] = -1;
else
printf("%d not present in the hash table\n",value);
}

void search(int value)


{
int key = value % size;
if(arr[key] == value)
printf("Search Found\n");
else
printf("Search Not Found\n");
}
void print()
{
int i;
for(i = 0; i < size; i++)
printf("arr[%d] = %d\n",i,arr[i]);
}

int main()
{
int choice, value;

init(); // Initialize the array

while(1)
{
printf("\n--- Hash Table Menu ---\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. Print Table\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insert(value);
break;

case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
del(value);
break;

case 3:
printf("Enter value to search: ");
scanf("%d", &value);
search(value);
break;

case 4:
print();
break;

case 5:
printf("Exiting...\n");
return 0;

default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}

You might also like