#include #include #include #define max_hash 29 void menu(void); int get_input(void); void insert_record(void); void delete_record(void); void find_record(void); int table_check(int); void output_table(void); int hash_table[max_hash]={0}; int hash_value=0, count=0; int main(void) { int choice=0; menu(); do { choice=get_input(); if (choice == 1) { insert_record(); output_table(); } else if (choice == 2) find_record(); else if (choice == 3) delete_record(); else if (choice == 4) menu(); else if (choice ==5) { printf("\n\nGoodbye.\n\n"); return(0); } else printf("\n\nOption not defined. Choose 4 for a list of options"); } while(choice!=5); return(0); } void menu(void) { printf("\n\n***Hash Table Manipulation***"); printf("\n\nNote: Size of Table is %d", max_hash); printf("\n\nOptions:"); printf("\n1 - Insert Record"); printf("\n2 - Find Record"); printf("\n3 - Delete Record"); printf("\n4 - Output This Menu"); printf("\n5 - Quit"); } int get_input(void) { int choice=0; printf("\nEnter Option: "); scanf("%d",&choice); return(choice); } void insert_record(void) { int hash=0; printf("\nEnter a value to insert into the table: "); scanf("%d",&hash_value); hash=hash_value%max_hash; printf("\nhash: %d", hash); do { if(table_check(hash)==1) { hash_table[hash]=hash_value; break; } else hash++; } while((table_check(hash)==0) || (hash <= max_hash)); } void delete_record(void) { int del_num=0, del_place=0; printf("\n\nEnter number to delete in table: "); scanf("%d",&del_num); del_place=del_num%max_hash; do { if(hash_table[del_place]==del_num) { hash_table[del_place]=-1; printf("%d deleted.\n",del_num); output_table(); break; } else { del_place++; } }while(count!=max_hash); } void find_record(void) { int find_num=0, find_place=0, count=1; printf("\n\nEnter number to find in table: "); scanf("%d",&find_num); find_place=find_num%max_hash; do { if(hash_table[find_place]==find_num) { printf("%d found at bucket %d in %d trys", find_num, find_place, count); break; } else { count++; find_place++; } }while(count!=max_hash); } int table_check(int hash) { if (hash_table[hash]>0) return(0); else return(1); } void output_table() { int i=0; printf("\n\nCurrent State of the table:\n\n"); for (i=0;i<=max_hash-1;i++) if (hash_table[i]==-1) printf("D "); else printf("%d ",hash_table[i]); }