Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting Static to Dynamic Hash Table
    primarykey
    data
    text
    <p>I'be been given the assignment to change a given hash-table with static memory allocation to dynamic, so that more memory can be allocated past the limit while the program is running. I by no means am demanding a solution to the problem, I'm simply asking if anyone knows a good place to start or which aspects of the code I need to pay attention to as I'm a little lost and confused with hash tables. I know the enums and the constructor needs to be changed but I'm not sure of much else. Here is the code given, and thanks in advance for any advice:</p> <pre><code>#ifndef TABLE1_H #define TABLE1_H #include &lt;cstdlib&gt; // Provides size_t #include &lt;cassert&gt; // Provides assert namespace main_savitch_12A { template &lt;class RecordType&gt; class table { public: enum { CAPACITY = 30 }; // CONSTRUCTOR table( ); // MODIFICATION MEMBER FUNCTIONS void insert(const RecordType&amp; entry); void remove(int key); // CONSTANT MEMBER FUNCTIONS bool is_present(int key) const; void find(int key, bool&amp; found, RecordType&amp; result) const; size_t size( ) const { return used; } private: // MEMBER CONSTANTS -- These are used in the key field of special records. enum { NEVER_USED = -1 }; enum { PREVIOUSLY_USED = -2 }; // MEMBER VARIABLES RecordType data[CAPACITY]; size_t used; // HELPER FUNCTIONS size_t hash(int key) const; size_t next_index(size_t index) const; void find_index(int key, bool&amp; found, size_t&amp; index) const; bool never_used(size_t index) const; bool is_vacant(size_t index) const; }; template &lt;class RecordType&gt; table&lt;RecordType&gt;::table( ) { size_t i; used = 0; for (i = 0; i &lt; CAPACITY; ++i) data[i].key = NEVER_USED; // Indicates a spot that's never been used. } template &lt;class RecordType&gt; void table&lt;RecordType&gt;::insert(const RecordType&amp; entry) // Library facilities used: cassert { bool already_present; // True if entry.key is already in the table size_t index; // data[index] is location for the new entry assert(entry.key &gt;= 0); // Set index so that data[index] is the spot to place the new entry. find_index(entry.key, already_present, index); // If the key wasn't already there, then find the location for the new entry. if (!already_present) { assert(size( ) &lt; CAPACITY); index = hash(entry.key); while (!is_vacant(index)) index = next_index(index); ++used; } data[index] = entry; size_t i; for (i=0; i&lt;CAPACITY; i++) cout &lt;&lt; data[i].key &lt;&lt; ' '; cout &lt;&lt; endl; } template &lt;class RecordType&gt; void table&lt;RecordType&gt;::remove(int key) // Library facilities used: cassert { bool found; // True if key occurs somewhere in the table size_t index; // Spot where data[index].key == key assert(key &gt;= 0); find_index(key, found, index); if (found) { // The key was found, so remove this record and reduce used by 1. data[index].key = PREVIOUSLY_USED; // Indicates a spot that's no longer in use. --used; } } template &lt;class RecordType&gt; bool table&lt;RecordType&gt;::is_present(int key) const // Library facilities used: assert.h { bool found; size_t index; assert(key &gt;= 0); find_index(key, found, index); return found; } template &lt;class RecordType&gt; void table&lt;RecordType&gt;::find(int key, bool&amp; found, RecordType&amp; result) const // Library facilities used: cassert.h { size_t index; assert(key &gt;= 0); find_index(key, found, index); if (found) result = data[index]; } template &lt;class RecordType&gt; inline size_t table&lt;RecordType&gt;::hash(int key) const { return (key % CAPACITY); } template &lt;class RecordType&gt; inline size_t table&lt;RecordType&gt;::next_index(size_t index) const // Library facilities used: cstdlib { return ((index+1) % CAPACITY); } template &lt;class RecordType&gt; void table&lt;RecordType&gt;::find_index(int key, bool&amp; found, size_t&amp; i) const // Library facilities used: cstdlib { size_t count; // Number of entries that have been examined count = 0; i = hash(key); while((count &lt; CAPACITY) &amp;&amp; (data[i].key != NEVER_USED) &amp;&amp; (data[i].key != key)) { ++count; i = next_index(i); } found = (data[i].key == key); } template &lt;class RecordType&gt; inline bool table&lt;RecordType&gt;::never_used(size_t index) const { return (data[index].key == NEVER_USED); } template &lt;class RecordType&gt; inline bool table&lt;RecordType&gt;::is_vacant(size_t index) const { return (data[index].key == NEVER_USED);// || (data[index].key == PREVIOUSLY_USED); } } #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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