Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe you've chosen the incorrect data structure for your hash tables:</p> <pre><code>typedef struct hashTable { char key[MAX_NAME]; int index; struct hashTable *next; struct hashTable *prev; }; </code></pre> <p>One of the primary benefits of a hash table is being able to jump directly to the bucket that contains the element you're searching for. This is part of a <em>linked list</em> of hash buckets -- which means you must iterate through an average of 4098/2 buckets on <em>every lookup or insertion</em>. That will not provide you with the performance you need.</p> <p>Your hash table should instead be an array of <code>structs</code>; each <code>struct</code> should have a pointer to a string (or direct storage for <em>short strings</em>) and a pointer to the next <code>struct</code> in the bucket. (While this <code>struct hashTable</code> could <em>also</em> be the in-bucket structure, it is a rare hash table that needs <code>next</code> and <code>prev</code> links within the buckets. Which is why I guessed this data structure is instead intended for the table itself.)</p> <p>You also need to select a good <a href="http://en.wikipedia.org/wiki/Hash_function" rel="nofollow">hash function</a>. There is a ton of research into good hash functions, but you're really looking for something better than horrible for a homework assignment. The input to the hash function is your strings, and the output should be an integer. You'll need to <code>%</code> the output with the size of your array (pick a prime near 5000) to figure out which bucket to use.</p> <p>Here's a hash function from the <a href="http://nothings.org/stb/stb_h.html" rel="nofollow"><code>stb.h</code> library of convenient functions</a>:</p> <pre><code>unsigned int stb_hash(char *str) { unsigned int hash = 0; while (*str) hash = (hash &lt;&lt; 7) + (hash &gt;&gt; 25) + *str++; return hash + (hash &gt;&gt; 16); } </code></pre> <p>A short hint that while the <code>stb.h</code> code is in the public domain, it would be very wise to reference the source in the program -- professors, lawyers, and in the future, your co-workers, will thank you for including the source of things you didn't do yourself.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload