Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<stdlib.h>
- #define TABLE_SIZE 10
- int h[TABLE_SIZE]={NULL};
- void insert()
- {
- int key,index,i,flag=0,hkey;
- printf("\n\nenter a value to insert into hash table\n");
- scanf("\n%d",&key);
- hkey=key%TABLE_SIZE;
- for(i=0;i<TABLE_SIZE;i++)
- {
- index=(hkey+i)%TABLE_SIZE;
- if(h[index] == NULL)
- {
- h[index]=key;
- break;
- }
- }
- if(i == TABLE_SIZE)
- printf("\n\nelement cannot be inserted\n");
- }
- void search()
- {
- int key,index,i,flag=0,hkey;
- printf("\n\nenter search element\n");
- scanf("\n%d",&key);
- hkey=key%TABLE_SIZE;
- for(i=0;i<TABLE_SIZE; i++)
- {
- index=(hkey+i)%TABLE_SIZE;
- if(h[index]==key)
- {
- printf("\nvalue is found at index %d",index);
- break;
- }
- }
- if(i == TABLE_SIZE)
- printf("\n\n value is not found\n");
- }
- void display()
- {
- int i;
- printf("\n\nelements in the hash table are \n");
- for(i=0;i< TABLE_SIZE; i++)
- printf("\n\nAt index %d \t value = %d",i,h[i]);
- }
- main()
- {
- int opt,i;
- while(1)
- {
- printf("\nPress \t1.Insert\t 2.Display\t 3.Search\t 4.Exit \n");
- scanf("\n%d",&opt);
- switch(opt)
- {
- case 1:
- insert();
- break;
- case 2:
- display();
- break;
- case 3:
- search();
- break;
- case 4:exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement