第8章 跳表和散列表.doc

第8章 跳表和散列表.doc

ID:28814623

大小:52.50 KB

页数:6页

时间:2018-12-14

第8章 跳表和散列表.doc_第1页
第8章 跳表和散列表.doc_第2页
第8章 跳表和散列表.doc_第3页
第8章 跳表和散列表.doc_第4页
第8章 跳表和散列表.doc_第5页
资源描述:

《第8章 跳表和散列表.doc》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库

1、第8章跳表和散列表程序8.1跳表结点类templatestructSNode{SNode(intmSize){link=newSNode*[mSize];}~SNode(){delete[]link;}Telement;SNode**link;}程序8.2跳表类templateclassSkiplist:publicDynamicSet{public:Skiplist(Tlarge,intmLev);~Skiplist();ResultCodeSearch(T

2、&x)const;ResultCodeInsert(T&x);ResultCodeRemove(T&x);private:intlevel();SNode*SaveSearch(constT&x);intmaxLevel,levels;SNode*head,*tail,**last;};程序8.3构造函数templateSkiplist::Skiplist(Tlarge,intmLev){6maxLevel=mLev;levels=0;head=newSNode(

3、maxLevel+1);tail=newSNode(0);last=newSNode*[maxLevel+1];tail->element=large;for(inti=0;i<=maxLevel;i++)head->link[i]=tail;}程序8.4跳表的搜索templateResultCodeSkiplist::Search(T&x)const{if(x>=tail->element)returnRangeError;SNode*p=head;for(in

4、ti=level;i>=0;i--)while(p->link[i]->elementlink[i];if(x==p->link[0]->element){x=p->link[0]->element;returnSuccess;}elsereturnNotPresent;}程序8.5跳表的插入templateintSkipList::Level(){intlev=0;while(rand()<=RAND_MAX/2)lev++;return(lev<=maxLeve

5、l)?lev:maxLevel;}templateSNode*SkipList::SaveSearch(constT&x){SNode*p=head;6for(inti=levels;i>=0;i--){while(p->link[i]->elementlink[i];last[i]=p;}return(p->link[0]);}templateResultCodeSkiplist::Insert(T&x)const{if(x>=t

6、ail->element)returnRangeErroe;SNode*p=SaveSearch(x);if(p->element==x){x=p->element;returnDuplicate;}intlev=Level();if(lev>levels){lev=++levels;last[lev]=head;}SNode*y=newSNode(lev+1);y->element=x;for(inti=0;i<=lev;i++){y->link[i]=last[i]->link[i

7、];last[i]->link[i]=y;}returnSuccess;}程序8.6跳表的删除templateResultCodeSkiplist::Remove(T&x){if(x>=tail->element)returnRangeError;6SNode*p=SaveSearch(x);if(p->element!=x)returnNotPresent;for(inti=0;i<=levels&&last[i]->link[i]==p;i++)last[i]->link

8、[i]=p->link[i];while(levels>0&&head->link[levels]==tail)level--;x=p->element;deletep;returnSuccess;}程序8.7散列表类templateclassHashTable:publicDynamicSet{public:HashTable(intdivisor=11);~HashTable(){delete[]ht;delete

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。