在cpp中跳过列表实现

时间:2017-11-20 07:15:56

标签: c++ pointers

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

我正在提供我现在已经实施的代码。到目前为止我只实现了一项功能,即插入功能。但我得到分段错误。我知道我在构造函数,更新或插入函数中的某处使用指针搞乱。有人可以帮忙吗。

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;

}

1 个答案:

答案 0 :(得分:0)

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

head[i] = new node(headkey,0,maxLevel-1);

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