gpt4 book ai didi

c++ - cpp 中的跳过列表实现

转载 作者:行者123 更新时间:2023-12-03 12:49:08 28 4
gpt4 key购买 nike

我正在尝试在 cpp 中实现跳过列表。有许多版本的跳表可用,但我特别想实现一个版本,其中每个节点都有一个向右和向下的指针,以形成各个级别的连接列表。此外,在每个更高级别都有一个节点的拷贝而不仅仅是一个指针。

我给出了到目前为止我已经实现的代码。到目前为止我只实现了一项功能,即插入。但我遇到了段错误。我知道我在构造函数、更新函数或插入函数中的某个地方弄乱了指针。有人可以帮忙吗?

class SkipList
{

private:

struct node {

int key;
int data;
int level;
struct node* rgt = nullptr;
struct node* dwn = nullptr ;

node(int k, int value, int l):
key(k), data(value), level(l)
{}

};


//generates the ndde level in tha range [1,maxLevel).
int randomLevel() const;

//returns a set of pointers to the location at each node where new links are to be created
std::vector<node*> update(int searchKey) const ;

//creates a new node and returns a pointer to it
static node* makeNode(int key, int val, int level);

const float probability;
const int maxLevel;


// head and tail vectors
vector<node*> head;
vector<node*> nil;


public:

SkipList();
~SkipList();
void insert(int searchKey, int val);
void print() const;
};

SkipList::SkipList() :
probability(0.5), maxLevel(16)
{

int headkey = std::numeric_limits<int>::min();
int nilkey = std::numeric_limits<int>::max();

for(int i = 0; i < maxLevel;i++)
{
head[i] = new node(headkey,0,maxLevel-1);
nil[i] = new node(nilkey,0,maxLevel-1);

if(i > 0)
{
head[i]-> dwn = nil[i-1];
nil[i] -> dwn = nil[i-1];
}

head[i]->rgt = nil[i];
}

}



void SkipList::insert(int searchKey, int val)
{

vector <node*> preds = update(searchKey);
node* temp;

const int newLevel = randomLevel();

for(int i = 0; i< newLevel; i++)

{

node* ptr = makeNode(searchKey,val, newLevel-1);
temp = preds[i]->rgt;
preds[i]->rgt = ptr;
ptr->rgt = temp;

}

}

void SkipList::print() const{

node* list = head[0]->rgt;
int lineLength = 0;

std::cout<<"{";

while (list->rgt != nil[list->level])
{
std::cout<<"value: "<<list->data
<<", key: "<<list->key
<<", level: "<<list->level;

list = list->rgt;

if(list->rgt != nil[list->level]) std::cout<<" : ";
if (++lineLength % 2 == 0) std::cout << "\n";
}

std::cout << "}\n";

}

int SkipList::randomLevel() const{

int v = 1;
while (((double)std::rand() / RAND_MAX) < probability
&& v < maxLevel)
{
v++;
}
return v;
}

SkipList::node* SkipList::makeNode(int key, int value, int level){
return new node(key, value, level);
}

std::vector<SkipList::node*>SkipList::update(int searchKey) const{

int level = head[0]->level;

std::vector<node*> result(level,nullptr);

node* x ;

for(unsigned int i = level;i-- >0;)
{
x = head[i];
while(x->rgt->key < searchKey)
{
x = x->rgt;
}
result[i]= x;

}

return result;

}

int main()
{

SkipList s;

s.insert(5,22);
s.insert(2,33);
s.print();

return 0;

}

最佳答案

你应该在SkipList的ctor中使用push_back方法。现在您正在创建对象

head[i] = 新节点(headkey,0,maxLevel-1);

并且您正在尝试将创建的节点对象分配给由 vector::operator[] 返回的对象,但该对象不存在。或者您可以在进入 for 循环之前调用 vector::resize(maxlevel) 方法。

关于c++ - cpp 中的跳过列表实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47386760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com