Skiplist实现——(C++版)

Comparator.h

#ifndef __COMPARATOR_H__
#define __COMPARATOR_H__
#include <iostream>
#include <functional>

template<class T>
class Comparator : public std::binary_function<T, T, bool> {
public:
    bool operator() (T t1, T t2) {
        return (t1 < t2);
    }
};

#endif //__COMPARATOR_H__


SkipList.h

#ifndef __SKIPLIST_H__
#define __SKIPLIST_H__

#define MAX_LEVEL 100 

template <class T>
class SkipNode {
public:
        //T value;
        //SkipNode<T> **forward; //array of poniter

        SkipNode(int level, const T &value) {
                forward = new SkipNode<T> * [level + 1];
                memset(forward, 0, sizeof(SkipNode<T>*) * (level + 1));
                this->value = value;
        }

        ~SkipNode() {
                delete[] forward;
        }

public:
    T value;
    SkipNode<T> **forward;
};

template <class T>
class SkipSet {
public:
        //SkipNode<T> *header;
        //int level;

        SkipSet() {
                header = new SkipNode<T>(MAX_LEVEL, T());
                level = 0;
        }

        ~SkipSet() {
                delete header;
        }

        void print() const;
        bool contains(const T &) const;
        void insert(const T &);
        void erase(const T &);
    T advance(const T &) const;

private:
    SkipNode<T> *header;
    int level;
};

#endif // __SKIPLIST_H__


SkipList.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>

#include "SkipList.h"
#include "Comparator.h" 

using namespace std;

const float P = 0.5;

float frand() {
        return (float) rand() / RAND_MAX;
}

int random_level() {
        static bool first = true;

        if (first) {
                srand((unsigned) time(NULL));
                first = false;
        }

        int lvl = (int) (log(frand())) / log(1.-P);

        return lvl < MAX_LEVEL ? lvl : MAX_LEVEL;
}

template <class T>
void SkipSet<T>::print() const {
        const SkipNode<T> *x = header->forward[0];
        cout << "{";

        while (x != NULL) {
                cout << x->value;
                x = x->forward[0];
                if (x != NULL)
                        cout << ",";
        }

        cout << "}" << endl;
}

template <class T>
bool SkipSet<T>::contains(const T& search_val) const {
        const SkipNode<T> *x = header;
        for (int i = level; i >= 0; --i) {
                while (x->forward[i] != NULL && x->forward[i]->value < search_val) {
                        x = x->forward[i];
                }
        }

        x = x->forward[0];
        return x != NULL && x->value == search_val;
}

template<class T>
T SkipSet<T>::advance(const T& target) const {
    Comparator<T> compareTo;

        const SkipNode<T> *x = header;
        for (int i = level; i >= 0; --i) {
                while (x->forward[i] != NULL && compareTo(x->forward[i]->value, target)/*x->forward[i]->value < target*/) {
                        x = x->forward[i];
                }
        }

        x = x->forward[0];
        return x;
}

template <class T>
void SkipSet<T>::insert(const T &value) {
        SkipNode<T> *x = header;
        SkipNode<T> *update[MAX_LEVEL + 1];
        memset(update, 0, sizeof(SkipNode<T>*) * (MAX_LEVEL + 1));

        for (int i = level; i >= 0; --i) {
                while (x->forward[i] != NULL && x->forward[i]->value < value) {
                        x = x->forward[i];
                }

                update[i] = x;
        }

        x = x->forward[0];

        if (x == NULL || x->value != value) {
                int lvl = random_level();
                if (lvl > level) {
                        for (int i = level + 1; i <= lvl; ++i) {
                                update[i] = header;
                        }
                }
                level = lvl;

                x = new SkipNode<T>(lvl, value);
                for (int i = 0; i <= lvl; ++i) {
                        x->forward[i] = update[i]->forward[i];
                        update[i]->forward[i] = x;
                }
        }
}

template <class T>
void SkipSet<T>::erase(const T &value) {
        SkipNode<T> *x = header;
        SkipNode<T> *update[MAX_LEVEL + 1];
        memset(update, 0, sizeof(SkipNode<T>*) * (MAX_LEVEL + 1));

        for (int i = level; i >= 0; --i) {
                while(x->forward[i] != NULL && x->forward[i]->value < value) {
                        x = x->forward[i];
                }

                update[i] = x;
        }

        x = x->forward[0];

        if (x->value == value ) {
                for (int i = 0; i <= level; i++) {
                        if (update[i]->forward[i] != x) break;
                        update[i]->forward[i] = x->forward[i];
                }

                delete x;

                while(level > 0 && header->forward[level] == NULL)
                        level --;
        }
}

int main() {
        SkipSet<int> ss;
        ss.print();

        ss.insert(7);
        ss.insert(14);
        ss.insert(21);
        ss.insert(32);
        ss.insert(37);
        ss.insert(71);
        ss.insert(85);
        ss.insert(117);

        if (ss.contains(7)) {
                cout << "7 in list" << endl;
        }

        ss.print();

        ss.erase(7);
        ss.print();

        if (!ss.contains(7))
                cout << "7 has been deleted" << endl;

        return 0;
}

SConstruct script

#!/usr/bin/env python

evn = Program('skiplist', 'SkipList.cpp')


你可能感兴趣的:(算法,跳跃表)