Question

C++ assignment - Hash table with linear probing --- implement in single file - main.cpp You...

C++ assignment - Hash table with linear probing --- implement in single file - main.cpp

You are asked to implement a very specific hash table. The keys are lower-case English words (e.g., apple, pear). The length of a key is at most 10. The hash function is “simply using the last character”. That is, the hash value of apple should be e, and the hash value of pear should be r. Your hash table contains exactly 26 slots (hash value a to hash value z). The total number of English words/keys you need to deal with is at most 26, so the table is never too small. A table slot has three different statuses: “never used”, “tombstone”, and “occupied”. Table starts with 26 “never used” slots.

Searching works as follows: given a key, take its last character as the hash value. First try the corresponding table slot, if the objective key is there, then you have found it. If the corresponding slot is never used, terminate because we are certain that the objective is not in the table. If the slot is occupied but it’s not the objective, or the slot is a “tombstone”, then we move on to the next slot (may need to wrap around the table if the current slot is the last one). We keep trying until we either find the key or are certain that the key does not exist in the table.

Insertion works as follows: First perform searching to ensure that the key does not exist. If it already exists, then do nothing. If it does not, take the last character of a key as the hash value. If the corresponding table slot is not occupied (either “never used” or “tombstone”), put the key there (the slot is now occupied). If the corresponding slot is already occupied, try the next slot. Repeat trying until you find an unoccupied slot.

Deletion works as follows: given a key, use the searching process to locate its slot. (If the key is not in the table, then do nothing.) Once you find the key, change the slot status to “tombstone”.

You should start your program by initializing an empty hash table.

Your program takes one line as input. The input line contains n “modification moves” separated by spaces (1 ≤ n ≤ 26). The available
modification moves are:-

• AWord (Character A followed by a lower-case English word of length at most 10): Aapple means insert key apple into the hash table. If apple is already in the table, do nothing.

• DWord (Character D followed by a lower-case English word of length at most 10): Dapple means delete key apple from the hash table. If apple is not in the tree, do nothing.

At the end, you need to go through the slots from a to z, and output all the keys separated by space.

You don’t need to worry about invalid inputs.

Sample input 1: Aaaa Accc Abbb
Sample output 1: aaa bbb ccc
Sample input 2: Abba Aaaa Acca
Sample output 2: bba aaa cca
Sample input 3: Abba Aaaa Acca Daaa
Sample output 3: bba cca

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main()
{
string s[26];
string str;
for(int i=0;i< 26;i++)
{
s[i]="never used";
}
cout << "Enter a string: ";
getline(cin, str);
  
vector <string> tokens;
  
stringstream check1(str);
  
string intermediate;
  
while(getline(check1, intermediate, ' '))
{
tokens.push_back(intermediate);
}
for(int i=0;i<tokens.size();i++)
{
  
if(tokens[i][0]=='A')
{
int len=tokens[i].length()-1;
char c=tokens[i][len];
string A=tokens[i].erase(0,1);
for(int j=c-97;j<26;j++)
{
int c=0;
if(s[j]=="never used" || s[j]=="tombstone")
{
s[j]=A;
break;
}
if(j==25)
{
j=0;
}
c++;
if(c==26)
{
break;
}
}
}
if(tokens[i][0]=='D')
{
string D=tokens[i].erase(0,1);
for(int i=0;i<26;i++)
{
if(s[i]==D)
{
s[i]="tombstone";
break;
}
}
}
}
for(int i=0;i< 26;i++)
{
if(s[i]!="never used" && s[i]!="tombstone")
{
cout<<s[i]<<" ";
}
  
}
  

return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ assignment - Hash table with linear probing --- implement in single file - main.cpp You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy.   You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable:             HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...

  • Insert elements into a hash table implemented using chain hashing, as an array of linked list...

    Insert elements into a hash table implemented using chain hashing, as an array of linked list in which each entry slot is as a linked list of key/value pairings that have the same hash (outcome value computed using certain hash function). You are allowed to use “Hash Function”, h(k) = k % x where, x is the value you will need to decide to use, that you find appropriate for this implementation. The main requirements are the following: 1. Input:...

  • in C++ Code should work for all cases In this assignment you are requested to implement...

    in C++ Code should work for all cases In this assignment you are requested to implement insert, search, and delete operations for an open-addressing hash table with double hashing. Create an empty hash table of size m= 13. Each integer of the input will be a key that you should insert into the hash table. Use the double hashing function h{k, i) = (hı(k) + ih2(k)) mod 13 where hi(k)= k mod 13 and h2(k) = 1+(k mod 11). The...

  • In C++, create a hash table for names (first name and last name only, no telephone...

    In C++, create a hash table for names (first name and last name only, no telephone numbers), and use the following to generate the keys: unsigned char key = 0; for (int i = 0; i < fullName.length(); i++) key ^= fullName[i]; Use the following names to test your program: Uzoma Acholonu Giuliana Asmad Michael Atkins-Combs Vishnu Bakthisaran Christopher Blowars William Bronson Trevor Butcher Tiffany Caceres-Bonilla Dulce Castro David Cifuentes Daniel Coursen Alexandra Davilla Amanda Dewitt Alfredo Diaz Jan Espinosa...

  • Following class is only part of my program that uses a hash table to simulate a...

    Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...

  • Project Description Allow the user to specify M (the size of the hash table) then, (1)...

    Project Description Allow the user to specify M (the size of the hash table) then, (1) add items found in text file "Project1.txt" to h, an initially-empty instance of a HASH (reject all items that have a key that duplicates the key of a previously added item—item keys must be unique); (2) display h (see format shown below); (3) delete 4 randomly-chosen items from the h; and finally, (4) display h. Note M must be a prime number, so your...

  • 1 Objective Build a hashing algorithm that is suitable for use in a Bloom Filter. Please...

    1 Objective Build a hashing algorithm that is suitable for use in a Bloom Filter. Please note that while a cryptographic hash is quite common in many Bloom Filters, the hashing algorithm to be implemented is a mix of the the following algorithmic models, specifically, a multiply & rotate hash colloquially known as a murmur hash, and an AND, rolale, & XOR hash colloquially known as an ARX hash. 2 Requirements • Inputs. Read the input file which contains strings...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT