-counting number of elements from table
-in case of collision, adding node at the end of the table
I have here the source codes:
table.h
#include <cstring>
using namespace std;
#define KeySize 16
#define ValueSize 64
#define DefaultTableSize 20
struct NODE
{
NODE(const char* Key1 = "\0", const char* Name = "\0")
{
strcpy(Key, Key1);
strcpy(FullName, Name);
next = NULL;
}
char Key[KeySize];
char FullName[ValueSize];
NODE *next;
};
class Hashtable
{
private:
int table_size;
NODE** table;
int size;
long hashString(char* Key);
NODE* find(char* Key);
NODE* current_entry;
int current_index;
public:
Hashtable(int T = DefaultTableSize);//constructor
virtual ~Hashtable();//destructor
bool put(NODE *);
bool get(NODE *);
bool contains(char* Key);
void removeAll();
void initIterator();
bool hasNext();
void getNextKey(char* Key);
friend void disp(NODE *);
};
HashFunction.cpp
#include <cstring>
using namespace std;
#define KeySize 16
#define ValueSize 64
#define DefaultTableSize 20
struct NODE
{
NODE(const char* Key1 = "\0", const char* Name = "\0")
{
strcpy(Key, Key1);
strcpy(FullName, Name);
next = NULL;
}
char Key[KeySize];
char FullName[ValueSize];
NODE *next;
};
class Hashtable
{
private:
int table_size;
NODE** table;
int size;
long hashString(char* Key);
NODE* find(char* Key);
NODE* current_entry;
int current_index;
public:
Hashtable(int T = DefaultTableSize);//constructor
virtual ~Hashtable();//destructor
bool put(NODE *);
bool get(NODE *);
bool contains(char* Key);
void removeAll();
void initIterator();
bool hasNext();
void getNextKey(char* Key);
friend void disp(NODE *);
};
main.cpp
#include <iostream>
#include "table.h"
using namespace std;
void dispAll(Hashtable* hashtable);
int main()
{
char temp1[KeySize];
Hashtable* hashtable = new Hashtable();
NODE N1("152","John Smith");
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}
strcpy_s(N1.Key, "1");
strcpy_s(N1.FullName, "Lisa Smith");
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}
strcpy_s(N1.Key, "254");
strcpy_s(N1.FullName, "Sam Doe");
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}
strcpy_s(N1.Key, "152");
strcpy_s(N1.FullName, "Sandra Dee");
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}
strcpy_s(N1.Key, "153");
strcpy_s(N1.FullName, "Ted Baker");
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}
dispAll(hashtable);
}
void dispAll(Hashtable *hashtable)
{
NODE N1;
cout << "\n\nNodes in table:" << endl;
hashtable->initIterator();
while(hashtable->hasNext())
{
hashtable->getNextKey(N1.Key);
hashtable->get(&N1);
disp(&N1);
}
}
Output
Adding node:
Key: 152
FullName: John Smith
Adding node:
Key: 1
FullName: Lisa Smith
Adding node:
Key: 254
FullName: Sam Doe
Adding node:
Key: 153
FullName: Ted Baker
Nodes in table:
Key: 152
FullName: John Smith
Nodes in table:
Key: 153
FullName: Ted Baker
Nodes in table:
Key: 1
FullName: Lisa Smith
Nodes in table:
Key: 254
FullName: Sam Doe
So, there is a collision between John Smith and Sandra Dee! I need to add her at the end of the table and count all nodes! Any ideas?
Thank you!

New Topic/Question
Reply



MultiQuote









|