数据结构--BlockQuery,HashQuery,通讯录

来源:互联网 发布:域名服务器 编辑:程序博客网 时间:2024/06/01 14:35

#include <iostream>#include<list>using namespace std; //jianli Hash class // class ContactInfo { public:    string name;    string phoneNum;    string email; ContactInfo(string n,string p,string e) {     name=n;     phoneNum=p;     email=e; } ContactInfo() { } string getName() {     return name; } string getPhoneNum() {    return phoneNum; } string getEmail() {     return email; } int getkey() {     int num=0;     for(int i=0;i<name.size()&&(name[i]!='\0');i++)     {            num+=name[i];     }     return num; } bool operator==(ContactInfo &c2) {     return name==c2.name; } bool operator<(ContactInfo &c2) {     return name<c2.name; } }; class HashFind{ public:     list<ContactInfo> *p;     int length;     HashFind(int n)     {        p=new list<ContactInfo>[n];        length=n;     }     int Hash(int x)     {         return (x)%length;     }     void buildHash(list<ContactInfo> &ll)     {           list<ContactInfo>::iterator iter;                for(iter=ll.begin();iter!=ll.end();iter++)                    {                            p[Hash((*iter).getkey())].push_back((*iter));                    }     }     ContactInfo find_contact(ContactInfo a)     {         list<ContactInfo>::iterator iter;                for(iter=p[Hash(a.getkey())].begin();iter!=p[Hash(a.getkey())].end();iter++)                    {                           if(a==(*iter))                           {                                cout<<"name: "<<(*iter).name<<" phone: "<<(*iter).phoneNum<<" Email "<<(*iter).email<<endl;                               return a;                           }                    }     } }; class ContactList {     public:     list<ContactInfo> contacts;     int blockInfo[26][2];     int length;     ContactList(int size)     {            for(int i=0;i<26;i++)                for(int j=0;j<2;j++)            {                blockInfo[i][j]=0;                length=size;            }     }     bool addContact(string name,string phonenum,string email)     {         int pos=0;         ContactInfo contact(name,phonenum,email);         while(pos<contacts.size())         {             list<ContactInfo>::iterator iter;             int i=0;             for(iter=contacts.begin();iter!=contacts.end();iter++)             {                      if(i==pos)                      {                          break;                      }                       i++;             }             if(*iter==contact)             {                 break;             }             else if(*iter<contact)             {                 pos++;             }             else             {                 contacts.insert(iter,contact);                 break;             }        }     }/*bool removeContact(string name,string phonenum,string email)     {         ContactInfo contact(name,phonenum,email);list<ContactInfo>::iterator iter;             for(iter=contacts.begin();iter!=contacts.end();iter++)             {                      if((*iter)==contact)                      {                            break;                      }             }            contacts.remove(contact);            return true;     }   */ bool UpdateContact(string name,string phonenum,string email)     {         ContactInfo contact(name,phonenum,email);list<ContactInfo>::iterator iter;             for(iter=contacts.begin();iter!=contacts.end();iter++)             {                      if((*iter)==contact)                      {                            break;                      }             }          (*iter).phoneNum=contact.phoneNum;          (*iter).email=contact.email;     } bool DeleteContact(string name,string phonenum,string email)     {                ContactInfo contact(name,phonenum,email);                list<ContactInfo>::iterator iter;             for(iter=contacts.begin();iter!=contacts.end();iter++)             {                      if((*iter)==contact)                      {                          iter= contacts.erase(iter);                            break;                      }             }     }    void showList()    {        list<ContactInfo>::iterator iter;        cout<<"-------------------ContactLists--------------"<<endl;        for(iter=contacts.begin();iter!=contacts.end();iter++)             {                      cout<<"name: "<<(*iter).name<<" phone: "<<(*iter).phoneNum<<" Email "<<(*iter).email<<endl;             }        cout<<"-------------------ContactLists--------------"<<endl;            }     int blockQuery(string name)     {         int I=97;         while(I<123)         {             int start=0;             int end=0;             start=end;             int flag1=1;             int flag2=1;             list<ContactInfo>::iterator iter;             while(start<contacts.size()&&flag1)             {                int i=0;                for(iter=contacts.begin();iter!=contacts.end();iter++)                    {                         if((*iter).name[0]<I)                         {                            start++;                            flag1=1;                         }                     else                      {                        flag1=0;                        break;                      }                }             }             end=(start);            while(end<contacts.size()&&flag2)             {                for(;iter!=contacts.end()&&(end<contacts.size());iter++)                {                         if((*iter).name[0]==I)                           {                                end++;                                flag2=1;                           }                    else  if(((*iter).name[0]!=I))                    {                       flag2=0;                       break;                   }               }             }             blockInfo[I-97][0]=start;             blockInfo[I-97][1]=end;             I++;         }         int nam=(int)name[0];         int s=blockInfo[nam-97][0];         int e=blockInfo[nam-97][1];         int ss=0;         list<ContactInfo>::iterator itera;         list<ContactInfo>::iterator is;         list<ContactInfo>::iterator ie;         for(itera=contacts.begin();itera!=contacts.end();itera++)         {                       if(ss==s)                       {                           is=itera;                       }                       if(ss==e)                       {                           ie=itera;                           break;                       }                       ss++;         }             for(itera=is;itera!=ie;itera++)         {                      if((*itera).name==name)                      {                          cout<<"name: "<<(*itera).name<<" phone: "<<(*itera).phoneNum<<"Email "<<(*itera).email<<endl;                          return true;                      }         }         cout<<"end"<<endl;         return false;     } }; void show_table() { }int main(){    ContactList a(10);    ContactInfo b("sss","sss","sss");    a.contacts.push_back(b);    a.addContact("abb","165","123456");    a.addContact("abc","156","123456");    a.addContact("cdc","187","123456");    a.addContact("bcd","163","123456");    a.showList();    cout<<"--------blockQuery--sss-------"<<endl;    a.blockQuery("sss");    cout<<"--------update--sss-------"<<endl;    a.UpdateContact("sss","1566653","13006666@qq.com");    a.showList();    cout<<"--------delete--abc-------"<<endl;    a.DeleteContact("abc","156","123456");    a.showList();     HashFind h(10);     cout<<"--------HashQuery--abb-------"<<endl;     h.buildHash(a.contacts);     ContactInfo c("abb","122","123");     h.find_contact(c);    return 0;}

0 0
原创粉丝点击