跳表的实现

最近看了一下跳表,看了文章 

http://www.cnblogs.com/xuqiang/archive/2011/05/22/2053516.html的实现,本想参照这个流程用c++类实现一下,结果却出现一些问题,在vc++6.0中正常运行,在codeblocks中时好时坏,调试还会出现gdb停止工作。虽然觉得应该是内存问题,可是找半天找不到,泪奔啊~~~~如果有大神找到错误,请一定告知啊。

后来只有按照上面博文用C照背了一遍,代码在第二个片段。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

template<class keyType,class valueType>
class skipList {
private:
    struct skipNode {
        keyType key;
        valueType value;
        struct skipNode** forward;
    };
    int level;
    skipNode *head;
    enum {MaxLevel = 16};
public:
    skipList():level(0) {
        head = new skipNode();
        head->forward = new skipNode*[MaxLevel];
        for(int i=0; i<MaxLevel; i++) {
            head->forward[i] = NULL;
        }
    }
    bool insertNode(keyType key,valueType value);
    bool deleteNode(keyType key);
    bool searchNode(keyType key,valueType *pValue);
    void print();
    ~skipList() {
        //skipNode *q,*p;
        //p = head;
       // while(p) {
        //    q = p;
        //    p = p->forward[0];
           //delete[] q->forward;
        //    delete q;
       // }
    }
};
template<class keyType,class valueType>
void skipList<keyType,valueType>::print()
{
    skipNode *p;
    for(p = head->forward[0]; p; p = p->forward[0]) {
        cout<<p->key<<" "<<p->value<<endl;
    }
}

template<class keyType,class valueType>
bool skipList<keyType,valueType>::insertNode(keyType key,valueType value)
{
    skipNode* update[MaxLevel];
    skipNode *p,*q;
    int k = level;
    p = head;
    while(k >= 0) {
        while((q = p->forward[k]) != NULL && q->key < key)
            p = q;
        update[k--] = p;
    }
    if(q != NULL && q->key == key) {
        q->value = value;
        return false;
    }
    srand(time(NULL));
    k = rand()%MaxLevel;
    if(k > level) {
        k = ++level;
        update[k] = head;
    }
    p = new skipNode();
    p->key = key;
    p->value = value;
    p->forward = new skipNode*[k];
    while(k >= 0) {
        q = update[k];
        p->forward[k] = q->forward[k];
        q->forward[k--] = p;
    }
    return true;
}

template<class keyType,class valueType>
bool skipList<keyType,valueType>::deleteNode(keyType key) {
    skipNode* update[MaxLevel];
    skipNode *p,*q;
    int m = level;
    p = head;
    while(m >= 0) {
        while((q = p->forward[m]) != NULL && q->key < key)
            p = q;
        update[m--] = p;
    }
    if(q != NULL && q->key == key) {
        for(m = 0;m <= level && (p = update[m])->forward[m] == q; m++) {
            p->forward[m] = q->forward[m];
        }
      //  delete[] q->forward;
      //  delete(q);
        m = level;
        while(m > 0 && head->forward[m] == NULL)
            m--;
        level = m;
        return true;
    } else {
        return false;
    }
}

