有关跳表级数产生的问题

#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include"exception.h"
using namespace std;

class SkipList;
class SkipNode
{
friend SkipList;
private:
SkipNode(int size)
{
link=new SkipNode *[size];
}
~SkipNode(){delete [] link;}
int data;
SkipNode **link;
};

class SkipList
{
public:
SkipList(int Large,int MaxE=10000,float p=0.5);
~SkipList();
bool Search(const int& k)const;
SkipList& Insert(const int& e);
SkipList& Delete(const int& k);
int Level();
void Output(ostream& out)const;
int getlevels(){return Levels;}
private:
SkipNode *SaveSearch(const int& k);
int MaxLevel;
int Levels;
int CutOff;
int TailKey;
SkipNode *head;
SkipNode *tail;
SkipNode **last;
};

SkipList::SkipList(int Large,int MaxE,float p)
{
CutOff=p*RAND_MAX;
MaxLevel=ceil(log(MaxE)/log(1/p))-1;
TailKey=Large;
srand((unsigned int)time(NULL));
Levels=0;
head=new SkipNode(MaxLevel 1);
tail=new SkipNode(0);
last=new SkipNode *[MaxLevel 1];
tail->data=Large;
for(int i=0;i <=MaxLevel;i )
head->link[i]=tail;
}

SkipList::~SkipList()
{
SkipNode *next;
while(head!=tail)
{
next=head->link[0];
delete head;
head=next;
}
delete tail;
delete [] last;
}

class element
{
friend void main(void);
public:
operator long()const{return key;}
element& operator=(long y)
{
key=y;
return *this;
}
private:
int data;
long key;
};

bool SkipList::Search(const int& k)const
{
int e;
if(k>=TailKey)
return false;
SkipNode *p=head;
for(int i=Levels;i>=0;i--)
while(p->link[i]->data <k)
p=p->link[i];
e=p->link[0]->data;
return (e==k);
}

SkipNode *SkipList::SaveSearch(const int& k)
{
SkipNode *p=head;
for(int i=Levels;i>=0;i--)
{
while(p->link[i]->data <k)
p=p->link[i];
last[i]=p;
}
return(p->link[0]);
}

int SkipList::Level()
{
int lev=0;
while(rand() <=CutOff)
lev ;
return (lev <=MaxLevel)?lev:MaxLevel;
}

SkipList& SkipList::Insert(const int& e)
{
if(e>=TailKey)
throw BadInput();
SkipNode *p=SaveSearch(e);
if(p->data==e)
throw BadInput();
int lev=Level();
if(lev>Levels)
{
lev= Levels;
last[lev]=head;
}
SkipNode *y=new SkipNode(lev 1);
y->data=e;
for(int i=0;i <=lev;i )
{
y->link[i]=last[i]->link[i];
last[i]->link[i]=y;
}
return *this;
}

SkipList& SkipList::Delete(const int& k)
{
if(k>=TailKey)
throw BadInput();
SkipNode *p=SaveSearch(k);
if(p->data!=k)
throw BadInput();
for(int i=0;i <=Levels && last[i]->link[i]==p;i )
last[i]->link[i]=p->link[i];
while(Levels>0 && head->link[Levels]==tail)
Levels--;
delete p;
return *this;
}
void SkipList::Output(ostream& out)const
{

for(int i=0;i <=Levels;i )
{
cout < <"第" < <i < <"级的元素:";
for(SkipNode *current=head;current->link[i],current->link[i]!=tail;current=current->link[i])
cout < <current->link[i]->data < <' ';
cout < <endl;

}
}
ostream& operator < <(ostream& out,const SkipList &x)
{
x.Output(out);
return out;
}
void main()
{
SkipList a(99999);
a.Insert(20).Insert(24).Insert(30).Insert(40).Insert(60).Insert(75).Insert(80);
cout < <a.getlevels() < <endl;
cout < <a < <endl;
a.Delete(20).Delete(30).Delete(40).Delete(60);
cout < <a.getlevels() < <endl;
cout < <a < <endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值