Table skip implementation

Source: Internet
Author: User

Table skip implementation
I recently read the jump table and the article "Skip tables ".

Bytes. Although I think it should be a memory problem, I cannot find it for a long time ~~~~ Please be sure to inform me of the error.

Later, I only followed the above blog post with a C image, and the code was in the second segment.

#include 
 
  #include 
  
   #include 
   
    #include 
    
     using namespace std;template
     
      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
      
       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
       
        void skipList
        
         ::print(){ skipNode *p; for(p = head->forward[0]; p; p = p->forward[0]) { cout<
         
          key<<" "<
          
           value<
           
            bool skipList
            
             ::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
             
              bool skipList
              
               ::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
               
                bool skipList
                
                 ::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
                 
                   skip; cout<<"insert:"<
                  
                   



#include 
                    
                     #include 
                     
                      #include 
                      
                       #define MAXLEVEL 8struct 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
                       
                        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;}
                       
                      
                     
                    


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.