繁体   English   中英

cpp中的跳过列表实现

[英]Skip List implementation in cpp

我正在尝试在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] = new node(headkey,0,maxLevel-1);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM