UPDATE: Errors still occuring 11:30 AM PST Assignment prompt below: A6
The main focus of this assignment is chained hash and template functions and class. . The material from Ch1 ~ 12 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 12.
You can add any number of paramours, functions and classes for this assignment and use any advance skills you learned. But there need to have at least two .h files, two template files and one .cpp file to generate the expecting result.
In sections 12.2 ~ 12.4 discuss the hashing concept and implementation. This assignment we will implement a chaining (data structure of a hash table).
Some information Files that you need to know or write:
- table2.h: A header file for this assignment’s Table class. This is a template class. The template parameter, called RecordType, may be instantiated as any struct or class that contains an integer member variable called key. The following are definition of a Table class, some useful functions and variables. a. 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. In the Table template class assignments and the copy constructor may be used with Table objects. If there is insufficient dynamic memory, then the copy constructor, insert, the assignment operator can add new memory.
b. CONSTRUCTOR for the Table template class: Table( ) Postcondition: The Table has been initialized as an empty Table.
c. MODIFICATION MEMBER FUNCTIONS for the Table class:
void insert(const RecordType& entry) Precondition: entry>= 0. Also if entry is not already a key in the table, then the Table has space for another record. (i.e., size( ) < CAPACITY). Postcondition: If the table already had a record with a key equal to entry, then that record is replaced by entry. Otherwise, the entry will be added as a new record of the Table.
void remove(int key) Postcondition: If a record was in the Table with the specified key, then that record has been removed; otherwise the table is unchanged.
void print(int index)const; Postcondition: If the Table with the index has data, then print the index and data in a chining format
d. CONSTANT MEMBER FUNCTIONS for the Table class:
bool is_present(const RecordType & 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. e. Private data members:
-
Node<RecordType> *data[TABLE_SIZE];
-
size_t total_records; // HELPER FUNCTION
-
size_t hash(int key) const;
-
Node<RecordType>* find_node(Node<RecordType> *&cursor,Node<RecordType> *&precursor, int key) const;
table2.template: is the implementation file for the table2.h.
link2.h is a header file to provide a toolkit of ten template functions for manipulating linked lists. Each node of the list contains a piece of data and a pointer to the next node (provided).
link2.template is the implementation file for the linke.h (provided).
CISP430V4A6.cpp is the driver for this assignment and there are several steps you need to do to test the program. a. Instantiate two Table objects and each with 10 fields. b. Display the two Table objects’ information. c. Use random number generator to generate 70 numbers each in between 0~200 for the Table objects. d. Display the two Table objects’ information. e. Removes all the data in first object. f. Display the two Table objects’ information. g. Using = to assign 2nd object to the first one. h. Display the two Table objects’ information.
Errors recieved:
Severity Code Description Project File Line Suppression State Error C2664 'void CISP430_A6::Table::insert(const CISP430_A6::Table::RecordType &)': cannot convert argument 1 from 'size_t' to 'const CISP430_A6::Table::RecordType &' CISP430_A6v4 C:\Users\kater\OneDrive\Desktop\CISP430_A6v4\CISP430_A6v4\table2driver.cpp 25
Driver file:
#include <iostream>
#include <cstdlib> // for rand and srand
#include <ctime> // for time
#include "table2.h"
#include "link2.h"
using namespace std;
using namespace CISP430_A6;
int main() {
srand(time(nullptr)); // seed the random number generator with the current time
Table<size_t> table1;
//Table<int> table2;
cout << "Initial state of table1:\n";
for (int i = 0; i < Table<int>::TABLE_SIZE; ++i) {
table1.print(i); // this works
}
/*cout << "Initial state of table2:\n";
for (size_t i = 0; i < Table<int>::TABLE_SIZE; ++i) {
table2.print(i);
}*/
//cout << "Generating random numbers...\n";
for (int i = 0; i < 70; ++i) {
size_t number = rand() % 201; // generate a random number between 0 and 200
table1.insert(number); // This will not work
//table2.insert(number);
}
cout << "State of table1 after inserting 70 random numbers:\n";
for (size_t i = 0; i < Table<int>::TABLE_SIZE; ++i) {
table1.print(i);
}
return 0;
}
Header File
#ifndef TABLE2_H
#define TABLE2_H
#include <cstdlib> // Provides size_t
#include <list>
#include "link2.h"
using namespace std;
namespace CISP430_A6
{
template <class RecordType> class Table
{
public:
//RecordType entry;
// MEMBER CONSTANT
static const size_t TABLE_SIZE = 10;
struct RecordType{ int key = 0; } entry;
// CONSTRUCTOR AND DESTRUCTOR
Table();
Table(const Table& source);
~Table();
// MODIFICATION MEMBER FUNCTIONS
void insert(const RecordType& entry);
void remove(int key);
void print(int index);
void operator =(const Table& source);
// CONSTANT MEMBER FUNCTIONS
void find(int key, bool& found, RecordType& result) const;
bool is_present(const RecordType& target) const;
size_t size() const { return total_records; };
private:
Node<RecordType>* data[TABLE_SIZE];
size_t total_records;
// HELPER MEMBER FUNCTION
size_t hash(int entry) const;
Node<RecordType>* find_node(Node<RecordType>*& cursor, Node<RecordType>*& precursor, int key) const;
};
}
#include "table2.template"
#endif
Insert function with other things:
template <class RecordType>
Table<RecordType>::Table()
{
//Class constructor: Constructs an empty table.
for (size_t i = 0; i < TABLE_SIZE; ++i)
{
data[i] = NULL;
}
total_records = 0;
}
template <class RecordType>
Table<RecordType>::Table(const Table& source)
{
// Copy constructor: Creates a copy of the con
for (int i = 0; i < TABLE_SIZE; ++i)
{
Node<RecordType>* tail_ptr;
list_copy(source.data[i], data[i], tail_ptr);
}
total_records = source.total_records;
}
template <class RecordType>
Table<RecordType>::~Table()
{
// Table destructor.
for (int i = 0; i < TABLE_SIZE; ++i)
{
if (data[i] != NULL)
{
list_clear(data[i]);
}
}
total_records = 0;
}
template<class RecordType>
void Table<RecordType>::operator=(const Table& source)
{
if (this != &source)
{
for (int i = 0; i < TABLE_SIZE; ++i)
{
Node<RecordType>* tail_ptr;
list_copy(source.data[i], data[i], tail_ptr);
}
total_records = source.total_records;
}
}
template<class RecordType>
void Table<RecordType>::print(int index)
{
Node<RecordType>* curNode = data[index];
cout << "[( " << index << " )]----> ";
if (data[index] != NULL)
{
while (curNode != NULL)
{
cout << "[" << data[index] << "]-->";
}
}
else if ((data[index] == NULL) || (curNode == NULL))
{
cout << " NULL ";
}
cout << endl;
}
template<class RecordType>
void Table<RecordType>::insert(const RecordType& entry)
{ // Precondition: entry >= 0 and there is space for another record
assert(entry.key >= 0 && total_records < TABLE_SIZE);
// Calculate the index in the array where the entry should be inserted
size_t index = hash(entry.key);
// Search for the entry in the linked list at the index
Node<RecordType>* cursor = list_search(data[index], entry.key);
// If the entry is found, replace it
if (cursor != nullptr)
{
cursor->data = entry;
} // Otherwise, insert the entry at the beginning of the list
else
{
list_head_insert(data[index], entry);
total_records++;
}
}
This is for a chained hashing assignment and the functions called in insert are prewritten and part of a linked list toolkit.
I am expecting print to print the newly inserted values from the driver file.