I'm working on this assignment to implement a skip list in c++. Part of the instructions was to do this utilizing a vector of doubly linked lists of nodes and to simulate infinity and negative infinity using the max values accepted for an integer. Also, we cant use pointers only iterators. In my constructor, if I set the height to anything less than 9 when I execute (compiles fine) I get this error:Error in `./a.out': double free or corruption (out): 0x00000000006cb2f0. I'm not sure how since I am not allocating any memory dynamically, also why would it work fine for say 9 or 10 but not for 2? I have included the code for the class.
This is my header file:
#include<iostream>
#include<list>
#include<vector>
using namespace std;
struct SkipNode{
int value;
list<SkipNode>::iterator down;
SkipNode(int x=0):value(x) {};
};
class SkipList{
private:
vector<list<SkipNode>> List;
int size;
int height;
public:
bool isEmpty(){ return List.begin() == List.end();}
void insert(int x);
void remove();
SkipNode search();
SkipList();
void printFirstLevel();
void printList();
};
This is my cpp file:
#include <limits>
#include <ctime>
#include "SkipList.h"
using namespace std;
SkipList::SkipList(): size(0), height(8){
list<SkipNode> Initial;
Initial.push_front(numeric_limits<int>::max());
Initial.push_front(numeric_limits<int>::min());
for(int i =0;i<height;i++){
List.push_back(Initial);
}
for(int y=height-1; y>0; y--){
(*List[y].begin()).down = List[y-1].begin();
(*List[y].end()--).down = List[y-1].end()--;
}
}
And this is the main function I am using to test it:
#include<iostream>
#include<forward_list>
#include<vector>
#include "SkipList.h"
using namespace std;
int main(){
SkipList test;
}
I am literally just creating a new object. If I comment out the following line:
(*List[y].end()--).down = List[y-1].end()--;
then it also runs properly but then its not setting the down iterator I have in each node to the one "undearneath" it.