I have a HashTable class. The purpose of this is to practice how to produce Hash Table. My current issue right now is the destructor. I should be seeing on the console, this:
But instead i see this:
I've ran gdb ./app which gives me this:
#include <iostream>
#include "HashTable.h"
using namespace std;
int main(){
HashTable table;
table.initTable();
table.addNode("Software");
table.addNode("Engineering");
table.addNode("Art");
table.addNode("Programming");
table.addNode("Miscellanous");
table.displayByIndex();
return 0;
}
HashTable Header:
#ifndef _HASHTABLE_H_
#define _HASHTABLE_H_
#include <iostream>
#include "Hash.h"
class HashTable{
private:
static const int TABLESIZE = 5;
Hash* head;
public:
HashTable();
~HashTable();
void initTable();
int hashKey(char*);
int quadProbing(int,int);
int hashKeyWithProbing(char*);
bool isEmpty();
void addNode(char*);
void displayByIndex();
void searchByKey(char*);
};#endif
Here it my Hash Table CPP, i included the Constructor/Destructor and init functions. The other functions arent included since i dont utilize the 'new' keyword. If its needed i will include it [hope to make this thread small]
#include "HashTable.h"
#include <cstring>
HashTable::HashTable(){
head = new Hash[TABLESIZE];
}
HashTable::~HashTable(){
Hash* destList = NULL;
for(int i = 0; i< TABLESIZE; i++){
destList = &head[i];
while(destList != NULL){
head = head->getNext();
delete destList;
destList = head;
}
}
head = NULL;
delete [] head;
}
void HashTable::initTable(){
for(int i = 0; i < TABLESIZE; i++){
Hash *traverseHeadArray = &head[i];
traverseHeadArray->setKey("EMPTY");
traverseHeadArray->setNext(NULL);
traverseHeadArray = NULL;
}
}
int HashTable::hashKey(char *tempKey){
//Find the asc value for the string
//add them together
//modules by the total table size
//then return the index (remainder using modules)
//Index is were that key will be stored
int index = 0;
for(int i = 0; i < strlen(tempKey); i++){
index += tempKey[i];
}
return index % TABLESIZE;
}//DONE
int HashTable::quadProbing(int index, int counter){
return (index + counter*counter) % TABLESIZE;
}//DONE
int HashTable::hashKeyWithProbing(char* key){
int index = hashKey(key);
int count = 1;
while(head[index].getKey() != key && head[index].getKey() != "EMPTY"){
index = quadProbing(index, count);
count++;
}
return index;
}//DONE
void HashTable::addNode(char* tempValue){
Hash* newNode = new Hash;
newNode->setKey(tempValue);
newNode->setNext(NULL);
int index = hashKey(tempValue);
int counter = 1;
while(head[index].getKey() != tempValue && head[index].getKey() !="EMPTY")
{
index = quadProbing(index, counter);
counter++;
}
Hash* traverse = &head[index];
if(isEmpty() || traverse->getKey() == "EMPTY"){
traverse->setKey(newNode->getKey());
traverse->setNext(newNode->getNext());
}else{
while(traverse->getNext() != NULL)
traverse = traverse->getNext();
traverse->setNext(newNode);
}
traverse = NULL;
delete newNode;
}
void HashTable::displayByIndex(){
for(int i = 0; i < TABLESIZE; i++){
Hash *traverse = &head[i];
std::cout << "----------------------------------------" << std::endl;
std::cout << "INDEX: " << i << std::endl;
while(traverse != NULL){
std::cout << "Key: " << traverse->getKey() << std::endl;
traverse = traverse->getNext();
}
std::cout << "----------------------------------------" << std::endl;
traverse = NULL;
}
}
bool HashTable::isEmpty(){
return (head == NULL);
}
void HashTable::searchByKey(char* key){
int index = hashKeyWithProbing(key);
if(isEmpty())
std::cout << "Empty Bucket\n";
else{
Hash* traverse = &head[index];
while(traverse != NULL && traverse->getKey() != "EMPTY"){
std::cout << "TOPIC KEY: " << traverse->getKey() << std::endl;
traverse = traverse->getNext();
}
traverse = NULL;
}
}
Here is my Hash Class header and CPP files:
#ifndef _HASH_H_
#define _HASH_H_
class Hash{
private:
char* key;
Hash* next;
public:
Hash();
~Hash();
void setKey(char*);
void setNext(Hash* const);
char* getKey();
Hash* getNext();
};#endif
#include "Hash.h"
#include <iostream>
Hash::Hash(){
key = NULL;
next = NULL;
}
Hash::~Hash(){
// TODO Destructor
}
void Hash::setKey(char* tempKey){
this->key = tempKey;
}
void Hash::setNext(Hash* const tempNext){
this->next = tempNext;
}
char* Hash::getKey(){
return key;
}
Hash* Hash::getNext(){
return next;
}
In your code, you allocating only the getNext() nodes,
so you should delete only them, (you tried to delete &head[i] which wasn't allocated at all).
There is still an exception for double freeing some node, you should debug and follow the stack for figuring it out.
HashTable::~HashTable(){
for(int i = 0; i< TABLE_SIZE; i++){
Hash* node = head[i].getNext();
while(node != nullptr){
Hash* temp = nullptr;
if (node -> getNext()) {
temp = node->getNext();
}
delete node;
node= temp;
}
}
delete[] head;
}
Output:
----------------------------------------
INDEX: 0
Key: Art
----------------------------------------
----------------------------------------
INDEX: 1
Key: Engineering
----------------------------------------
----------------------------------------
INDEX: 2
Key: Miscellanous
----------------------------------------
----------------------------------------
INDEX: 3
Key: Software
----------------------------------------
----------------------------------------
INDEX: 4
Key: Programming
----------------------------------------
double free or corruption (fasttop)
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
Related
I have to make two types of list data types for my data structures class, an ArrayList and a Linkedlist. The ArrayList will be based on arrays and the LinkedList will be based on nodes, then i need to compare their performance on adding (on the desired position), updating and removing elements from front, end and middle of the list for lists with 100,1000 and 10000 elements. The point of the homework is to show that linked lists are faster. I made both structures and all the functions but when i test them the result always shows that my array lists are faster. Can someone please tell me why my linked lists works slower?
struct ArrayList {
int* head;
int size;
void build();
void add(int, int);
void update(int, int);
void remove(int);
void print();
void clear();
};
struct Node {
int data;
Node* next;
};
struct LinkedList {
Node* head;
int size;
void build();
void add(int, int);
void update(int, int);
void remove(int);
void print();
void clear();
};
void LinkedList::build() {
head = NULL;
size = 0;
}
void LinkedList::add(int index, int value) {
Node* n = new Node();
n->data = value;
if (head == NULL) { // adding to an empty list
head = n;
size++;
}
else {
if (index == 0) { // adding at front
n->next = head;
head = n;
size++;
}
else if (index < size) { // adding inside
Node* p = head;
int c = 1;
while (c < index) {
p = p->next;
c++;
}
n->next = p->next;
p->next = n;
size++;
}
else if (index == size) { // adding at the end
Node* p = head;
while (p->next) {
p = p->next;
}
p->next = n;
size++;
}
else {
cout << "index out of range";
}
}
}
void ArrayList::build() {
size = 0;
head = new int[size];
}
void ArrayList::add(int index, int value) {
int* arr = new int[size + 1];
if (index == size) { //adding at the end
for (int i = 0; i < size; i++) {
arr[i] = head[i];
}
arr[size] = value;
delete head;
head = arr;
size++;
}
else if (index < size) { //adding inside or front
for (int i = 0; i < index; i++) {
arr[i] = head[i];
}
arr[index] = value;
for (int i = index; i < size; i++) {
arr[i + 1] = head[i];
}
delete head;
head = arr;
size++;
}
else {
cout << "index out of range.";
}
}
Testing code
ArrayList* a = new ArrayList();
LinkedList* l = new LinkedList();
l->build();
a->build();
int n = 10000;
for (int i = 0; i < n; i++) {
l->add(i, i);
a->add(i, i);
}
auto begin = std::chrono::high_resolution_clock::now();
a->add(n, 1);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
auto begin2 = std::chrono::high_resolution_clock::now();
l->add(n, 1);
auto end2 = std::chrono::high_resolution_clock::now();
auto elapsed2 = std::chrono::duration_cast<std::chrono::nanoseconds>(end2 - begin2);
cout << "with " << n << " " << "elements" << endl;
cout << endl;
cout << "ArrayList adding time: " << elapsed.count() << " nano-seconds" << endl;
cout << endl;
cout << "LinkedList adding time: " << elapsed2.count() << " nano-seconds" << endl;
I have implemented a basic linked list in order to prepare for a more difficult one as an assignment. I can't seem to be able to print the contents of a node without printing out random symbols.
I know this means I am accessing bad memory but I cannot identify where or how it is happening.
Can someone please help me fix this error?
built with g++ on linux
Header File
#ifndef LLIST_H
#define LLIST_H
#include <string>
using namespace std;
struct node
{
int age;
string name;
string hair;
int height;
node* next;
};
node loadNode(node a, string b);
class linkedlist
{
private:
node* head = NULL;
node* tail = NULL;
int size;
public:
linkedlist();
linkedlist(node* n[], int si);
int getSize();
void print();
void addNode(node add);
void del(string record);
node sort(node sortee);
void printList();
node * getHead();
};
#endif
Implementation
#include "list.h"
#include <iostream>
#include <sstream>
#include <string>
node loadNode(node a, string b)
{
int counter = 0;
string ag = "";
string nam = "";
string hai = "";
string hei = "";
for(int i = 0; i < b.length(); i++)
{
if(b[i] == ',')
{
counter++;
i++;
}
if(counter == 0)
ag = ag + b[i];
else if(counter == 1)
nam = nam + b[i];
else if(counter == 2)
hai = hai + b[i];
else
hei = hei + b[i];
}
stringstream s(hei);
stringstream o(ag);
int f,g;
s >> f;
o >> g;
a.hair = hai;
a.height = f;
a.age = g;
a.name = nam;
return a;
}
linkedlist::linkedlist()
{
head = NULL;
tail = NULL;
size = 0;
}
linkedlist::linkedlist(node a[],int si)
{
head = NULL;
tail = NULL;
size = 0;
for(int i = 0; i < si; i++)
{
addNode(a[i]);
}
}
void linkedlist::addNode(node added)
{
node * pointer = new node;
//if the first element
if(size == 0)
{
tail = pointer;
pointer->next = NULL;
}
pointer->next = head;
head = pointer;
//add data members
pointer-> age = added.age;
pointer-> hair = added.hair;
pointer-> name = added.name;
pointer-> height = added.height;
size++;
//time to exit!
pointer = NULL;
delete pointer;
}
node * linkedlist::getHead()
{
node * temp = head;
return temp;
}
void linkedlist::print()
{
node* iterator = (node*)malloc(sizeof(node));
iterator = head;
while(iterator != NULL)
{
printNode(iterator);
iterator = (iterator->next);
}
iterator = NULL;
delete iterator;
}
void printNode(node* printed)
{
cout << printed->name << endl;
cout << printed->age << endl;
cout << printed->height << endl;
cout << printed->hair<< endl;
}
main program
#include "list.h"
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string k = "21,Ces Goose,brown,5";
string a = "25, SteveO, Brown, 6";
string b = "23, Jimmy , Brown, 5";
node d,f,c;
d = loadNode(d,k);
f = loadNode(f,a);
c = loadNode(c,b);
node lib[3] = {d,f,c};
linkedlist doublel = linkedlist(lib, 3);
doublel.print();
return 0;
}
The output I am getting is:
6304000
#g���
#C.M. please let me know if this fixes the issue
void linkedlist::addNode(node added)
{
node * temp = new node;
//add data members
temp-> age = added.age;
temp-> hair = added.hair;
temp-> name = added.name;
temp-> height = added.height;
//if the first element
if(size == 0)
{
head = temp;
tail = temp;
temp->next = NULL;
size++;
}
else
{
temp->next = head;
head = temp;
size++;
//time to exit!
temp = NULL;
delete pointer;
}
}
also changed linkedlist doublel = linkedlist(lib, 3); to
linkedlist * doublel = new linkedlist(lib,3);
doublel->print();
in main() file
I'm trying to make a stack of hash tables with the push and pop functions. I have the push and pop down, but is there a way to modify the stack to put H1 and H2 into Stack One? Currently, my stack holds ints for testing purposes.
Below are all headers and source files. I'd really like a solution for this issue. Thanks.
Stack.h
#ifndef STACK_H
#define STACK_H
#include <string>
using namespace std;
class Stack
{
private:
struct item
{
int value;
item* prev;
};
item* stackPtr;
public:
void Push(int value);
void Pop();
void ReadItem(item* r);
void Print();
Stack(); //called when stack is created
~Stack();//called when stacked item is ended
};
#endif /* STACK_H */
Stack.cpp
#include <iostream>
#include <cstdlib>
#include "Stack.h"
using namespace std;
Stack::Stack()
{
stackPtr = NULL;
}
Stack::~Stack()
{
item* p1; //pointer 1
item* p2; //pointer 2
p1 = stackPtr;
while(p1 != NULL)
{
p2 = p1;
p1 = p1->prev;
p2->prev = NULL;
delete p2;
}
}
void Stack::Push(int value)
{
item* n = new item;
n->value = value;
if(stackPtr == NULL)
{
stackPtr = n;
stackPtr->prev = NULL;
}
else
{
n->prev = stackPtr;
stackPtr = n;
}
}
void Stack::ReadItem(item* r)
{
cout << "value: " << r->value << endl;
cout << "------------------\n";
}
void Stack::Pop()
{
if(stackPtr == NULL)
{
cout << "The stack is empty\n";
}
else
{
item* p = stackPtr;
ReadItem(p);
stackPtr = stackPtr->prev;
p->prev = NULL;
delete p;
}
}
void Stack::Print()
{
item* p = stackPtr;
while(p != NULL)
{
ReadItem(p);
p = p->prev;
}
}
Hashtable.h
#include <cstdlib>
#include <iostream>
#include <string>
#ifndef HASHTABLE_H
#define HASHTABLE_H
class HashTable
{
private:
static const int tableSize = 10;
//everything in the braces are what makes the item
struct obj
{
int name;
obj* next;
};
obj* HASHTBL[tableSize];
public:
HashTable();
//Hash is the function represents where in the hash table
//we will store the key
//take a string stored in variable
int Hash( int key);
void AddObj(int name);
int ItemsinBucket(int index);
void PrintTable();
};
#endif /* HASHTABLE_H */
Hashtable.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "HashTable.h"
using namespace std;
//Takes from the HashTable class in HashTable.h
HashTable::HashTable()
{
for(int x = 0; x < tableSize; x++)
{
HASHTBL[x] = new obj;
HASHTBL[x]->name = NULL;
HASHTBL[x]->next = NULL;
}
}
void HashTable::AddObj( int name)
{
int index = Hash(name);
if(HASHTBL[index]->name == NULL)
{
HASHTBL[index]->name = name;
}
else
{
obj* Ptr = HASHTBL[index];
obj* n = new obj;
n->name = name;
n->next = NULL;
while(Ptr->next !=NULL)
{
Ptr = Ptr->next;
}
Ptr->next = n;
}
}
int HashTable::ItemsinBucket(int index)
{
int count = 0;
if (HASHTBL[index]->name == NULL)
{
return count;
}
else
{
count++;
obj* Ptr = HASHTBL[index];
while(Ptr->next != NULL)
{
count++;
Ptr = Ptr->next;
}
}
return count;
}
void HashTable::PrintTable()
{
int number;
for(int x = 0; x< tableSize; x++)
{
number = ItemsinBucket(x);
cout << "------------------------\n";
cout << "index = " << x << endl;
cout << HASHTBL[x]->name << endl;
cout << "The num of items in index = "<< number << endl;
cout << "------------------------\n";
}
}
int HashTable::Hash(int key)
{
//defining HashTable function:
int HashTable = 0;
int index;
//will return integer value...or length of string you pass in
for(int x = 0; x < key-1; x++)
{
HashTable = HashTable + x;
}
//so the hash table will take a number and mod it to return the remainder
//the remainder is the index
index = HashTable % tableSize;
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "Stack.h"
#include "HashTable.h"
using namespace std;
int main(int argc, char** argv)
{
Stack One;
One.Push(3);
One.Push(0);
One.Push(4);
One.Push(5);
HashTable H1;
H1.AddObj(4);
H1.AddObj(23);
H1.AddObj(200);
H1.AddObj(10);
H1.AddObj(15);
H1.AddObj(42);
H1.AddObj(33);
H1.AddObj(44);
H1.AddObj(55);
H1.AddObj(5);
H1.AddObj(9);
H1.AddObj(90);
HashTable H2;
H2.AddObj(10);
H2.AddObj(90);
H2.AddObj(99);
H2.AddObj(34);
H2.AddObj(88);
H2.AddObj(14);
H2.AddObj(87);
H2.AddObj(18);
H2.AddObj(54);
H2.AddObj(56);
H2.AddObj(6);
H2.AddObj(2);
One.Print();
cout << "\n\n\n";
H1.PrintTable();
/*
cout << "1. Push Stack Element" << endl;
cout << "2. Pop Stack Element" << endl;
cout << "3. Search Hash Table" << endl;
*/
return 0;
}
my code is suppose to create a singly linked list using and array of nodes.
each Node has variable item which hold data and variable next which holds the index of the next node in the list. the last node has -1 in its next data field to simulate a nullptr. head holds the index of the first node in the list.
for some reason when i try to create new memory the program throws an instance of bad_alloc
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
#include "ArrayList.h"
#include <iostream>
using namespace std;
ArrayList::ArrayList(char ch){
array = new Node[Size];
(array[0]).item = ch;
(array[0]).next = 1;
free = 1;
head = 0;
}
bool ArrayList::cons(char ch) {
if (this->isFull()){
this->doubleArray();
}
(array[free]).item = ch;
(array[free]).next = head;
head = free;
free++;
// cout << free << endl;
return true;
}
bool ArrayList::isFull() const{
int current = head;
int count =0;
while( (array[current]).next != -1 ){
count++;
current = (array[current]).next;
}
return (count == Size) ? true : false;
}
bool ArrayList::doubleArray() {
int oldSize = Size;
Size *=2;
Node* newArray = new Node[Size]; // problem occurs here, i think
int count =0;
while(oldSize >= count ){
(newArray[count]).item = (array[count]).item;
(newArray[count]).next = (array[count]).next;
count++;
}
free = count +1;
delete [] array;
array = newArray;
cout <<"free: "<< free << endl;
return true; // come up with thiss
}
void ArrayList::print() const{
if (head == -1) return;
int current = head;
cout << "[ ";
while( (array[current]).next != -1){
cout << (array[current]).item <<" ";
current = (array[current]).next;
}
cout << (array[current]).item <<"]";
return;
}
///////////////////////////////////
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
#include <iostream>
using namespace std;
class Node{
public:
char item;
int next;
Node(){
next = -1;
}
Node(char input){
this->item = input;
next = -1;
}
};
class ArrayList{
public:
ArrayList(char ch);
int length() const;
void print() const;
private:
bool doubleArray();
bool isFull() const;
Node* array;
int Size = 5;
int head = -1;
int free = 0;
};
#endif
////////////////////////////////////////
#include <iostream>
#include "ArrayList.h"
using namespace std;
int main(){
ArrayList list('1');
list.cons('2');
list.cons('3');
list.cons('4');
list.cons('5');
list.cons('6');
list.cons('7');
list.print();
//cout << list.length();
return 0;
}
Below is my code
to create hashtable with key as Char* and value as Function pointer
// hash1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#define SIZE_KEY 16
#define SIZE_VALUE1 64
#define SIZE_VALUE2 16
#define DEFAULT_TABLESIZE 101
using namespace std;
typedef void (*FunctionPtr)();
typedef void (*FunctionPtr1)();
void (*FunctionPtr2)();
void ping(){
cout<<"hello";
}
void refresh(){
cout<<"refresh";
}
typedef struct NODE
{
NODE(char* Key1,FunctionPtr func_ptr)
{
strcpy_s(Key,Key1);
FunctionPtr1 func_ptr1;
func_ptr1=func_ptr;
next = NULL;
}
NODE(){
}
char Key[SIZE_KEY];
FunctionPtr1 func_ptr1[SIZE_VALUE1];
NODE *next;
};
class Hashtable
{
private:
int table_size;
NODE** table;
int size;
long hashString(char* Key);
NODE* find(char* Key);
NODE* current_entry;
public:
int current_index;
Hashtable(int T = DEFAULT_TABLESIZE);//constructor
virtual ~Hashtable();//destructor
bool put(NODE *);
bool get(NODE *);
bool contains(char* Key);
void removeAll();
int getSize();
void initIterator();
bool hasNext();
void getNextKey(char* Key);
friend void disp(NODE *);
};
Hashtable::Hashtable(int T)
{
size = 0;
table_size = T;
table = new NODE*[table_size];
for(int i=0; i<table_size; i++)
{
table[i] = NULL;
}
}
Hashtable::~Hashtable()
{
removeAll();
delete[] table;
}
bool Hashtable::put(NODE *N)
{//start put
if(find(N->Key) != NULL)
{
return false;
}
//NODE* entry = new NODE( N->Key ,*(FunctionPtr *)N->func_ptr1 );
NODE* entry = new NODE( N->Key ,*(FunctionPtr *)N->func_ptr1 );
int bucket = hashString(N->Key);
entry->next = table[bucket];
table[bucket] = entry;
size++;
return true;
}//end put
bool Hashtable::get(NODE* N)
{//start get
NODE* temp = find(N->Key);
if(temp == NULL)
{
*(FunctionPtr *)N->func_ptr1 = refresh;
return false;
}
else
{
*(FunctionPtr *)N->func_ptr1= *(FunctionPtr *)temp->func_ptr1;
return true;
}
}//end get
bool Hashtable::contains(char* Key)
{//start contains
if(find(Key) == NULL)
{
return false;
}
else
{
return true;
}
}//end contains
void Hashtable::removeAll()
{//start removeAll
for(int i=0; i<table_size; i++)
{
NODE* temp = table[i];
while(temp != NULL)
{
NODE* next = temp->next;
disp(temp);
delete temp;
temp = next;
}
}
size = 0;
}//end removeAll
int Hashtable::getSize()
{
return size;
}
NODE* Hashtable::find(char* Key)
{ //start find
int bucket = hashString(Key);
NODE* temp = table[bucket];
while(temp != NULL)
{
if(strcmp(Key, temp->Key) == 0)
{
return temp;
}
temp = temp->next;
}
return NULL;
}//end find
long Hashtable::hashString(char* Key)
{//start hashString
int n = strlen(Key);
long h = 0;
for(int i=0; i<n; i++)
{
//To get almost fair distributions of NODEs over the array
h = (h << 3) ^ Key[i];
}
return abs(h % table_size );
}//end hashString
void Hashtable::initIterator()
{//start initIterator
current_entry = NULL;
current_index = table_size;
for(int i=0; i<table_size; i++)
{
if(table[i] == NULL)
{
continue;
}
else
{
current_entry = table[i];
current_index = i;
break;
}
}
}//end initIterator
bool Hashtable::hasNext()
{
if(current_entry == NULL)
{
return false;
}
else
{
return true;
}
}
void Hashtable::getNextKey(char* Key)
{
if(current_entry == NULL)
{
Key[0] = '\0';
return;
}
strcpy(Key, current_entry->Key);
if(current_entry->next != NULL)
{
current_entry = current_entry->next;
}
else
{
for(int i=current_index+1; i<table_size; i++)
{
if(table[i] == NULL)
{
continue;
}
current_entry = table[i];
current_index = i;
return;
}
current_entry = NULL;
current_index = table_size;
}
}
void dispAll(Hashtable* hashtable);
int main()
{
char temp1[SIZE_KEY];
Hashtable* hashtable = new Hashtable();
NODE N1("1",ping);
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding NODE: ";
disp(&N1);
hashtable->put(&N1);
}
// dispAll(hashtable);
strcpy(N1.Key, "314");
*(FunctionPtr *) N1.func_ptr1=refresh;
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding NODE: ";
disp(&N1);
hashtable->put(&N1);
}
/* strcpy(N1.Key, "320");
*(FunctionPtr *) N1.func_ptr1= ping;
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding NODE: ";
disp(&N1);
hashtable->put(&N1);
}
strcpy(N1.Key, "768");
*(FunctionPtr *)N1.func_ptr1= refresh;
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}
strcpy(N1.Key, "756");
*(FunctionPtr *) N1.func_ptr1= refresh;
if(!hashtable->contains(N1.Key))
{
cout << "\nAdding node: ";
disp(&N1);
hashtable->put(&N1);
} */
dispAll(hashtable);
// strcpy(temp1,"314");
// hashtable->remove(temp1);
// cout << "\n\nAfter removing 314:" << endl;
// dispAll(hashtable);
cout << "\n\nDestroying hashtable:" << endl;
delete hashtable;
return 0;
}
void disp(NODE *N1)
{
cout << "\nKey: " << N1->Key << "\nFunction "
<< N1->func_ptr1 << endl;
// FunctionPtr2();
}
void dispAll(Hashtable *hashtable)
{
NODE N1;
cout << "\n\nCurrent nodes in hashtable:" << endl;
hashtable->initIterator();
while(hashtable->hasNext())
{
//cout<<"Current Index === "<<hashtable->current_index;
hashtable->getNextKey(N1.Key);
hashtable->get(&N1);
disp(&N1);
}
}
each time data is written in hash table VALUE cointains the same address :(
i want address of particular function that i will send..
Some of the problems may be in struct NODE.
typedef struct NODE
{
NODE(char* Key1,FunctionPtr func_ptr)
{
strcpy_s(Key,Key1);
FunctionPtr1 func_ptr1; // <-- Problem may be here
func_ptr1=func_ptr;
next = NULL;
}
NODE(){
}
char Key[SIZE_KEY];
FunctionPtr1 func_ptr1[SIZE_VALUE1]; // <-- And here
NODE *next;
};
You declared a local func_ptr1 in NODE::NODE(char*, FunctionPtr), and assign the parameter func_ptr to func_ptr1. Thus, func_ptr is assigned to a local variable, not a member variable. And, once the constructor returns, nothing about the function pointer is remembered.
Another problem: why is NODE::func_ptr1 an array of FunctionPtr1? I don't think you intended to store multiple function pointers in one NODE instance.