Note that there are some explanatory texts on larger screens.

plurals
  1. POHashtable searching, and command prompt weird error
    primarykey
    data
    text
    <p>I'm trying to search through a hash table, and I'm having some problems. My code compiles, but there's something wrong with it. I'm not quite sure what. When I ran it at the command prompt, and passed the file to the program. I'm having some problems with the searching</p> <p>0 insincere</p> <pre><code>struct htablerec { char **key; int *frequencies; int num_keys; int capacity; }; static unsigned int htable_word_to_int(char *word) { unsigned int result = 0; while (*word != '\0') { result = (*word++ + 31 * result); } return result; } int htable_insert(htable h, char *str) { int i; /*convert string to integer*/ unsigned int index = htable_word_to_int(str); /*calculate index to insert into hash table*/ int remainder = index%h-&gt;capacity; /*once calculated position in the hash table, 3 possibilities occur*/ /*no string in this positon, copy string to that position, increment number of keys, return 1*/ if (h-&gt;key[remainder] == NULL) { char *key = emalloc(strlen(str) + 1); strcpy(key, str); h-&gt;key[remainder] = key; h-&gt;frequencies[remainder] = 1; h-&gt;num_keys++; return 1; } /*the exact same string is at the position, increment frequency at that position, return frequency*/ if (strcmp(str, h-&gt;key[remainder]) == 0) { h-&gt;frequencies[remainder]++; return h-&gt;frequencies[remainder]; }/*a string is at that position, but it isnt the rightone, keep moving along the array until you find either an open space or the string you are looking for*/ if (h-&gt;key[remainder] != NULL &amp;&amp; strcmp(str, h-&gt;key[remainder]) != 0) { /*you may need to wrap back around to the beginning of the table, so each time you add,to the position you should also mod by the table capacity.*/ for (i = 0; i &lt; h-&gt;capacity; i++) { /*no string in this positon, copy string to that position, increment number of keys*/ if (h-&gt;key[remainder] == NULL) { char *key = emalloc(strlen(str) + 1); strcpy(key, str); h-&gt;key[remainder] = key; h-&gt;frequencies[remainder] = 1; h-&gt;num_keys++; } /*if you find the string you were looking for, increment the frequency at the position and return the frequency*/ if (strcmp(str, h-&gt;key[remainder]) == 0) { h-&gt;frequencies[remainder]++; return h-&gt;frequencies[remainder]; } if (h-&gt;key[remainder] != NULL &amp;&amp; h-&gt;capacity == i) { i = 0; } } } /*if you have kept looking for an open space but there isnt one, the hash table must fu*/ return 0; } int htable_search(htable h, char *str) { /*create an initialise, a value to hold the number of collisions we have when looking for our key*/ int collisions = 0; unsigned int index = htable_word_to_int(str); /*calculate the position at which we should start looking for our key*/ int remainder = index%h-&gt;capacity; /*while there is an item at that position, but it isn't the key, and we haven't yet checked the entire hash table*/ while (h-&gt;key[remainder] != NULL &amp;&amp; strcmp(str, h-&gt;key[remainder]) != 0 &amp;&amp; h-&gt;key[remainder] != h-&gt;key[h-&gt;capacity]) { /*increment our position to look for the next cell*/ h-&gt;key[remainder]++; /*increment the number of collisions we've had so far*/ collisions++; } /*if our number of collisions == the hashtable capacity*/ if(collisions == h-&gt;capacity) { /*then out hashtable was full, did not contain our key, so we should return 0*/ return 0; } else { /*else return the frequency at our final position*/ return h-&gt;frequencies[h-&gt;capacity]; } } #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include "mylib.h" #include "htable.h" int main(void) { htable h = htable_new(113); char word[256]; char op; /*We must have a space before the %c*/ while(2 == scanf(" %c %255s", &amp;op, word)) { if ('+' == op) { htable_insert(h, word); } else if ('?' == op) { printf("%d %s\n", htable_search(h, word), word); } } htable_free(h); return EXIT_SUCCESS; } </code></pre> <p>I have included the mylib.c above.</p> <pre><code>+ sociable + galleries + Russia + screamer + thickness + combed + escorted + revocable + digressed + Malawi + infringing + onslaught + files + kidded + unsound + tied + Ottawa + puzzles + build + necrosis + admire + cupful + brokers + segregation + Capet + Georges + bran + binders + zebras + contented + keypad + snowily + replaced + dominating + outright + Latinizers + invalidity + wakes + diversification + Riemannian + leadings + rhythmically + gentler + swarthy + disconnects + factoring + sequences + tiring + attendances + unloaded ? insincere ? constants ? unordered ? Toland ? butterfly ? suburban ? overtone ? Hauser ? numbers ? disadvantageous ? saintly ? Dutton ? homomorphic ? corporation ? climaxes ? dietitian ? manifestly ? greyest ? challenge ? tentacle ? Bernice ? bottle ? involve ? resisted ? wholesale ? trustworthiness ? Poole ? fourfold ? relentlessly ? fittingly ? doctorates ? cowlick ? Missoula ? curtsy ? calmness ? reallocate ? bossed ? scarce ? catheters ? regained ? autographing ? unobservable ? apprise ? lancer ? chicken ? crunches ? scrambled ? reared ? pealing ? violate ? sociable ? galleries ? Russia ? screamer ? thickness ? combed ? escorted ? revocable ? digressed ? Malawi ? infringing ? onslaught ? files ? kidded ? unsound ? tied ? Ottawa ? puzzles ? build ? necrosis ? admire ? cupful ? brokers ? segregation ? Capet ? Georges ? bran ? binders ? zebras ? contented ? keypad ? snowily ? replaced ? dominating ? outright ? Latinizers ? invalidity ? wakes ? diversification ? Riemannian ? leadings ? rhythmically ? gentler ? swarthy ? disconnects ? factoring ? sequences ? tiring ? attendances ? unloaded </code></pre> <p>This is the contents of the file i'm passing in. My insert method works, but if that is needed, i can include that too.</p>
    singulars
    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.
 

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