[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.8

Trying to build a hashtable using linked lists

Asked by Sandra-24 in C++ Programming Language, Microsoft Visual C++

Tags: c2039, hashtable

I'm building a hash table that is actually three hash tables, each one for storage of a specific type, shorts, longs, and c-strings. Such a hash table would make for fats and convenient storage and retrieval of said data types by a string key.  It would also be possible to add a method for saving the data to a file to the hashtables destructor, and a method for reloading this information to the constructor. I plan to do this later, then I would have a very simple method of saving information that would keep it's state between application sessions (usefull in most any app, and won't require any external libraries).

Still with me? I have created the basics of this hash table but I'm obviously misunderstanding some critical C++ concept because it spews compiler errors. I suspect the problem lies with the structs. My code is below:

-----------------------------------------------------------------------------------
DATA.H:

#define NULL 0; //temp null define, should probably add an #ifndef statement or something to it

class data
{
public:
      data(int hashTableLength);
      ~data();
      // Adds a long integer value to the hash table under the 32bit hash key given
      void Add(const long value, const char * key);
      
      // Gets a long integer value from the hash table at the given 32 bit hash key
      long GetLong(const char * key);

private:

      LONG ** longs;
      int hashTableLength;
};

typedef struct {
      long hash = 0;
    LONG * next;
      long data;
} LONG;

-----------------------------------------------------------------------------------
DATA.CPP:

#include "data.h"

data::data(int hashTableLength)
{
      longs = new LONG * [hashTableLength];
      //init all the pointers at NULL (to prevent stray pointer errors)
      for(int i=0; i < hashTableLength; i++)
      {
            longs[i] = NULL;
      }
      //store the length for future usage.
      this->hashTableLength = hashTableLength;
}

data::~data()
{
      //store the data to disk and free all the memory used      

}

// Adds a long integer value to the hash table under the 32bit hash key given
void data::Add(const long value, const char * key)
{
      long hash = strHash(key,strlen(key));
      int index = (int)(hash % hashTableLength); //0-49 for a length of 50

      LONG * entry = longs[index];

      while((entry->next != NULL) && (entry->hash != hash))
      {
            entry = entry->next;
      }

      //so now, this entry->next pointer is either NULL (making it the lest entry)
      //or we've stumbled across an existing entry by this key.
      if(entry->hash == hash)
      {
            entry->data = value;
      }
      //else we need to add a new entry to the end
      else
      {
            entry->next = new LONG; //add a new struct
            entry = entry->next; //set entry to point to the new struct
            entry->next = NULL; //set the next pointer to NULL (we're at the end after all)
            //set the data for this struct
            entry->hash = hash;
            entry->data = value;
      }

}


long data::GetLong(const char * key)
{
      long hash = strHash(key,strlen(key));
      int index = (int)(hash % hashTableLength); //0-49 for a length of 50

      LONG * entry = longs[index];

      while((entry != NULL) && (entry->hash != hash))
      {
            entry = entry->next; //set entry to point to the next LONG struct
      }

      if(entry == NULL)
            return 0; //not found
      else
            return entry->data;
}

-----------------------------------------------------------------------------------
COMPILER ERRORS:

data.h(19): error C2501: 'data::longs' : missing storage-class or type specifiers
data.cpp(33): error C2039: 'next' : is not a member of 'LONG'
data.cpp(6): error C2065: 'longs' : undeclared identifier
data.cpp(12): error C2109: subscript requires array or pointer type
data.cpp(31): error C2109: subscript requires array or pointer type
data.cpp(33): error C2143: syntax error : missing ')' before ';'
data.cpp(33): error C2143: syntax error : missing ')' before ';'
data.h(19): error C2143: syntax error : missing ';' before '*'
data.h(32): error C2143: syntax error : missing ';' before '*'
data.h(32): error C2501: '$UnnamedClass$0x5a7588c4$1$::LONG' : missing storage-class or type specifiers
data.h(32): error C2501: '$UnnamedClass$0x5a7588c4$1$::next' : missing storage-class or type specifiers
data.h(19): error C2501: 'data::LONG' : missing storage-class or type specifiers
 
Related Solutions
Keywords: Trying to build a hashtable using linked …
 
Loading Advertisement...
 
[+][-]08/07/03 01:42 PM, ID: 9103193Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/07/03 01:42 PM, ID: 9103196Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]08/07/03 01:44 PM, ID: 9103206Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/07/03 01:46 PM, ID: 9103218Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/07/03 04:35 PM, ID: 9104283Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/07/03 09:48 PM, ID: 9105675Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: C++ Programming Language, Microsoft Visual C++
Tags: c2039, hashtable
Sign Up Now!
Solution Provided By: shajithchandran
Participating Experts: 5
Solution Grade: B
 
[+][-]08/07/03 11:25 PM, ID: 9106036Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/07/03 11:42 PM, ID: 9106109Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/08/03 11:55 AM, ID: 9110377Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/09/03 01:09 AM, ID: 9113136Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89