Help with Hash Tables using an array of linked lists the add function in C++

here is my hasht.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma once
#include <string>
class HashT
{
public:
	HashT(void);
	HashT(int tsize);
	unsigned int hash_gen(int age_value); //takes the age and converts it into an index into the array of linked lists
	void hashtable_add(char *new_name, int new_age);
	void hashtable_display(); //prints the hash table
	void hashtable_delete(char *name, int age);
	~HashT(void);
private:
	class Node{
	public:
		Node(char * newName, int age)
			:age(age), next(0)
		{
			strncpy(name, newName, 1024);
			this -> next = 0;
		}
		Node()
		{
		}
		char name [1024];
		int age;
		Node * next;
	};
	int tsize;
	Node ** myArray;
};


here is my HashT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "hasht.h"
#include <iostream>
#include <iomanip>

using namespace std;

HashT::HashT(void)
	:tsize(10)
{
	myArray = new Node * [10];
	for (int i = 0; i < 10; i++)
		myArray [i] = 0;
	cout<<"Default Constructor Called \n";
}

HashT::HashT(int tsize)
	:tsize(tsize)
{
	myArray = new Node * [tsize];
	for (int i = 0; i < tsize; i++)
		myArray[i] = 0; 
	cout<<"Constructor Called for a tsize of:" << tsize << "\n";
}

unsigned int HashT::hash_gen(int age_value)
{
	unsigned int hashKey;
	hashKey = age_value%tsize;
	return hashKey;
	cout<<"hash_gen function called \n";
}

void HashT::hashtable_add(char *new_name, int new_age)
{ 
	int hashKey = hash_gen(new_age);
	Node * nodePtr;
	nodePtr = myArray[hashKey];
	if (myArray[hashKey] == 0) //adding first node in this bucket
	{
		myArray[hashKey] = new Node (new_name, new_age);
		cout<<"first node in list added with hash index of:" << hashKey<< "\n";
	}
	else
	{
		while (nodePtr ->next != 0)
		{
			//cout<<"while loop in add function called \n";
			nodePtr = nodePtr -> next;
			if(nodePtr ->name == new_name) //prevents duplication
			{
				return;
			}
		}
		nodePtr = new Node(new_name, new_age);
	}
}

void HashT::hashtable_display()
{
	for (int i = 0; i < tsize; i++)
	{
		cout<<myArray[i];
		if (myArray[i] != 0)
		{
			Node * nodePtr;
			nodePtr = myArray[i];
			cout << "Bucket "<< i <<": ";
			while (nodePtr ->name != 0)
			{
				int j;
				j = 0;
				while (nodePtr->name[j] != 0)
				{
					cout<<nodePtr->name[j];
					j++;
				}
				cout << " " << nodePtr ->age << " \n";
				nodePtr = nodePtr -> next;
				//cout<<"first while loop entered in display \n";
			}
			//cout<<"if loop entered in display function \n"; 
		}
	}
	//cout<<"hashtable_display function called \n";
}

void HashT::hashtable_delete(char *name, int age)
{
	int hashKey =hash_gen(age);
	Node * nodePtr;
	Node * prevPtr;
	int num = 1;
	nodePtr = myArray[hashKey];
	while (nodePtr ->name != name)
	{
		prevPtr = nodePtr;
		nodePtr = nodePtr->next;
		num++;
	}
	if (nodePtr -> next == 0) //deleting last node
	{
		prevPtr = 0;
		delete nodePtr;
	}
	else if(num == 1) //deleting first node
	{ //not sure if this is right
		myArray[hashKey] = nodePtr -> next; //might be nodePtr
		delete nodePtr;
	}
	else
	{
		prevPtr ->next = nodePtr ->next;
	}
}



HashT::~HashT(void)
{
	for (int i = 0; i < tsize; i++)
	{
		while (myArray[i] != 0)
		{
			Node * tempPtr;
			tempPtr = myArray [i];
			myArray[i] = tempPtr ->next;
			delete tempPtr;
		}
	}
	delete [] myArray;
}

here is the program to test it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<string>
#include "hasht.h"
using namespace std;


