Implement Hash Table using C language
#include<stdio.h> #include<conio.h> #define MAX 8 void createwor(int [][2]); void insertwor(int [][2]); void deletwor(int [][2]); void displaywor(int [][2]); void createwr(int [][2]); void insertwr(int [][2]); void deletwr(int [][2]); void displaywr(int [][2]); void searchwr(int [][2]); void searchwor(int [][2]); void main() { int ch,ch1,i,j; int a[MAX][2],b[MAX][2]; for(i=0;i<MAX;i++) for(j=0;j<2;j++) b[i][j]=-1; for(i=0;i<MAX;i++) for(j=0;j<2;j++) a[i][j]=-1; do { clrscr(); printf("\n\t\tHASH TABLE\n\t\t1.CREATE HASH TABLE\n\t\t2.INSERT\n\t\t3.DELETE\n\t\t4.DISPLAY\n\t\t5.SEARCH\n\t\t6.EXIT"); printf("\n\t\tENTER UR CHOICE"); scanf("%d",&ch); switch(ch) { case 1: do { clrscr(); printf("CREATE HASH TABLE\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT"); printf("\n\t\tENTER UR CHOICE"); scanf("%d",&ch1); switch(ch1) { case 1: printf("WITH REPLACEMENT"); createwr(a); break; case 2: printf("WITHOUT REPLACEMENT"); createwor(b); break; case 3: break; } }while(ch1!=3); break; case 2: do { clrscr(); printf("\n\t\tINSERT\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT"); printf("\n\t\tENTER UR CHOICE"); scanf("%d",&ch1); switch(ch1) { case 1: printf("WITH REPLACEMENT"); insertwr(a); break; case 2: printf("WITHOUT REPLACEMENT"); insertwor(b); break; case 3: break; } }while(ch1!=3); break; case 3: do { clrscr(); printf("\n\t\tDELETE\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT"); printf("\n\t\tENTER UR CHOICE"); scanf("%d",&ch1); switch(ch1) { case 1: printf("WITH REPLACEMENT"); deletwr(a); break; case 2: printf("WITHOUT REPLACEMENT"); deletwor(b); break; case 3: break; } }while(ch1!=3); break; case 4: do { clrscr(); printf("\n\t\tDISPLAY\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT"); printf("\n\t\tENTER UR CHOICE"); scanf("%d",&ch1); switch(ch1) { case 1: printf("WITH REPLACEMENT"); displaywr(a) ; break; case 2: printf("WITHOUT REPLACEMENT"); displaywor(b) ; break; case 3: break; } }while(ch1!=3); break; case 5: do { clrscr(); printf("\n\t\tSEARCH\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT"); printf("\n\t\tENTER UR CHOICE"); scanf("%d",&ch1); switch(ch1) { case 1: printf("WITH REPLACEMENT"); searchwr(a); break; case 2: printf("WITHOUT REPLACEMENT"); searchwor(b); break; case 3: break; } }while(ch1!=3); break; case 6: break; } }while(ch!=6); getch(); } void createwor(int b[][2]) { int i,j; for(i=0;i<MAX;i++) for(j=0;j<2;j++) b[i][j]=-1; do { insertwor(b); printf("DO U WANT TO ENTER MORE"); }while(getche()=='y'); } void insertwor(int b[][2]) { int r,s,t,a; printf("\nENTER DATA"); scanf("%d",&a); s=a%5; if(b[s][0]==-1) b[s][0]=a; else { //printf("COLLISION OCCURS"); //getch(); while(1) { printf("\nTRUE COLLISION OCCURS"); getch(); if(b[s][1]==-1) { t=s; while(b[s][0]!=-1) { //s++; s=(s+1)%MAX; if(s==t) { printf("OVERFLOW"); break; } } if(s!=t) { b[s][0]=a; b[t][1]=s; } break; } else s=b[s][1]; } } } void deletwr(int b[][2]) { int r,s,t,a; printf("ENTER DATA WHICH U WANT TO DELETE"); scanf("%d",&a); t=s=a%5; while(1) { if(b[s][0]==a) break; t=s; s=b[s][1]; if(s==-1) break; } if(s==-1) printf("NOT FOUND"); else { while(1) { if(b[b[t][1]][0]!=a) b[t][0]=b[b[t][1]][0]; r=b[t][1]; if(b[r][1]==-1) { b[t][1]=b[r][1]; break; } t=r; } b[r][0]=-1; } } void displaywor(int b[][2]) { int i,r,s,t,a; printf("\nINDEX\tDATA\tCHAIN"); for(i=0;i<MAX;i++) { if(b[i][0]!=-1) { printf("\n%d",i); printf("\t%d",b[i][0]); //if(b[i][1]!=-1) printf("\t%d",b[i][1]); } } getch(); } void createwr(int b[][2]) { int i,j; for(i=0;i<MAX;i++) for(j=0;j<2;j++) b[i][j]=-1; do { insertwr(b); printf("DO U WANT TO ENTER MORE"); }while(getche()=='y'); } void insertwr(int b[][2]) { int r,s,t,a,i; printf("\nENTER DATA"); scanf("%d",&a); s=a%5; if(b[s][0]==-1) b[s][0]=a; else if(s==(b[s][0]%5)) { //printf("TRUE COLLISION OCCURS"); //getch(); while(1) { printf("\nTRUE COLLISION OCCURS"); getch(); if(b[s][1]==-1) { t=s; while(b[s][0]!=-1) { s=(s+1)%MAX; if(t==s) { printf("OVERFLOW"); getch(); break; } } if(t!=s) { b[s][0]=a; b[t][1]=s; } break; } else s=b[s][1]; } } else { printf("FALSE COLLISION OCCURS"); getch(); r=i=b[s][0]%5; while(1) { if(b[i][1]==s) { b[i][1]=b[s][1]; break; } i=b[i][1]; } t=i; while(b[i][1]!=-1) i=b[i][1]; t=i; i=(i+1)%MAX; while(b[i][0]!=-1) { i=(i+1)%MAX; if(r==i) { printf("OVERFLOW"); getch(); break; } } if(r!=i) { b[t][1]=i; b[i][0]=b[s][0]; //b[i][1]=-1; b[s][0]=a; b[s][1]=-1; //b[r][1]=i; } } } void displaywr(int b[][2]) { int i,r,s,t,a; printf("\nINDEX\tDATA\tCHAIN"); for(i=0;i<MAX;i++) { if(b[i][0]!=-1) { printf("\n%d",i); printf("\t%d",b[i][0]); //if(b[i][1]!=-1) printf("\t%d",b[i][1]); } } getch(); } void searchwr(int b[][2]) { int r,s,a; printf("ENTER DATA WHICH U WANT TO SEARCH"); scanf("%d",&a); s=a%5; while(1) { if(b[s][0]==a) break; s=b[s][1]; if(s==-1) break; } if(s==-1) printf("NOT FOUND"); else { printf("\nINDEX IS %d:",s); printf("\nDATA IS %d:",b[s][0]); printf("\nINDEX WHICH IS CHAINED FROM IT:%d",b[s][1]); } getch(); } void searchwor(int b[][2]) { int r,s,a; printf("ENTER DATA WHICH U WANT TO SEARCH"); scanf("%d",&a); s=a%5; while(1) { if(b[s][0]==a) break; s=b[s][1]; if(s==-1) break; } if(s==-1) printf("NOT FOUND"); else { printf("\nINDEX IS %d:",s); printf("\nDATA IS %d:",b[s][0]); printf("\nINDEX WHICH IS CHAINED FROM IT:%d",b[s][1]); } getch(); } void deletwor(int b[][2]) { int r,s,t,a,k; printf("ENTER DATA WHICH U WANT TO DELETE"); scanf("%d",&a); t=s=a%5; while(1) { if(b[s][0]==a) break; t=s; s=b[s][1]; if(s==-1) break; } if(s==-1) printf("NOT FOUND"); else { r=t; while(1) { if(b[b[r][1]][0]!=a&&b[b[r][1]][0]%5!=b[r][1]) { b[t][0]=b[b[r][1]][0]; t=r; } else if(b[b[t][1]][1]==-1) { b[t][1]=-1; break; } r=b[r][1]; if(b[r][1]==-1) { b[t][1]=b[r][1]; break; } if(b[b[t][1]][0]!=a&&b[b[t][1]][0]%5!=b[t][1]) { t=r; k=r; } } b[r][0]=b[k][1]=-1; } }
Leave a Reply