Question: Could really use some help with this assignment Using only C++ Please read carefully before starting. All files that are being used are provided below.

Could really use some help with this assignment

Using only C++

Please read carefully before starting.

All files that are being used are provided below.

Table1 header and template file are only examples, they are not being used for this problem. The table2.h file and testtab2.cpp are the files being used.

Could really use some help with this assignment Using only C++ Please

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// FILE: table2.h // TEMPLATE CLASS PROVIDED: Table // This class is a container template class for a Table of records. // The template parameter, RecordType, is the data type of the records in the // Table. It may any type with a default constructor, a copy constructor, // an assignment operator, and an integer member variable called key. // // CONSTRUCTOR for the Table template class: // Table( ) // Postcondition: The Table has been initialized as an empty Table. // // MODIFICATION MEMBER FUNCTIONS for the Table class: // void insert(const RecordType& entry) // Precondition: entry.key >= 0. Also if entry.key is not already a key in // the table, then the Table has space for another record // (i.e., size( ) class: // bool is_present(const Item& target) const // Postcondition: The return value is true if there is a record in the // Table with the specified key. Otherwise, the return value is false. // // void find(int key, bool& found, RecordType& result) const // Postcondition: If a record is in the Table with the specified key, then // found is true and result is set to a copy of the record with that key. // Otherwise found is false and the result contains garbage. // // size_t size( ) const // Postcondition: Return value is the total number of records in the // Table. // // VALUE SEMANTICS for the Table template class: // Assignments and the copy constructor may be used with Table objects. // // DYNAMIC MEMORY USAGE by the Table template class: // If there is insufficient dynamic memory, then the following functions // can call new_handler: the copy constructor, insert, the assignment // operator.

#ifndef TABLE2_H #define TABLE2_H #include // Provides size_t #include // Provides the STL list class

