Image

Imagelone_wolf225 wrote in Imagecpp

Really strange error

Ok, my assignment is to implement an ordered dictionary with a skiplist. Whoever makes these problems up, I don't know. But anyways. I am using C++ in Visual Studio.net 2003

If anyone isn't sure what a skiplist is, it basically inserts values, and then has a sort of coin flip that decides how many times to insert it.

The problem is in my insert function I'm using. I'll gladly copy/paste the code if needed, but hopefully won't have to , since it's kind of long and possibly hard to read to some people.

I'm using sort of a linked list sort of thing, with pointers to the next, previous, above, and below keys.

There is a positive infinity marker, and a negative infinity marker. So when I get an element, I test to see how many times I'm going to insert the value (the coin toss thing). So after that, I increase the levels by one, since normally, you have one higher level than how many levels your elements go up to. So I then create two new Nodes in a loop, and build the levels.

Now, just to make sure, I traversed through that to see that it did what I wanted. It works fine. The weird problem I have, is when am inserting the new value for even the first time. I iterate through the skiplist, and stop before I go onto a key larger than my element, or the positive infinity marker. I then create another new Node.

Node *newNode = new Node(elem, k, NULL, NULL, NULL, NULL, NULL);


This node is named differently than the nodes I used to increase the levels in the list. The parameters for that Node are the element, key, spec (if it's going to be used as a positive/negative infinity marker), next pointer, previous pointer, above pointer, and below pointer. The thing is, whenever my program gets to declaring that node, the program crashes. I tried moving the statement around, but wherever the statement is, it crashes. I'm not sure what's wrong with it, since it seems to be declared correctly and everything. Any pointers?

The part of this code that is causing the problem is the insertElement method, but I thought it'd be useful to show you everything, just in case.

coinCount -- How many heads were made (number of times to insert element)
speCount -- counter used to insert elements equal to coinCount
levelcount -- finds what element to add on an above pointer to a new node
I put two lines of comment slashes (///////) above and below where the program crashes.

class Node {
	char element;
	int key;
	string spec;
	Node *next;
	Node *prev;
	Node *above;
	Node *below;
	Node( ): element(NULL), key(NULL), next(NULL), spec(NULL), above(NULL), prev(NULL), below(NULL) { }
	Node(char e, int n): element(e), key(n) { }
	Node(string s): spec(s) {}
	Node(char e, int n, string sp, Node* ne, Node* pr, Node* ab, Node* be): element(e), key(n), spec(sp), next(ne), above(ab), prev(pr), below(be) {}
	friend class skipD;
	friend ostream& operator<<(ostream& os, const skipD skip);
};

class skipD {
	Node *posINF;
	Node *negINF;
	int levels;
public:
	friend class Node;
	skipD(){
		levels = 0;
		posINF = new Node(NULL,NULL,"PINF",NULL,NULL,NULL,NULL);
		negINF = new Node(NULL,NULL,"NINF",NULL,NULL,NULL,NULL);
		posINF->prev = negINF;
		negINF->next = posINF;
	}
	int size(){
		Node *ptr = negINF;
		int count = 0;
		while (ptr->next != posINF){
			count++;
			ptr = ptr->next;
		}
		return count;
	}
	bool isEmpty(){
		if (negINF->next == posINF)
			return true;
		else
			return false;
	}
	void removeElement(int k){
		int counter = 0;
		Node *ptr1 = negINF;
		while ((ptr1->key != k) && (ptr1 != posINF))
			ptr1 = ptr1->next;
		if (ptr1 == posINF){
			cout << "Key not found" << endl;
			return;
		}
		else {
			Node *count = ptr1;
			while (count->above != NULL){
				counter++;
				count = count->above;
			}
			(ptr1->prev)->next = ptr1->next;
			(ptr1->next)->prev = ptr1->prev;
			Node *ptr2;
			while (counter != 0){
				ptr2 = ptr1;
				while (ptr2->above != NULL)
					ptr2 = ptr2->above;
				delete ptr2;
				counter--;
			}
			delete ptr1;
			return;
		}
	}
	void removeAllElements(int k) {
		Node *ptr1;
		Node *ptr2;
		Node *count;
		int counter;
		bool endReach = false;
		while (endReach = false){
			ptr1 = negINF;
			while ((ptr1->key != k) && (ptr1 != posINF))
				ptr1 = ptr1->next;
			if (ptr1 == posINF){
				cout << "No remaining elements to be found with matching key" << endl;
				return;
			}
			else {
				count = ptr1;
				while (count->above != NULL){
					counter++;
					count = count->above;
				}
				(ptr1->prev)->next = ptr1->next;
				(ptr1->next)->prev = ptr1->prev;
				while (counter != 0){
					ptr2 = ptr1;
					while (ptr2->above != NULL)
						ptr2 = ptr2->above;
					delete ptr2;
					counter--;
				}
				if (ptr1->next == posINF)
					endReach = true;
				delete ptr1;
			}
		}
		return;
	}

	void elements(){
		Node *ptr1 = negINF->next;
		while (ptr1 != posINF){
			cout << ptr1->element << " ";
			ptr1=ptr1->next;
		}
		cout << endl;
		return;
	}

	void keys(){
		Node *ptr1 = negINF->next;
		while (ptr1 != posINF){
			cout << ptr1->key << " ";
			ptr1=ptr1->next;
		}
		cout << endl;
		return;
	}

	int flipCoin(){
		int random = rand() % 2;
		return random;
	}

	void insertItem(char elem, int k) {
		Node *pospt = posINF;
		Node *negpt = negINF;
		Node *levelpt;
		int levelptCount;
		int coinCount = 0;
		while (flipCoin() != 0)
			coinCount++;
		if (coinCount > levels)
			levels = coinCount+1;
		int inCount = 0;
		while (pospt->above != NULL && negpt->above !=NULL){
			negpt = negpt->above;
			pospt = pospt->above;
			inCount++;
		}
		while (inCount < levels) {
			Node *newlev1 = new Node(NULL, NULL, "NINF", NULL, NULL, NULL, negpt);
			Node *newlev2 = new Node(NULL, NULL, "PINF", NULL, NULL, NULL, pospt);
			newlev1->next = newlev2;
			newlev2->prev = newlev1;
			negpt->above = newlev1;
			pospt->above = newlev2;
			negpt = negpt->above;
			pospt = pospt->above;
			inCount++;
		}
		int speCount = 0;
		int levelCount;
		Node *insertPtr;
		while (speCount <= coinCount) {
			levelCount = 0;
			negpt = negINF;
			pospt = posINF;
			while (levelCount != speCount){
				negpt = negpt->above;
				pospt = pospt->above;
				levelCount++;
			}
			insertPtr = negpt;
			levelpt = negINF;
		while (((insertPtr->next)->key <= insertPtr->key) && ((insertPtr->next)->spec!= "PINF")){
				insertPtr = insertPtr->next;
			}
			if ((insertPtr->next)->spec == "PINF"){
				Node *newNode;
                        //////////////////////////////////////////////
				newNode = new Node(elem, k, NULL, NULL, NULL, NULL,NULL);
			//////////////////////////////////////////////
				newNode->next = insertPtr->next;
				newNode->prev = insertPtr;
				insertPtr->next = newNode;
				levelptCount = 0;
				if (speCount != 0) {
					while (levelpt->key != k && levelpt->element != elem)
						levelpt = levelpt->next;
					while (levelptCount != speCount) {
						levelpt = levelpt->above;
						levelptCount++;
					}
					levelpt->above = newNode;
					newNode->below = levelpt;
				}
			}
			else {
				Node *newNode = new Node(elem, k, NULL, NULL, NULL, NULL, NULL);
				newNode->next = insertPtr->next;
				newNode->prev = insertPtr;
				insertPtr->next = newNode;
				levelptCount = 0;
				if (speCount != 0) {
					while (levelpt->key != k && levelpt->element != elem)
						levelpt = levelpt->next;
					while (levelptCount != speCount) {
						levelpt = levelpt->above;
						levelptCount++;
					}
					levelpt->above = newNode;
					newNode->below = levelpt;
				}
			}
			speCount++;
		}
		return;
	}
};