template<class keyType,class valueType>
bool skipList<keyType,valueType>::searchNode(keyType key,valueType *pValue)
{
    skipNode *p,*q;
    int k = level;
    p = head;
    while(k >= 0) {
        while((q = p->forward[k]) != NULL && q->key < key)
            p = q;
        k--;
    }
    if(q != NULL && q->key == key) {
        *pValue = q->value;
        return true;
    }
    return false;
}
int main(void)
{
    skipList<int,string> skip;
    cout<<"insert:"<<skip.insertNode(3,"Wanglei")<<endl;
    cout<<"insert:"<<skip.insertNode(9,"Wangli")<<endl;
    cout<<"insert:"<<skip.insertNode(5,"Tanxuefei")<<endl;
    cout<<"insert:"<<skip.insertNode(1,"Tujike")<<endl;
    cout<<"insert:"<<skip.insertNode(2,"Zhousheng")<<endl;
    cout<<"insert:"<<skip.insertNode(6,"Xiaochao")<<endl;
    cout<<"insert:"<<skip.insertNode(5,"Jiayou")<<endl;
    skip.print();
    cout<<"delete:"<<skip.deleteNode(4)<<endl;
    cout<<"delete:"<<skip.deleteNode(3)<<endl;
    cout<<"delete:"<<skip.deleteNode(9)<<endl;
    skip.print();
    string p;
    cout<<"search:"<<skip.searchNode(8,&p)<<' '<<p<<endl;
    cout<<"search:"<<skip.searchNode(3,&p)<<' '<<p<<endl;
    cout<<"search:"<<skip.searchNode(9,&p)<<' '<<p<<endl;
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAXLEVEL 8

struct skipNode {
    int key;
    int value;
    struct skipNode** forward;
};

struct skipList {
    int level;
    struct skipNode *head;
};

skipList* InitSkipList(void) {
    skipList *l = (skipList*)malloc(sizeof(skipList));
    l->head = (skipNode*)malloc(sizeof(skipNode));
    l->head->forward = (skipNode**)malloc(MAXLEVEL*sizeof(skipNode*));
    l->level = 0;
    for(int i=0; i<MAXLEVEL; i++)
        l->head->forward[i] = NULL;
    return l;
}

int insert(skipList *l,int key,int value) {
    skipNode *p,*q;
    skipNode* update[MAXLEVEL];
    int k;
    k = l->level;
    p = l->head;
    do {
        while((q = p->forward[k]) != NULL && q->key < key)
            p = q;
        update[k] = p;
    }while(--k >= 0);

    if((q != NULL) && (q->key == key)) {
        q->value =value;
        return 0;
    }

    srand(time(NULL));
    k = rand()%MAXLEVEL;

    if(k > l->level) {
        k = ++l->level;
        update[k] = l->head;
    }

    q = (skipNode*)malloc(sizeof(skipNode));
    q->forward = (skipNode**)malloc(MAXLEVEL*sizeof(skipNode*));
    q->key = key;
    q->value = value;
    do {
        p = update[k];
        q->forward[k] = p->forward[k];
        p->forward[k] = q;
    }while(--k >= 0);

    return 1;
}

int delete_(skipList *l,int key) {
    skipNode *p,*q;
    skipNode* update[MAXLEVEL];
    int k = l->level;
    int t = k;
    p = l->head;
    do {
        while((q = p->forward[k]) != NULL && (q->key < key))
            p = q;
        update[k] = p;
    }while(--k >= 0);

    if(q != NULL && q->key == key) {
        for(k=0; k<=t&&(p=update[k])->forward[k] == q;k++)
            p->forward[k] = q->forward[k];
        free(q->forward);
        free(q);

        while(l->head->forward[t] == NULL && t > 0)
            t--;
        l->level = t;
        return 1;
    } else {
        return 0;
    }
}


int search(skipList *l,int key,int *pvalue) {
    skipNode *p,*q;
    int k = l->level;
    p = l->head;
    do {
        while((q = p->forward[k]) != NULL && q->key < key)
            p = q;
    }while(--k >= 0);

    if(q != NULL && q->key == key) {
        *pvalue = q->value;
        return 1;
    }

    return 0;
}

void print(skipList *l) {
    skipNode *p;
    for(p = l->head->forward[0];p != NULL;p = p->forward[0])
        printf("%d  %d\n",p->key,p->value);

}

void FreeSkipList(skipList *l) {
    skipNode *p,*q;
    p = l->head;
    while(p) {
        q = p;
        p = p->forward[0];
        free(q->forward);
        free(q);
    }
}

int main(void)
{
    skipList *skip = InitSkipList();
    int p;
    printf("insert:%d\n",insert(skip,3,300));
    printf("insert:%d\n",insert(skip,8,800));
    printf("insert:%d\n",insert(skip,5,500));
    printf("insert:%d\n",insert(skip,1,100));
    printf("insert:%d\n",insert(skip,7,700));
    printf("insert:%d\n",insert(skip,2,200));
    printf("insert:%d\n",insert(skip,3,333));
    printf("insert:%d\n",insert(skip,10,1000));
    print(skip);
    printf("delete:%d\n",delete_(skip,0));
    printf("delete:%d\n",delete_(skip,5));
    printf("delete:%d\n",delete_(skip,2));
    print(skip);

    printf("search:%d  ",search(skip,3,&p));
    printf("%d\n",p);
    printf("search:%d  ",search(skip,7,&p));
    printf("%d\n",p);
    printf("search:%d  ",search(skip,0,&p));
    printf("%d\n",p);
    FreeSkipList(skip);
    return 0;
}
点赞