Thread: problem with hash table

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18

    problem with hash table

    i got this problem in creating a hash table for string. Basically, my program will ask the user to input the string, than the program will create the index by calculating the ascii number of the string and put the string into an array of pointers. and i also use struct to contain the string. this is my code ( its a english-indonesia dictionary) :

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    struct EnglishIndonesia{
    	
    	string english;
    	string indonesia;
    	EnglishIndonesia *link;
    };
    EnglishIndonesia *p, *newNode;
    EnglishIndonesia *q[99];
    
    
    void mainMenu();
    void addDef();
    void searchDef();
    void deleteDef();
    int quit();
    int hashing (string s);
    void show (EnglishIndonesia x[]);
    void createNode(string a, string b);
    
    int main (int argc, char * const argv[]) {
    		
    		mainMenu();
    }
    
    void mainMenu()
    {
    	int choice;
    	
    	cout<<"Welcome to Dictionary of English-Indonesia 2012(KIAMAT !)"<<endl;
    	cout<<"What do You want to do ?"<<endl;
    	cout<<"1. add Definition to Dictionary"<<endl;
    	cout<<"2. Search for Definition"<<endl;
    	cout<<"3. delete Definition"<<endl;
    	cout<<"4. Quit"<<endl;
    	cout<<"Enter the number you want :  ";
    	cin>>choice;
    	
    	if( choice == 1)
    	{
    		addDef();
    	}
    	else if (choice == 2)
    	{
    		searchDef();
    	}
    	else if (choice == 3)
    	{
    		deleteDef();
    	}
    	else if ( choice == 4 )
    	{
    		quit ();
    	}
    	else
    	{
    		cout<<"Input Invalid ! Enter between 1-4 !"<<endl<<endl;
    		mainMenu();
    	}
    }
    
    void addDef()
    {
    	int no;
    	int hashVal;
    	string a;
    	string b;
    	
    	
    	while ( no != 2)
    	{
    		cout << "Word in English : ";
    		cin>> a;
    		cout<<"Transelation in Indonesia : ";
    		cin>> b;
    		createNode(a,b);
    		hashVal = hashing (newNode->english);
    		q[hashVal] = newNode;
    		cout<<"Do You Want To Continue ? ( 1.Yes 2.No)  ";
    		cin>>no;
    	}
    
    }
    
    void searchDef()
    {
    }
    
    void deleteDef()
    {
    }
    
    int hashing ( string s )
    {
    	int total = 0;
    	int k = 0;
    	int i = 0;
    	char z[99];
    	
    	while (k < 99)
    	{
    		z[k] = s.at(k);
    		k++;
    	}
    	while(i < 99)
    	{
    		total = total + (int)z[i];
    		i++;
    	}
    	return total % 99;
    }
    
    void show (EnglishIndonesia x[])
    {
    	for ( int i = 0; i <= 99; i++)
    	{
    		if(q[i] != NULL)
    		{
    			cout<<q[i]->english<<endl;
    			cout<<q[i]->indonesia<<endl;
    		}
    	}
    		
    	
    }
    
    int quit()
    {
    	return 0;
    }
    
    void createNode ( string a, string b)
    {
         newNode = (EnglishIndonesia *)malloc(sizeof(EnglishIndonesia));
         if (newNode != NULL)
         {
            newNode->english = a;
            newNode->indonesia = b;
            }
            else
                cout <<"Memory FULL !";
    }
    i have no idea where i'm doing wrong. i mean the program just crash after the second input.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18

    please..

    somebody.. anybody.. please... i'm way over my limit here...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I would use a switch instead of all your else ifs.
    you could try this
    Code:
    int hashmap::hashStr(char const * const str)
    {
    
    
    	size_t length = strlen( str );
    
    	int hash = 0;
    
    	for (unsigned i = 0; i < length ; i++ )
    	{
    
    
    		hash = 31 * hash + str[i];        
    	}                                  
    	return hash ;

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't use malloc in C++. In C++ you should always use 'new' and in this instance you absolutely must use 'new'.

    You allocate memory in createNode, but how is the rest of the program supposed to know about that bit of memory? At the moment it is just leaked.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  2. trying to make a hash table......trouble w/ memory?
    By cuizy in forum C Programming
    Replies: 3
    Last Post: 05-11-2009, 04:47 PM
  3. Hash Table outputting incorrect number
    By Paul Skinner in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 06:19 AM
  4. Problem with Hash Table Linear Probing
    By kirby1024 in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 06:03 AM
  5. Hash Table
    By ihsir in forum C++ Programming
    Replies: 0
    Last Post: 04-13-2002, 12:08 AM