Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hash Table: Given code below ------------------------------- main.cpp #include #include #include Hash.h using namespace std; int main(int argc,char **argv) { // read the number of buckets

image text in transcribedimage text in transcribed

Hash Table: Given code below

-------------------------------

main.cpp

#include

#include

#include "Hash.h"

using namespace std;

int main(int argc,char **argv) {

// read the number of buckets

int bNo; cin >> bNo;

Hash table(bNo);

// Using a fixed size buffer for reading content is not always safe,

// but ok here because we know how our input has to be:

char line[100];

// Main loop

while ( std::cin.getline(line,100) ) {

string str(line);

if ( str.size()==0 ) continue;

if ( str[0]=='e' ) return 0;

// Use cerr if you want to always print to the console, because cout

// will be redirected to the output file when calling the Grade05 script:

// cerr

if ( str.substr(0,1)=="o" ) {

table.Print();

cout

}

else {

int key;

stringstream convert_stm(str.substr(2, str.size()-1));

if ( !(convert_stm>>key) ) key = -1;

if (str.substr(0,1) == "i") {

table.Insert(key);

}

else {

if (str.substr(0,1) == "d") {

bool ret = table.Delete(key);

if (true == ret) cout

else cout

cout

}

else if (str.substr(0,1) == "s") {

int b, p;

bool ret = table.Search(key, b, p);

if (true == ret) cout

else cout

cout

}

}

}

}

return 0;

}

----------------------------------------------

Hash.h

#ifndef HASH_H

#define HASH_H

#include

#include

using namespace std;

class Hash {

// Public Functions/Variables

public:

Hash(int _bNo); // Constructor

virtual ~Hash(); // Destructor

void Insert(int toInsert);

// return true if the key is deleted; false, if there is no such key

bool Delete(int toDelete);

// return true if key is found and corresponding position; false, otherwise

bool Search(int key, int& _bucket, int& _pos);

void Print();

// Private Functions/Variables

private:

int bNo;

vector > table;

};

#endif

------------------------------------------------------

Hash.cpp

#include

#include

#include

#include

#include "Hash.h"

using namespace std;

/****************************************************************

* CONSTRUCTOR

****************************************************************/

Hash::Hash(int _bNo) {

}

/****************************************************************

* DESTRUCTOR

****************************************************************/

Hash::~Hash() {

// Write code to remove and deallocate everything

}

void Hash::Insert(int toInsert) {

// Write your code here

}

bool Hash::Delete(int toDelete) {

// Write your code here

return true;

}

bool Hash::Search(int key, int& _bucket, int& _pos) {

// Write your code here

return true;

}

void Hash::Print() {

// Write your code here

}

In this assignment you are requested to implement a hash table that handles collisions with chaining. You have to use linked sts, either from the Standard Template Library (STL) (recommended) or by implementing your own. For usage of STL, refer for instance to http //www.cppreference.com/wiki. A sample class structure, with empty methods, is given in the support code. You can either use the given class structure or create your own. In this assign- ment the keys are integers. Your execution file name must be "Hash.exe". You will be running "./Grade05" to test your code. Refer to the previous lab assignments for more details on how to use the testing tools. You have to implement the insert, search and delete operations. The keys are integers (C+ int type) and you can assume that all keys k are non-negative. The first number in the input will be the size m of the chained hash table. You will use the simple hash function h(k)-k mod m. The input contains one of the following commands on a line: key Insert key into the hash table. For example, "i 2" means "Insert key 2 into the hash table". For collisions, insert the colliding key at the beginning of the linked list. You just need to insert the key and do not have to output anything in this case. d key: Delete key from the hash table. For example, "d 2" means "Delete key 2 from the hash table". Do nothing if the hash table does not contain the key. If there are multiple elements with the same key value, delete just one element. If the delete is successful, you have to output: key : DELETED If not (since there was no element with the given key), output: key : DELETE FAILED s key: Search key in the hash table. If there is an element with the key value, then you have to output: key: FOUND AT i,j where i is the hash table index and j is the linked list index. If there are multiple elements with the same key value, choose the first one appearing in the linked list. If you do not find the key, then output key : NOT FOUND :Output the hash table. Output the entire hash table. Each line should begin with the slot/hash table index followed by key values in the linked list. For example, if m= 3 and we inserted 3, 6, and 1 into an empty table in this order, then you should output: 0:6->3 e: Finish your program

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_2

Step: 3

blur-text-image_3

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions

Question

=+ What topics should be provided in training programs?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago