【问题标题】:skiplist errors what is wrong?跳过列表错误有什么问题?
【发布时间】:2011-11-21 08:39:55
【问题描述】:

我有skiplist的实现,但它显示我非常不清楚的错误

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
using namespace std;
const float P=0.5;
    const int max_level=6;
template<class T>
struct Skip
{

    T value;
    Skip<T> ** forward;//array of pointers
    Skip(int level,const T &value){
        forward=new Skip<T>*[level+1];
        memset(forward,0,sizeof(Skip<T>*)*(level+1));
        this->value=values;
    }


~Skip(){

    delete[]forward;
}

};
template<class T>

struct  skipset
{
    Skip<T>*header;
    int level;
    skipset(){
header=new Skip<T>(max_level,T());
level=0;
    }

    ~skipset()
    {
        delete header;

    }
    void print() const;
    bool contains(const T &) const;
    void insert(const T &) ;
    void delet(const T &);
    };
 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 Skip<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_value) const {
     const SkipNode<T> *x = header;
     for (int i = level; i >= 0; i--) {
         while (x->forward[i] != NULL && x->forward[i]->value < search_value) {
            x = x->forward[i];
         }
     }
     x = x->forward[0];

    return x != NULL && x->value == search_value;


 }
 template<class T>
 void skipset<T>::insert(const T &value){
     Skip<T> *x=header;
     Skip<T> *update[max_level+1];
     memset(update,0,sizeof(Skip<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 Skip<T>(lvl,value);
 for(int i=0;i<=lvl;i++){
     x->forward[i]=update[i]->forward[i];
     update[i]->forward[i]=x;



 }



      }


 }



int main(){





    return 0;
}

error 表示 contains 不是 skipset 结构的函数,some ;未命中等,请查看错误列表

1>------ Build started: Project: skip_list, Configuration: Debug Win32 ------
1>  skip_list.cpp
1>c:\users\dato\documents\visual studio 2010\projects\skip_list\skip_list\skip_list.cpp(80): error C2143: syntax error : missing ';' before '<'
1>c:\users\dato\documents\visual studio 2010\projects\skip_list\skip_list\skip_list.cpp(80): error C2988: unrecognizable template declaration/definition
1>c:\users\dato\documents\visual studio 2010\projects\skip_list\skip_list\skip_list.cpp(80): error C2059: syntax error : '<'
1>c:\users\dato\documents\visual studio 2010\projects\skip_list\skip_list\skip_list.cpp(80): error C2039: 'contains' : is not a member of '`global namespace''
1>c:\users\dato\documents\visual studio 2010\projects\skip_list\skip_list\skip_list.cpp(94): error C2065: 'T' : undeclared identifier
1>c:\users\dato\documents\visual studio 2010\projects\skip_list\skip_list\skip_list.cpp(94): error C2143: syntax error : missing ';' before '{'
1>c:\users\dato\documents\visual studio 2010\projects\skip_list\skip_list\skip_list.cpp(94): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

【问题讨论】:

  • 如何修复你的语法错误?....
  • 你能至少标出哪一行是 80 吗?
  • 您还应该指出错误所在,我们大多数人都没有时间通过​​您的代码来确定行号等!
  • 欢迎您,请正确缩进您的代码并注释错误指向的行 (// line 80),这样我们就不必猜测了。

标签: c++ data-structures skip-lists


【解决方案1】:

SkipSet 应为 skipset
skipset&lt;T&gt;contains 应为 skipset&lt;T&gt;::contains
SkipNode 未声明。

此时我放弃了。

【讨论】:

    【解决方案2】:

    你的代码第80行不应该是这样吗-->

    bool SkipSet<T>::contains(const T &search_value) const {... }
    

    我认为这就是你有错误的地方......(或者我猜是这样;))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多