c++編寫雜湊表

來源:互聯網
上載者:User
文章目錄
  •          好久沒有更新文章了,不知不覺發覺自己已經工作,感慨萬千啊,不過,廢話少說,還是發一篇自己的最近寫的一個雜湊表吧,寫的不好,好多問題沒有得到妥善解決,還望大家多多指教
  •            本文有不足之處,還望大家多多指正。
         好久沒有更新文章了,不知不覺發覺自己已經工作,感慨萬千啊,不過,廢話少說,還是發一篇自己的最近寫的一個雜湊表吧,寫的不好,好多問題沒有得到妥善解決,還望大家多多指教
#include<iostream>#include<string>using namespace std;template <class t1,class t2>struct node{t1 key;t2 data;node *next;};template <class t1,class t2>class hash{public:typedef node<t1,t2>* linknode;typedef node<t1,t2> ln;hash();~hash();void insert(t1 key,t2 data);void remove(t1 key);t2 query(t1 key);private:int func(char *);int func(string);int func(int);int func(char);int func(float);int func(void*);linknode *array;string type;};template<class t1,class t2>hash<t1,t2>::hash(){array=new linknode[10];for(int i=0;i<10;i++){array[i]=0;}}template<class t1,class t2>hash<t1,t2>::~hash(){for(int i=0;i<10;i++){if(array[i]){linknode n=array[i];while(n){linknode n1=n;n=n->next;delete n1;}}}}template<class t1,class t2>void hash<t1,t2>::insert(t1 key,t2 data){int sum=func(key)%10;if(array[sum]){ln *n=new ln;n->key=key;n->data=data;n->next=0;linknode p=array[sum];while(p->next){p=p->next;}p->next=n;}else{ln *n=new ln;n->key=key;n->data=data;n->next=0;array[sum]=n;}}template<class t1,class t2>t2 hash<t1,t2>::query(t1 key){int sum=func(key)%10;linknode p=array[sum];while(p){if(type=="char*"){if(stricmp((char*)(int)p->key,(char*)(int)key)==0){return p->data;}else{p=p->next;}}else if(type=="void*"){if((int)p->key==(int)key){return p->key;}else{p=p->next;}}else{if(p->key==key){return p->data;}else{p=p->next;}}}return 0;}template<class t1,class t2>int hash<t1,t2>::func(int i){type="int";return i;}template<class t1,class t2>int hash<t1,t2>::func(float i){type="float";int s=i*100;return s;}template<class t1,class t2>int hash<t1,t2>::func(char *i){type="char*";int s=strlen(i)*i[0];return s;}template<class t1,class t2>int hash<t1,t2>::func(char i){type="char";int s=(int)i;return s;}template<class t1,class t2>int hash<t1,t2>::func(string i){type="string";int s=i.length()*(int)i[0];return s;}template<class t1,class t2>int hash<t1,t2>::func(void *i){type="void*";int s=(int)i;return s;}template<class t1,class t2>void hash<t1,t2>::remove(t1 key){int sum=func(key)%10;linknode p=array[sum];linknode p1=p;while(p){if(type=="char*"){if(stricmp((char*)(int)p->key,(char*)(int)key)==0){if(array[sum]==p){linknode p2=p;array[sum]=p->next;delete p2;}else{linknode p2=p;p1->next=p->next;delete p2;}return;}else{p1=p;p=p->next;}}else if(type=="void*"){if((int)p->key==(int)key){if(array[sum]==p){linknode p2=p;array[sum]=p->next;delete p2;}else{linknode p2=p;p1->next=p->next;delete p2;}return ;}else{p1=p;p=p->next;}}else{if(p->key==key){if(array[sum]==p){linknode p2=p;array[sum]=p->next;delete p2;}else{linknode p2=p;p1->next=p->next;delete p2;}return ;}else{p1=p;p=p->next;}}}return ;}main(){hash<char *,char *> h;h.insert("sds","etew");h.insert("45","45");h.insert("4.5765k","2");h.insert("$345","6");h.insert("43.r","55");h.insert("sx","24");h.remove("$345");cout<<h.query("43.r");}
           本文有不足之處,還望大家多多指正。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.