Nov 28, 2014 at 3:49am UTC
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 Nov 28, 2014 at 4:11am UTC
Nov 28, 2014 at 5:24am UTC
Could you be more specific about the problem you're having?
Nov 28, 2014 at 5:37am UTC
i just need to know if my insert() function is correct
Nov 28, 2014 at 5:47am UTC
The if statement on line 7 can never be true.
Line 24 can never be reached.
Last edited on Nov 28, 2014 at 5:47am UTC
Nov 28, 2014 at 7:38am UTC
Do you require further help?