Skiplist实现——(C++版)
2013-10-17 23:04
453 查看
Comparator.h
SkipList.h
SkipList.cpp
SConstruct script
#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')
相关文章推荐
- 跳跃表(skip list)基本原理及C/C++实现
- 跳跃表(Skip List)的实现及测试 C++实现
- [C++]数据结构:跳表SkipList的实现与使用
- c++实现跳跃表(Skip List)的方法示例
- 浅谈STL list<T>链表容器和迭代器的使用C++实现
- C++STL库list容器简单实现
- 用C++的STL部分实现Python中的list(列表)
- list类的C++模板实现
- list容器的C++代码实现
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- C++通过list和template实现简单的对象池
- C++实现List类
- C语言跳表(skiplist)实现
- 【转】浅析SkipList跳跃表原理及代码实现
- An implementation of the skip list data structure written in C++
- SkipList实现——Java版
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
- C++ 简单实现MFC ListControl 点击列头排序
- Skip List(跳跃表)原理详解与实现
- 自己实现C++list容器