Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

hash function help!!!!

Last post 04-24-2008 8:36 AM by Mohammad Rastkar. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 04-22-2008 6:34 PM

    • junt86
    • Not Ranked
    • Joined on 04-22-2008
    • United Kingdom
    • New Member
    • Points 10

    hash function help!!!!

    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;
       
    }*/

    • Post Points: 10
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 04-24-2008 8:36 AM In reply to

    Time [O] Using STL

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

    - NetTimeSaver : My software that logs your connecting informations (like duration).
    • Post Points: 5
Page 1 of 1 (2 items)