C++ skiplist'中存在未知错误;s打印成员函数
我一直在研究skiplist实现,但在打印它时遇到了一些问题。我想知道问题是出在我的打印算法还是我写的另一段代码上 QuadNode.h:C++ skiplist'中存在未知错误;s打印成员函数,c++,skip-lists,C++,Skip Lists,我一直在研究skiplist实现,但在打印它时遇到了一些问题。我想知道问题是出在我的打印算法还是我写的另一段代码上 QuadNode.h: #include "Entry.h" class QuadNode{ friend class SkipList; public: QuadNode(Entry* e= NULL) :prev(NULL), next(NULL), below(NULL), above(NULL), entry(e){}
#include "Entry.h"
class QuadNode{
friend class SkipList;
public:
QuadNode(Entry* e= NULL)
:prev(NULL), next(NULL), below(NULL), above(NULL), entry(e){}
private:
Entry* entry;
int level;
QuadNode* prev;
QuadNode* next;
QuadNode* below;
QuadNode* above;
};
条目h:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
//declaration of entry class
class Entry{
friend class SkipList;
public:
Entry(){};
void random();
int getKey(){return key;}
string getData(){return data;}
void toString();
private:
//private constructor so user can't make entry with same key and data as "special end entry"
Entry(int k, string str)
:key(k), data(str){}
int key;
string data;
};
SkipList.cpp:
SkipList::SkipList()
:n(0),height(0){
//creating the endpoints and setting their entries and making them point to eachother
start = new QuadNode(new Entry(INT_MIN,""));
end = new QuadNode(new Entry(INT_MAX,""));
start->next = end;
end->prev = start;
}
...
void SkipList::put(int k, string v){
int count = 0;
while(rand()%2 == 0){
count++;
}
//adding new levels if they are needed
for(int i = 0; i < (count - height) + 1; i++){
start->above = new QuadNode(new Entry(INT_MIN,""));
start->above->below = start;
start = start->above;
start->next = new QuadNode(new Entry(INT_MAX,""));
start->next->prev = start;
}
if(count - height >= 0){
height += (count - height) + 1;
}
int currHeight = height;
QuadNode* curr = start;
QuadNode* recentInsert = NULL;
while(curr != NULL){
if(k < curr->next->entry->key){//if the key is less than the next node we either insert and move down or just move down
if (currHeight <= count){//if the current height is less than or equal to the "heads" count and we are in the right insertion spot, insert the a new node
//creating the new node
QuadNode* insertee = new QuadNode(new Entry(k,v));
//linking it to the node above it
insertee->above = recentInsert;
if(recentInsert != NULL){
recentInsert->below = insertee;
}
//linking it in on its respective level
insertee->next = curr->next;
curr->next->prev = insertee;
insertee->prev = curr;
curr->next = insertee;
//storing it for use the next level
recentInsert = insertee;
}
//moving down to the next lower level
curr = curr->below;
currHeight--;
}
else if(k > curr->next->entry->key){//if the key is greater than the next nodes key then we move foreward
curr = curr->next;
}
else if(k == curr->next->entry->key){//if the key is already present on that level, change the key and move down
curr->next->entry->data = v;
//links it to last insert
curr->next->above = recentInsert;
recentInsert->below = curr->next;
recentInsert = curr->next;
//moves down to next level
curr = curr->below;
currHeight--;
}
}
}
...
void SkipList::print(){
QuadNode* curr = start;
curr = curr->below;
QuadNode* bottomLeft = start;
//going to bottom level
while(bottomLeft->below != NULL){
bottomLeft = bottomLeft->below;
}
QuadNode* A;
QuadNode* B;
while(curr != NULL){
A = curr;
A = A->next;
B = bottomLeft;
B = B->next;
cout << "-inf";
while(A != NULL){
cout<<"--";
if(A->entry->key == B->entry->key){
if(A->next != NULL){
cout<<A->entry->key;
}
A = A->next;
B = B->next;
}
if(A != NULL && A->entry->key != B->entry->key){
B = B->next;
cout<<"--";
}
}
cout << "inf" << endl;
curr = curr->below;
}
}
我得到的结果是:
-inf------24--------inf
-inf--13--24--54--74--inf
我想要的输出是:
-inf------24----------inf
-inf--13--24--54--74--inf
一双“一”字的。这是因为我的打印功能还是skiplist是如何设计的?
谢谢
-inf------24--------inf
-inf--13--24--54--74--inf
-inf------24----------inf
-inf--13--24--54--74--inf