Hi, while i was doing an assignment for class to write a skip list i get a error telling me the struct Node isnt defined at one function while it works fine for other ones.
i been looking at it for quite a few days already but i cant figure out whats wrong.
hope someone could point me to the right direction
#include "SkipList.h"
#include <stdlib.h>
usingnamespace std;
SkipList::SkipList() {
head = new Node;
head->level = new Node*[16];
tail = new Node;
tail->level = new Node*[16];
for(int i =0; i<16;i++) {
head->level[i] = tail;
tail->level[i] = NULL;
}
}
void SkipList::add(string s){
Node* found = find(s);
if(found != NULL){ //if the value is found increment the occurence
found->occurences++;
}else // if its not found create a new node with the value and place it into the list
{
srand(time(NULL));
int lvl = rand()%16;
Node *temp = new Node;
temp->level = new Node*[lvl];
temp ->occurences = 1;
temp->val = s;
Node* start = head;
for(int i = 15; i >=0;i--){ // decrement start from the highest level
while(start->level[i] != NULL && compare(s, start->level[i]->val) >= 0){ // loop until reach the end of the level or the value of the next
if(start->level[i]->val == s) start->level[i]->occurences++; // node is greater than the current s
else {
}
}
}
}
}
/**
* find and return the node containing string
* if no node is found return NULL
*/
Node* SkipList::find(string s){
Node* start = head;
Node* temp = NULL;
for(int i = 15; i >=0;i--){ // decrement start from the highest level
while(start->level[i] != NULL && compare(s, start->level[i]->val) >= 0){ // loop until reach the end of the level or the value of the next
if(start->level[i]->val == s) temp = start->level[i]->val; // node is greater than the current s
else start = start->level[i];
}
}
return temp;
}
string SkipList::getFirst(){
return head->level[0]->val;
}
void SkipList::removeFirst(){
Node* temp = head->level[0];
int nextlength = sizeof(head->level)/sizeof(head->level[0]);
for (int i = 0; i < 16; i ++){
if ( i < nextlength){
head->level[i] = temp->level[i];
}
}
delete temp;
}
/**
* compare 2 strings a and b
* return 1 if a > b
* return -1 if a < b
* return 0 if a == b
*/
int SkipList::compare(string a, string b){
if (a == b) return 0;
for(int i =0; i < min(a.length(),b.length()); i ++){
if(a[i] > b[i]) return 1;
if(a[i] < b[i]) return -1;
}
if(a.length()>b.length()) return 1;
elseif(b.length()>a.length()) return -1;
}
SkipList::~SkipList() {
// TODO Auto-generated destructor stub
}
the error occurs at the find method
this is the exact error message
g++ -O0 -g3 -Wall -c -fmessage-length=0 -oSkipList.o ../SkipList.cpp
../SkipList.cpp:52:1: error: ‘Node’ does not name a type
../SkipList.cpp: In member function ‘int SkipList::compare(std::string, std::string)’:
../SkipList.cpp:86:45: warning: comparison between signed and unsigned integer expressions
../SkipList.cpp:92:1: warning: control reaches end of non-void function
Build error occurred, build is stopped