Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the function to print all items in my hash table. Line 163 (Image included of where I'm stuck) //============================================================================ // Name

I need help with the function to print all items in my hash table. Line 163 (Image included of where I'm stuck)

//============================================================================ // Name : HashTable.cpp // Author : // Version : 1.0 // Copyright : // Description : Hello World in C++, Ansi-style //============================================================================

#include #include #include #include #include

#include "CSVparser.hpp"

using namespace std;

//============================================================================ // Global definitions visible to all methods and classes //============================================================================

const unsigned int DEFAULT_SIZE = 179;

// forward declarations double strToDouble(string str, char ch);

// define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } };

//============================================================================ // Hash Table class definition //============================================================================

/** * Define a class containing data members and methods to * implement a hash table with chaining. */ class HashTable {

private: // FIXME (1): Define structures to hold bids

struct Node { Bid bid; unsigned key; Node* next;

Node() { key = UINT_MAX; next = nullptr; } // initiliaze structure with a bid Node(Bid aBid) : Node() { bid = aBid;

} Node(Bid aBid, unsigned aKey) : Node(aBid) { key = aKey; } };

vector nodes;

unsigned tableSize = DEFAULT_SIZE;

unsigned int hash(int key);

public: HashTable(); HashTable(unsigned size); virtual ~HashTable(); void Insert(Bid bid); void PrintAll(); void Remove(string bidId); Bid Search(string bidId); };

/** * Default constructor */ HashTable::HashTable() { // FIXME (2): Initialize the structures used to hold bids nodes.resize(tableSize); } HashTable::HashTable(unsigned size) { this->tableSize = size; nodes.resize(tableSize);

}

/** * Destructor */ HashTable::~HashTable() { nodes.erase(nodes.begin());

// FIXME (3): Implement logic to free storage when class is destroyed }

/** * Calculate the hash value of a given key. * Note that key is specifically defined as * unsigned int to prevent undefined results * of a negative list index. * * @param key The key to hash * @return The calculated hash */ unsigned int HashTable::hash(int key) { // FIXME (4): Implement logic to calculate a hash value return key % tableSize; }

/** * Insert a bid * * @param bid The bid to insert */ void HashTable::Insert(Bid bid) { // FIXME (5): Implement logic to insert a bid unsigned key = hash(atoi(bid.bidId.c_str()));

//retrieve node using key Node* oldNode = &(nodes.at(key));

// if no node found if (oldNode == nullptr) { Node* newNode = new Node(bid, key); nodes.insert(nodes.begin() + key, (*newNode));

} else { //if node is found if (oldNode->key == UINT_MAX) { oldNode->key = key; oldNode->bid = bid; oldNode->next = nullptr;

} else { while (oldNode->next != nullptr) { oldNode = oldNode->next; } oldNode->next = new Node(bid, key); } } }

/** * Print all bids */ void HashTable::PrintAll() { // FIXME (6): Implement logic to print all bids int number; for (int i = 0; i

}

}

/** * Remove a bid * * @param bidId The bid id to search for */ void HashTable::Remove(string bidId) { // FIXME (7): Implement logic to remove a bid unsigned key = hash(atoi(bidId.c_str())); nodes.erase(nodes.begin() + key); }

/** * Search for the specified bidId * * @param bidId The bid id to search for */ Bid HashTable::Search(string bidId) { Bid bid;

// FIXME (8): Implement logic to search for and return a bid unsigned key = hash(atoi(bidId.c_str())); Node* node = &(nodes.at(key));

if (node == nullptr || node->key != UINT_MAX) { return bid; }

if (node != nullptr && node->key != UINT_MAX && node->bid.bidId.compare(bidId)==0) { return node->bid; } while (node != nullptr) { if (node->key != UINT_MAX && node->bid.bidId.compare(bidId) == 0) { return node->bid; } node = node->next; }

return bid; }

//============================================================================ // Static methods used for testing //============================================================================

/** * Display the bid information to the console (std::out) * * @param bid struct containing the bid info */ void displayBid(Bid bid) { cout

/** * Load a CSV file containing bids into a container * * @param csvPath the path to the CSV file to load * @return a container holding all the bids read */ void loadBids(string csvPath, HashTable* hashTable) { cout

// initialize the CSV Parser using the given path csv::Parser file = csv::Parser(csvPath);

// read and display header row - optional vector header = file.getHeader(); for (auto const& c : header) { cout

try { // loop to read rows of a CSV file for (unsigned int i = 0; i

// Create a data structure and add to the collection of bids Bid bid; bid.bidId = file[i][1]; bid.title = file[i][0]; bid.fund = file[i][8]; bid.amount = strToDouble(file[i][4], '$');

//cout

// push this bid to the end hashTable->Insert(bid); } } catch (csv::Error & e) { std::cerr

/** * Simple C function to convert a string to a double * after stripping out unwanted char * * credit: http://stackoverflow.com/a/24875936 * * @param ch The character to strip out */ double strToDouble(string str, char ch) { str.erase(remove(str.begin(), str.end(), ch), str.end()); return atof(str.c_str()); }

/** * The one and only main() method */ int main(int argc, char* argv[]) {

// process command line arguments string csvPath, bidKey; switch (argc) { case 2: csvPath = argv[1]; bidKey = "98109"; break; case 3: csvPath = argv[1]; bidKey = argv[2]; break; default: csvPath = "eBid_Monthly_Sales_Dec_2016.csv"; bidKey = "98109"; }

// Define a timer variable clock_t ticks;

// Define a hash table to hold all the bids HashTable* bidTable{};

Bid bid;

int choice = 0; while (choice != 9) { cout > choice;

switch (choice) {

case 1: bidTable = new HashTable();

// Initialize a timer variable before loading bids ticks = clock();

// Complete the method call to load the bids loadBids(csvPath, bidTable);

// Calculate elapsed time and display result ticks = clock() - ticks; // current clock ticks minus starting clock ticks cout

case 2: bidTable->PrintAll(); break;

case 3: ticks = clock();

bid = bidTable->Search(bidKey);

ticks = clock() - ticks; // current clock ticks minus starting clock ticks

if (!bid.bidId.empty()) { displayBid(bid); } else { cout

cout

case 4: bidTable->Remove(bidKey); break; } }

cout

return 0; } image text in transcribed

162 163 void HashTable::PrintAll() { // FIXME (6): Implement logic to print all bids int number; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions

Question

Buddy Dog Foods management to change its focus?

Answered: 1 week ago