namespace main_savitch_12B { template class table { public: // MEMBER CONSTANT -- See Appendix E if this fails to compile. static const std::size_t TABLE_SIZE = 811; // CONSTRUCTORS AND DESTRUCTOR table( ); table(const table& source); ~table( ); // MODIFICATION MEMBER FUNCTIONS void insert(const RecordType& entry); void remove(int key); void operator =(const table& source); // CONSTANT MEMBER FUNCTIONS void find(int key, bool& found, RecordType& result) const; bool is_present(int key) const; std::size_t size( ) const { return total_records; } private: std::list* data[TABLE_SIZE]; std::size_t total_records; // HELPER MEMBER FUNCTION std::size_t hash(int key) const; }; }

#include "table2.template" // Include the implementation

#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// FILE: testtab2.cxx // An interactive test program for the second table ADT.

#include // Provides toupper #include // Provides EXIT_SUCCESS and size_t #include // Provides cin, cout #include "table2.h" // Provides the table class using namespace std; using namespace main_savitch_12B;

// Struct definition for the test_record_type, which has a key and a double. struct test_record_type { int key; double data; };

// PROTOTYPES for functions used by this test program: void print_menu( ); // Postcondition: A menu of choices for this program has been written to cout.

char get_user_command( ); // Postcondition: The user has been prompted to enter a one character command. // The next character has been read (skipping blanks and newline characters), // and this character has been returned.

test_record_type get_record( ); // Postcondition: The user has been prompted to enter data for a record. The // key has been read, echoed to the screen, and returned by the function.

int get_key( ); // Postcondition: The user has been prompted to enter a key for a record. The // items have been read, echoed to the screen, and returned by the function.

int main( ) { table test; // A table that we'll perform tests on char choice; // A command character entered by the user bool found; // Value returned by find function test_record_type result; // Value returned by find function cout

do { print_menu( ); choice = toupper(get_user_command( )); switch (choice) { case 'S': cout

return EXIT_SUCCESS; }

void print_menu( ) // Library facilities used: iostream.h { cout

char get_user_command( ) // Library facilities used: iostream.h { char command;

cout > command; // Input of characters skips blanks and newline character

return command; }

test_record_type get_record( ) // Library facilities used: iostream.h { test_record_type result; cout > result.data; cout

int get_key( ) // Library facilities used: iostream.h { int key; do { cout > key; } while (key

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// FILE: table1.h (part of the namespace main_savitch_12A) // TEMPLATE CLASS PROVIDED: table (a table of records with keys). // This class is a container template class for a table of records. // The template parameter, RecordType, is the data type of the records in the // table. It may be any of the bulit-in C++ types (int, char, etc.), or a // class with a default constructor, an assignment operator, and an integer // member variable called key. // // MEMBER CONSTANT for the table class: // static const size_type CAPACITY = ________ // table::CAPACITY is the maximum number of records held by a table. // // CONSTRUCTOR for the table template class: // table( ) // Postcondition: The table has been initialized as an empty table. // // MODIFICATION MEMBER FUNCTIONS for the table class: // void insert(const RecordType& entry) // Precondition: entry.key >= 0. Also if entry.key is not already a key in // the table, then the table has space for another record // (i.e., size( ) class: // bool is_present(const Item& target) const // Postcondition: The return value is true if there is a record in the // table with the specified key. Otherwise, the return value is false. // // void find(int key, bool& found, RecordType& result) const // Postcondition: If a record is in the table with the specified key, then // found is true and result is set to a copy of the record with that key. // Otherwise found is false and the result contains garbage. // // size_type size( ) const // Postcondition: Return value is the total number of records in the // table. // // VALUE SEMANTICS for the table template class: // Assignments and the copy constructor may be used with table objects.

#ifndef TABLE1_H #define TABLE1_H #include // Provides size_t

namespace main_savitch_12A { template class table { public: // MEMBER CONSTANT -- See Appendix E if this fails to compile. static const std::size_t CAPACITY = 811; // CONSTRUCTOR table( ); // MODIFICATION MEMBER FUNCTIONS void insert(const RecordType& entry); void remove(int key); // CONSTANT MEMBER FUNCTIONS bool is_present(int key) const; void find(int key, bool& found, RecordType& result) const; std::size_t size( ) const { return used; } private: // MEMBER CONSTANTS -- These are used in the key field of special records. static const int NEVER_USED = -1; static const int PREVIOUSLY_USED = -2; // MEMBER VARIABLES RecordType data[CAPACITY]; std::size_t used; // HELPER FUNCTIONS std::size_t hash(int key) const; std::size_t next_index(std::size_t index) const; void find_index(int key, bool& found, std::size_t& index) const; bool never_used(std::size_t index) const; bool is_vacant(std::size_t index) const; }; } #include "table1.template" // Include the implementation. #endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// FILE: table1.template // TEMPLATE CLASS IMPLEMENTED: table (see table1.h for documentation) // INVARIANT for the table ADT: // 1. The number of records in the table is in the member variable used. // 2. The actual records of the table are stored in the array data, with // a maximum of CAPACITY entries. Each used spot in the array has a // non-negative key. Any unused record in the array has a key field of // NEVER_USED (if it has never been used) or PREVIOUSLY_USED (if it once // was used, but is now vacant).

#include // Provides assert #include // Provides size_t

namespace main_savitch_12A { template const std::size_t table::CAPACITY;

template const int table::NEVER_USED;

template const int table::PREVIOUSLY_USED;

template table::table( ) { std::size_t i;

used = 0; for (i = 0; i

template void table::insert(const RecordType& entry) // Library facilities used: cassert { bool already_present; // True if entry.key is already in the table std::size_t index; // data[index] is location for the new entry

assert(entry.key >= 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( )

data[index] = entry; }

template void table::remove(int key) // Library facilities used: cassert { bool found; // True if key occurs somewhere in the table std::size_t index; // Spot where data[index].key == key

assert(key >= 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 bool table::is_present(int key) const // Library facilities used: assert.h { bool found; std::size_t index;

assert(key >= 0);

find_index(key, found, index); return found; }

template void table::find(int key, bool& found, RecordType& result) const // Library facilities used: cassert.h { std::size_t index;

assert(key >= 0);

find_index(key, found, index); if (found) result = data[index]; }

template inline std::size_t table::hash(int key) const { return (key % CAPACITY); }

template inline std::size_t table::next_index(std::size_t index) const // Library facilities used: cstdlib { return ((index+1) % CAPACITY); }

template void table::find_index(int key, bool& found, std::size_t& i) const // Library facilities used: cstdlib { std::size_t count; // Number of entries that have been examined

count = 0; i = hash(key); while((count

template inline bool table::never_used(std::size_t index) const { return (data[index].key == NEVER_USED); } template inline bool table::is_vacant(std::size_t index) const { return (data[index].key == NEVER_USED) || (data[index].key == PREVIOUSLY_USED); } }

The goal of this assignment is to reinforce hash tables in C++. Specifically, the assignment is to implement the author's table2 using the STL list class instead of the author's list class. The table2.h and the test file testtab2.cpp will be provided for you. The goal of this assignment is to reinforce hash tables in C++. Specifically, the assignment is to implement the author's table2 using the STL list class instead of the author's list class. The table2.h and the test file testtab2.cpp will be provided for you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!