int main(void)
{

 //  the default class constructor
    HashT htable; //bucket size is 10

    
    //  the other class constructor
    HashT hashtable(7); //bucket size is 7
 

// Test1: add data to htable 
       htable.hashtable_add("David", 52); // add data to bucket 2 since hash value is 2
	htable.hashtable_add("Goliath", 45); // add data to bucket 5 since hash value is 5
	htable.hashtable_add("Alan", 31);   // add data to bucket 1 since hash value is 1
cout << "\nTest 1: Printing Hash table with default bucket size :\n" ; 
	htable.hashtable_display();

// Test2: add data to hashtable
 hashtable.hashtable_add("David", 52); // add data to bucket 3 since hash value is 3
	hashtable.hashtable_add("Goliath", 46); // add data to bucket 4 since hash value is 4
	hashtable.hashtable_add("Alan", 33);   // add data to bucket 5 since hash value is 5
cout << "\nTest 2: Printing Hash table with bucket size 7 : \n" ; 
	hashtable.hashtable_display();

// Test3: delete data from htable
       htable.hashtable_delete("David", 52); // delete data from bucket 2 since hash value is 2
cout << "\nTest 3: Printing Hash table with default bucket size :\n" ; 
	htable.hashtable_display();

//  Test4: delete data from hashtable
       hashtable.hashtable_delete("David", 52); // delete data from bucket 3 since hash value is 3
cout << "\nTest 4: Printing Hash table with bucket size 7 : \n" ; 
	hashtable.hashtable_display();

// Test5: add duplicate data to htable
       htable.hashtable_add("Alan", 31);   // data is not added since it already exists
cout << "\nTest 5: Printing Hash table with default bucket size :\n" ; 
	htable.hashtable_display();

// Test6: add duplicate data to hashtable
	hashtable.hashtable_add("Alan", 33);   // data is not added since it already exists
cout << "\nTest 6: Printing Hash table with bucket size 7 : \n" ; 
	hashtable.hashtable_display();

// Test7: adds multiple data items to same bucket in htable
	htable.hashtable_add("Tom", 65); // add data to bucket 5 since hash value is 5
	htable.hashtable_add("Alice", 51);   // add data to bucket 1 since hash value is 1
	htable.hashtable_add("Ben", 25); // add data to bucket 5 since hash value is 5
	htable.hashtable_add("Amy", 21);   // add data to bucket 1 since hash value is 1
cout << "\nTest 7: Printing Hash table with default bucket size :\n" ; //adds multiple data items to same bucket
	htable.hashtable_display();


// Test8: adds multiple data items to same bucket in hashtable
	hashtable.hashtable_add("Ricky", 18); // add data to bucket 4 since hash value is 4
	hashtable.hashtable_add("Adrian", 19);   // add data to bucket 5 since hash value is 5
	hashtable.hashtable_add("Jack", 25); // add data to bucket 4 since hash value is 4
	hashtable.hashtable_add("Grace", 26);   // add data to bucket 5 since hash value is 5
cout << "\nTest 8: Printing Hash table with bucket size 7 : \n" ;  //adds multiple data items to same bucket
	hashtable.hashtable_display();

// Test9: deletes data from buckets with multiple data items in htable
	htable.hashtable_delete("Ben", 25); // delete data from bucket 5 since hash value is 5
	htable.hashtable_delete("Amy", 21);   // delete data from bucket 1 since hash value is 1
cout << "\nTest 9: Printing Hash table with default bucket size :\n" ; //deletes data from buckets with multiple data items
	htable.hashtable_display();

// Test10: deletes data from buckets with multiple data items in hashtable
	hashtable.hashtable_delete("Jack", 25); // delete data from bucket 4 since hash value is 4
	hashtable.hashtable_delete("Grace", 26);   // delete data from bucket 5 since hash value is 5
cout << "\nTest 10: Printing Hash table with bucket size 7 : \n" ;  //deletes data from buckets with multiple data items
	hashtable.hashtable_display();

	
	return 0;

    
}


I am able to print out tests 1 and 2 but after it prints test 2 i get a segmentation fault
This should print all of the names and ages and has 10 different tests

What is wrong with my delete function?
Last edited on
Topic archived. No new replies allowed.