Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class singlerecord
- { public:
- int key;
- long int ph_num;
- string name;
- singlerecord()
- {
- key=-1;
- ph_num=-1;
- string name;
- }
- void upd(int k,string nam,long int phu)
- {
- key=k;
- name=nam;
- ph_num=phu;
- }
- };
- class hashtable
- {
- public:
- singlerecord rec[10];
- long long int ph_num;
- string name;
- int index,i,j;
- int counter;
- long long int phone;
- void insert()
- {
- cout<<"\nEnter student name:";
- cin>>name;
- cout<<"\nEnter phone number:";
- cin>>phone;
- index=phone%10;
- int counter =0;
- while(rec[index].key!=-1)
- {
- index=(index+1)%10;
- counter++;
- if(counter>10)
- return;
- }
- rec[index].upd(counter,name,phone);
- }
- void display(){
- for(i=0;i<10;i++)
- {
- cout<<rec[i].key<<" - "<<rec[i].name<<" "<<rec[i].ph_num<<endl;
- }
- }
- void searching(){
- cout<<" \n Enter phone Number";
- cin>>phone;
- int j=0;
- for(j=0;j<10;j++)
- {
- if(phone==rec[j].ph_num)
- {cout<<"\nrecord found at"<<endl;
- cout<<rec[j].key<<" - "<<rec[j].name<<" "<<rec[j].ph_num<<endl;}
- }
- if(j==10)
- cout<<"Record not found"<<endl;
- }
- void del(){
- cout<<"\n Enter phone number";
- cin>>phone;
- int j=0;
- for(j=0;j<10;j++)
- {
- if(phone==rec[j].ph_num)
- {
- rec[j].key=-1;
- rec[j].ph_num=-1;
- rec[j].name="";
- break;
- }
- if(j==10)
- cout<<"Record not found to delete"<<endl;
- }
- }
- };
- int main()
- {
- hashtable h;
- int choice=0;
- do
- {
- cout<<"Enter 1 to insert a data item";
- cout<<"\nEnter 2 to delete a data item";
- cout<<"\nEnter 3 to display data item";
- cout<<"\nEnter 4 to search data item";
- cout<<"\nEnter 0 to exit\n";
- cin>>choice;
- switch(choice)
- {
- case 1:
- h.insert();
- break;
- case 2:
- h.del();
- break;
- case 3:
- h.display();
- break;
- case 4:
- h.searching();
- break;
- default:
- cout<<"You entered a wrong choice";
- break;
- }
- }while(choice!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement