Question
Implementation of Symbol Table with Suitable Hashing.
Program
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct symtable
{
int address;
char label[10];
int flag;
}a[11];
int hashvalue,addr,i,ch;
char lbl[10];
void display()
{
printf("\n The Symbol Table is \n");
printf("\n Hash Values Address Label\n");
for(i=0;i<11;i++)
{
printf("\n\n %d %d %s",i,a[i].address,a[i].label);
}
}
void create()
{
char opt[10];
do
{
printf("\n Enter the address:");
scanf("%d",&addr);
hashvalue=addr%11;
printf("\n Enter the Label:");
scanf("%s",lbl);
if(a[hashvalue].flag==0)
{
a[hashvalue].address=addr;
strcpy(a[hashvalue].label,lbl);
a[hashvalue].flag=1;
}
else
{
int allocated=0;
for(hashvalue++;hashvalue<11;hashvalue++)
{
if(a[hashvalue].flag==0)
{
a[hashvalue].address=addr;
strcpy(a[hashvalue].label,lbl);
a[hashvalue].flag=1;
allocated=1;
break;
}
}
if(allocated==0)
{
hashvalue=0;
for(;hashvalue<11;hashvalue++)
{
if(a[hashvalue].flag==0)
{
a[hashvalue].address=addr;
strcpy(a[hashvalue].label,lbl);
a[hashvalue].flag=1;
break;
}
}
}
}
printf("\n Continue(y/n)? ");
scanf("%s",opt);
}while(strcmp(opt,"n")!=0);
display();
}
void search()
{
printf("\n Enter the label:");
scanf("%s",lbl);
for(i=0;i<11;i++)
{
if(a[i].flag==1)
{
if(strcmp(a[i].label,lbl)==0)
{
printf("\n The label %s found at address %d",lbl,a[i].address);
}
}
}
}
void main()
{
for(i=0;i<10;i++)
{
a[i].flag = 0;
a[i].address=0;
strcpy(a[i].label," ");
}
while(1)
{
ph:
printf("\n\n 1.Create a Symbol Table \n 2.Search in Symbol Table \n 3.Exit");
printf("\n\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
search();
break;
case 3:
exit(0);
break;
default:
printf("\n Enter a valid option:");
goto ph;
}
}
}
Download Source Code
No comments:
Post a Comment