Untitled

 avatar
unknown
plain_text
4 years ago
4.0 kB
4
Indexable
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct element
{
        long int key;
        char name[100];
        int mark;
};

void display(struct element *, int);
void insert_to_hash(struct element*, int, long int, char*, int*);
void delete_from_hash(struct element *,int ,long int ,int *);
void search_in_hash(struct element *,int ,long int key,int );

int main()
{
        struct element *hashtable;
        int i,n,ch,no_elements;
        long int key;
        char name[100];
        int tablesz;
        printf("Enter the directory size: ");
        scanf("%d",&tablesz);
        hashtable=(struct element*)(malloc(tablesz* sizeof(struct element)));
        for(i=0;i<tablesz;i++)
                hashtable[i].mark=0;
        no_elements=0;
        while(1)
        {
            printf("\n1. Insert");
            printf("\n2. Delete");
            printf("\n3. Search");
            printf("\n4. Display");
            printf("\n5. Exit");
            printf("\nEnter choice: ");
            scanf("%d",&ch);
            printf("\n");
            switch(ch)
            {
                case 1:       printf("Enter phone number: ");
                              scanf("%ld",&key);
                              printf("Enter name: ");
                              scanf("%s",name);
                              insert_to_hash(hashtable,tablesz,key,name,&no_elements);
                              break;
                case 2:       printf("Enter the phone number for deletion: ");
                              scanf("%ld",&key);
                              delete_from_hash(hashtable,tablesz,key,&no_elements);
                              break;
                case 3:       printf("Enter the phone number to search: ");
                              scanf("%ld",&key);
                              search_in_hash(hashtable,tablesz,key,no_elements);
                              break;
                case 4:       display(hashtable,tablesz);
                              break;
                case 5:       exit(0);
                default:      printf("Invalid choice\n");
            }
        }
}

void insert_to_hash(struct element *ht, int size, long int key, char *name, int *count)
{
        int index;
        if(size==*count)
        {
                printf("Directory full\n");
                return;
        }
        index=key % size;
        while(ht[index].mark !=0)
		index=(index+1)%size;
	ht[index].key=key;
	strcpy(ht[index].name,name);
	ht[index].mark=1;
        (*count)++;
	return;
}

void delete_from_hash(struct element *ht,int size,long int key,int *count)
{
        int i,index;
        printf("Count = %d\n",*count);
        if(*count==0)
        {
                printf("Directory empty\n");
                return;
        }
        index = key % size;
        i=0;
	while(i<*count)
	{
	        if(ht[index].mark==1)
	        {
	                if(ht[index].key==key) 
			{ 
			        ht[index].mark=0;
				(*count)--;
				return;
			}
			i++;
		}
                index=(index+1)%size;
        }
        printf("Record not found");
        return;
}

void search_in_hash(struct element *ht,int size,long int key,int count)
{
        int i,index;
        if(count==0)
        {
                printf("Directory empty\n");
                return;
        }
        index = key % size;
        i=0;
	while(i<count)
	{
	        if(ht[index].mark==1)
	        {
		        if(ht[index].key==key) 
	                {
		                printf("Record is Found");
		                printf("Phone number: %ld\nName: %s\n",ht[index].key,ht[index].name);
				return;
			}
			i++;
		}
	        index=(index+1)%size;
        }
        printf("Record not found");
	return;
}

void display(struct element *ht, int size)
{
        int i;
        for(i=0;i<size;i++)
        {
                printf("%d :", i);
                if (ht[i].mark!=0)
                        printf("Phone number: %ld\nName: %s \n",ht[i].key,ht[i].name);
                printf("\n");
        }
}  
Editor is loading...