Separate Chaining Hash Table

anything wrong in my insert() function
Hope someone help!

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
template <class T>
class Node
{
public:
	T key;
	Node* next;
};

template <class T>
class Bucket
{
public:
	Node<T> * head;
	int count;
	Bucket()
	{
		head = NULL;
	}
};

template <class T> 
class HASH_TABLE 
{
private:
	Bucket<T> *items; 
	int maxSize; 
public:
	HASH_TABLE(int size, char* FileName); 
	int HashFunction(T key);
	bool check(T key); // Check 
	// operations
	bool insert(T newItem);
	bool search(T key);
}; 


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
bool HASH_TABLE<T>::insert(T newItem)
{
	int hashindex = this->HashFunction(newItem);
	cout << hashindex;
	
	Node<T>* p = new Node <T>;
	if (p != NULL)
	{
		p->key = newItem;
		p->next = NULL;
	}

	if (!items[hashindex].head)
	{
		items[hashindex].head = p;
		items[hashindex].count = 1;
		return true;
	}
	p->next = (items[hashindex].head);
    items[hashindex].head = p;
    items[hashindex].count++;
	return true;
	
	return false;
}
Last edited on
Could you be more specific about the problem you're having?
i just need to know if my insert() function is correct
The if statement on line 7 can never be true.

Line 24 can never be reached.
Last edited on
my mistake
Do you require further help?
Oh yes

here is my code to delete all Hash table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <class T>
int HASH_TABLE<T>::deleteHashTable()
{
//count here to count the number of conflict element
	int count = 0;
	for (int i = 0; i < maxSize; i++)
	{
		Node<T>* p;
		while (items[i].head != NULL)
		{		
			p = items[i].head;
			items[i].head = items[i].head->next;
			delete p;
			if (items[i].count != 1) 
				count ++;
		}
	}
	delete []items;
	return count;
}


is it ok, thanks in advance!!!!
Topic archived. No new replies allowed.