hash function help!!!!

  • 9 years ago

    Can anyone help me to write up a 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 and this will be my hash table.
    please im stuck!!!

    this is my piece of code for the linklist.cpp

     

     

    #include "LinkedList.h"
    using namespace std;

     

    LinkedList::LinkedList(){ //constructor
        head = NULL;
        last = NULL;
        count = 0;
    }

    void LinkedList:: insertNodeAtStart(int height, int distanceFromHome, bool climbed, int gridReference){
     Node *nextNode;   
     nextNode = new Node;  
     nextNode->height = height; 
     nextNode->distanceFromHome= distanceFromHome;
     nextNode->climbed= climbed;
     nextNode->gridReference= gridReference;

     nextNode->link = head; 
         
     head = nextNode; 

     count++;   
     
    if(last == NULL){ 
      last = nextNode; 
     }    
    }

    void LinkedList:: insertNodeAtEnd(int height, int distanceFromHome, bool climbed, int gridReference ){
         Node *lastNode;
         lastNode= new Node;
        
         lastNode->link= NULL;
         lastNode->height = height; 
         lastNode->distanceFromHome= distanceFromHome;
      lastNode->climbed= climbed;
      lastNode->gridReference= gridReference;
        
         count++;
        
         if(head==NULL){
                        head= lastNode;                             //if this is the only node, make it first and last.
                        last= lastNode;
                        }
                        else{
                             last->link= lastNode;                 //make the link of the last node point to our new node
                             last= lastNode;                       //make last or newly created node.
                        }
         }
        
     

    int LinkedList::getNodeCount(){
        return count;
        }
       
    Node* LinkedList::getFirst(){
          return head;
          }
         

    void LinkedList::deleteFirstNode(){
    if(count > 0){
      Node *temp = head;
           
      head = head->link;  
          
      delete temp; 
      count--;
    }
     else{
      cout<<"There are no nodes to delete."<<endl;
     }
    }
     
    /*bool LinkedList::findNumber(int number){
        
         int noOfNodes=getNodeCount();
         Node *current;
         current= getFirst();
        
         for(int i=0; i<=noOfNodes-1; i++){
                 if(number==current->data){
                                          cout<<"found"<<endl;
                                          return true;
                                          }
                 else{
                      current=current->link;
                      }
        
                 }
         cout<<"Number is not in the Linked List"<<endl;
         return false;
         }*/
        
    /*void LinkedList::deleteNode(int number){
        
         Node *current, *next;                                      //two pointer to help maipulate position
         current= head;                                             //set current to the head of the list
         if(current->data== number)                                 //if the number is first in the list
         {
                      head= current->link;                          //make head point to the one after
                      delete current;                               //delete the node which used to be head
                      count--;                                     //reduce count by one
                      return; 
         }
        
         next=current;                                               //make next = the new head node (current)
         while(next!=NULL)                                           //iterate through until no more nodes
         {
                    if(current->data==number)                        //if the data in current is the number
                    {
                                      next->link= current->link;     //make the link next points to = to what current is pointing to
                                      delete current;                 //delete the current node
                                      count--;
                                      return;
                    }
                   
                    next=current;                                    //keep iterating until the number is found
                    current=current->link;
         }
         cout<<"Element not Found!"<<endl;                            //if not in the list display suitable.
    }*/
                   

     

    void LinkedList:: deleteAllNodes(){
         int noOfNodes= getNodeCount();
         Node *current;
         current= head;
        
         for(int i=0; i<noOfNodes;i++){
                              current->link=current;
                              delete current;
                              count--;
                              }
        }

    void LinkedList::printNode(){
         int noOfNodes= getNodeCount();
         Node *current;
         current= head;
        
         for(int i=0; i<noOfNodes; i++){
                 cout<<"Height: "<<current->height<<endl;
                 cout<<"Distance from Home: "<<current->distanceFromHome<<endl;
                 cout<<"Climbed: "<<current->climbed<<endl;
                 cout<<"Grid Ref: "<<current->gridReference<<endl;
                 cout<<"-------------------"<<endl;
                 current= current->link;
                 }
         }
        
    void LinkedList::insertNodet(int height, int distanceFromHome, bool climbed, int gridReference){
        
         Node* current= head;
         Node* previous= NULL;
         Node* temp= new Node();
         count++;
        
         temp->height= height;
         temp->distanceFromHome= distanceFromHome;
         temp->climbed= climbed;
         temp->gridReference= gridReference;
         temp->link= NULL;

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

                                 if(current== head)
                                  {
                                     head= temp;
                                     return;
                                  }
                                 else
                                 {
                                   previous->link= temp;
                                   return;
                                 }
                                 if(current==last)
                                 {
                                    current->link=temp;
                                    last= temp;
                                    return;
                                 }
                    }
                    previous=current;
                    current= current->link;
                   
                    }         
         while(current!=NULL);
    }


    void LinkedList::swap(Node *current, Node *next){

         Node *swap;
         Node *swapLink;
        
         swapLink= current->link;
         current->link= next->link;
         next->link= swapLink;
        
         swap= current;
         current=next;
         next=swap;
    };

     

    void LinkedList::sort();
    {

    {
         int noOfNodes= getNodeCount();
         Node *current;
         Node *nextNode;
         current= head;
         nextNode= current->link;
        
         for(int i=0;i<noOfNodes;i++){
                 if(current=NULL){
                                  cout<<"No hills in list"<<endl;
                                  return;
                                  }
                 if(current->height>nextNode->height){
                                                      swap(current, nextNode);
                 }
         }

     

     


    /*void LinkedList::printNode()
    {
         int noOfNodes= getNodeCount();
         Node *current;
         current= head;
         cout << "Number of Nodes == " << noOfNodes << endl;
         for(int i=0; i<noOfNodes && current != NULL; i++)
         {
                 cout<<"Height: "<<current->height<<endl;
                 cout<<"Distance from Home: "<<current->distanceFromHome<<endl;
                 cout<<"Climbed: "<<current->climbed<<endl;
                 cout<<"Grid Ref: "<<current->gridReference<<endl;
                 cout<<"-------------------"<<endl;
                 current= current->link;
       
    }*/

  • 9 years ago

    You can also use STL;  <hash_map> or <hash_set> or even <map>. See MSDN for infos.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Brevity is the soul of wit” - Shakespeare