Infinite printing of hash table bug?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Infinite printing of hash table bug?

  1. #1
    Join Date
    Apr 2017
    Posts
    1

    Infinite printing of hash table bug?

    So the point of this code is to generate 200 random integers in the range of 0-9999. These random integers are placed into a hash table of size 551 and placed in random locations. If there are collisions, the items are placed into a linked list and sorted in descending order. My problem is that my code seems to be infinitely printing out the final part of the hash table. I implemented a linked list fashion and made an array of linked lists, with links coming from each location. Can someone see where my error may be? The code gets stuck in my print function.

    Code:
    #include<iostream>
    #include<math.h>
    #include<cstdlib>
    #include<ctime>
    #include<string>
    using namespace std;
    
    
    struct node {
    	int data;
    	node* link;
    	node(int a);
    };
    
    node::node(int a) {
    	data = a;
    	link = NULL;
    }
    
    class nodeUser{
    public:
    	int data;
    	nodeUser();
    	void print();
    	void sortNode(int a, int position, int& switches);
    	node* arrHelper[511];
    };
    
    nodeUser::nodeUser() {
    	data = NULL;
    	for (int i = 0; i < 511; i++) {
    		arrHelper[i] = NULL;
    	}
    }
    
    void nodeUser::print() {
    	node *current = arrHelper[0];
    	for (int i = 0; i < 511; i++) {
    		if (arrHelper[i] == NULL) {
    			current = arrHelper[i];
    		}
    		else if (arrHelper[i] != NULL) {
    			current = arrHelper[i];
    			cout << (i + 1) << ".  ";
    			while (current->link != NULL) {
    				cout << current->data << " ";
    				current = current->link;
    			}
    			cout << endl;
    		}
    	}
    }
    
    void nodeUser::sortNode(int a, int position, int& switches) {
    	node *new_node;
    	new_node = new node(a);
    	node *current = new node(NULL);
    	
    	if (arrHelper[position] == NULL) {
    		arrHelper[position] = new_node;
    		current = arrHelper[position];
    	}
    	else {
    		while (current != NULL) {
    			if (new_node->data < arrHelper[position]->data && arrHelper[position]->link == NULL) {
    				arrHelper[position]->link = new_node;
    			}
    			else if (new_node->data <= arrHelper[position]->data) {
    				new_node->link = arrHelper[position + 1];
    				arrHelper[position]->link = new_node;
    				switches++;
    			}
    			else if(new_node->data > arrHelper[position]->data){
    				new_node->link = arrHelper[position];
    			}
    			current = arrHelper[position + 1];
    			position++;
    		}
    	}
    }
    
    int main() {
    	int a = 0;
    	int switches = 0;
    	nodeUser new_node;
    	int collisions[511];
    	for (int i = 0; i < 511; i++) {
    		collisions[i] = 0;
    	}
    	for (int i = 0; i < 2000; i++) {
    		int num = rand() % 510;
    		int data = rand() % 9999;
    		collisions[num] += 1;
    		new_node.sortNode(data, num, switches);
    	}
    	new_node.print();
    	cout << "This is the amount of switches" << switches;
    	cout << "This is the amount of collision";
    	for (int i = 0; i < 511; i++) {
    		cout << collisions[i] << endl;
    	}
    	cin >> a;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,461

    Re: Infinite printing of hash table bug?

    My problem is that my code seems to be infinitely printing out the final part of the hash table.
    Have you traced through the code using the debugger to see what is happening? Using the debugger you will be able to trace through the code and see the contents of the variables and the flow of execution through the code - so you'll see why a loop isn't terminating as expected.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)