hi there i was wondering if any1 could help me with this problem. i am a beginner at c++ and my coursework is asking me to create a program with a hash function and i am lost! could any1 tell me how to implement this? here is my problem -

Write your own hash function* that uses the 6 digit grid reference as a key. Using this function and the hill details already generated, add the hill details to another array, this time of 63 elements. This will be your hash table.Test that a user can search the hash table by asking the user to enter a grid reference. The program should either output details of the hill at that grid reference or output that there is no hill at that grid reference.

here is my code-

``````#include <iostream>
using namespace std;

struct Node{		//a structure that can be
int data;		//thought of as a simple class
Node *link;		//All fields are public
int height;
int gridReference;
int distance;
bool climbed;
};

public:
Node *head;		//pointer to the first node
Node *last;		//pointer to the last node
int count;		//number of nodes

//first = NULL
//last = NULL
//count = 0

~LinkedList();	//destructor - deletes all nodes

void insertFirstNode(int newinfo); // inserts a node
//at the start of the list
void insertLastNode(int newinfo);  // inserts a node
//at the end of the list
void insertHills(int height, int distance, bool climbed, int gridReference);

int getNodeCount();  //returns the number of nodes
Node* getFirst();	 //returns the pointer to the
//the first node - the value of "head"
void displayData();
};

last = NULL;
count = 0;
}

while(temp) {
delete temp;
}
}

Node *newNode;
newNode = new Node;
newNode->data = newInfo;

count++;

if(last == NULL){
last = newNode;
}
}

Node *newNode;
newNode = new Node;
newNode->data = newInfo;

count++;

last = newNode;
}
else{
last = newNode;
}
}

return count;
}

}

void LinkedList::insertHills(int height, int distance, bool climbed, int gridReference){

Node* previous= NULL;
Node* hills= new Node();
count++;

hills->height= height;
hills->distance= distance;
hills->climbed= climbed;
hills->gridReference= gridReference;

if(count==1)
{
last= hills;
return;
}
do{
if(current->height<hills->height)
{

{
return;
}
else
{
return;
}
if(current==last)
{
last= hills;
return;
}
}
previous=current;

}
while(current!=NULL);
}

int noOfNodes= getNodeCount();
Node *current;

cout << "Number of Nodes = " << noOfNodes << endl;
for(int i=0; i<noOfNodes && current != NULL; i++){
cout<<"Hill Number: "<<count--<<endl;

cout<<"Height: "<<current->height<<endl;

cout<<"Distance from Home: "<<current->distance<<endl;

cout<<"Climbed: "<<current->climbed<<endl;

cout<<"Grid Reference: "<<current->gridReference<<endl;

cout<<"**************"<<endl;

}
}

#include <cstdlib>
#include <iostream>

int main(){

for(int i=1; i<41; i++){

int randHeight= (rand()%1400)+3000;// generates random number for height of hill

int randDistance= (rand()%280)+20;// generates random number for distance of hill

int randGridReference= (rand()%100000)+400000;// generates random number for gridreference of hill

int randClimbedNumber= (rand()%10)+1;// generates random number to tell user if hill is climbed

bool randClimbed;
if(randClimbedNumber>5)//This takes the random number generated from randClimbedNumber and determines if it has been climbed or not
{
randClimbed= true;
}
else
{
randClimbed= false;
}
l.insertHills(randHeight, randDistance, randClimbed, randGridReference);
}

l.displayData();

cout<<endl;

l.displayData();

return 0;
}``````

THANKS!!

2
Contributors
3
Replies
5
Views
10 Years
Discussion Span
Last Post by Narue

>could any1 tell me how to implement this?
Use this hash function until you get the rest of your hash table working:

``````unsigned hash ( int )
{
return 0;
}``````

Hi there I am sorry i don't understand. i have tried looking up simple ways of implementing hash tables but i can't find any. i habe no idea where to start with them

Well, that's different then. Your question strongly suggested that you were having trouble writing a hash function, not a hash table. I'll suggest this tutorial, and this one as well.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.