我已经实现了跳过列表,但它显示了我非常不清楚的错误
#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;
}
错误说包含不是跳过结构的功能,有些;未命中等,请参阅错误列表
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 ==========
答案 0 :(得分:0)
代码的第80行不应该是这样的 - &gt;
bool SkipSet<T>::contains(const T &search_value) const {... }
我认为这就是你有错误的地方......(或者我猜是这样;))
答案 1 :(得分:0)
SkipSet
应为skipset
skipset<T>contains
应为skipset<T>::contains
<{1}}未声明。
此时我放弃了。