Question

Write the code to make a complete copy of a hash table (using chaining) of the last chain (that exists) into a new linear lin with c++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Const int DEFAULT_TABLE_SIZE=128;

Class HashMap {

Private:

float threshold;

Int maxSize;

Int tableSize;

Int Size;

LinkedHashEntry** table;

Void resize() {

int oldTableSize= tableSize;

tableSize *= 2;

max Size=(int) (tableSize*threshold) ;

LinkedHashEntry **oldTable= table;

table = new LinkedHashEntry * (tableSize);

for ( int I =0 ; i < tableSize; i++)

table (i)= NULL;

Size = 0;

for ( int hash =0;

hash < oldTableSize; hash++)

if (oldTable(hash)! = NULL) {

LinkedHashEntry * oldEntry;

LinkedHashEntry * entry =oldTable (hash) ;

while

(entry! =NULL) {

put ( entry-> getNext() ;

delete oldEntry;

}}

delete() oldTable;

}

PUBLIC:

HashMap() {

threshold =0.75 f;

maxmaxSize= 96;

tableSize=DEFAULT_TABLE_SIZE;

size=0;

table=new LinkedHashEntry*(tableSize) ;

For ( int i= 0; i<tableSize; I++)

table (I) =NUll;(

}

Void setThreshold(float threshold) {

this->threshold=threshold;

maxSize=(int) (tableSize* threshold) ;}

Int get (int key) {

Int hash=(key% tableSize) ;

If(table( hash) == NULL)

return -1;else{

LinkedHashEntry* entry = table [ hash];

While (entry! = NULL &&entry->getNext () ;

If( entry==NULL)

return -1;else return entry->get value () ;

}}

void put ( int key, int value) {

int hash=(key% tableSize) ;

If ( table (hash) == NULL) {

table (hash) = new LinkedHashEntry ( key, value) ;

size++;}else{

LinkedHashEntry* entry = table ( hash) ;

while ( entry-> getNext()! = NULL)

entry= entry->getNext() ;

if entry->getKey()==key) entry->setValue(value) ;

else{

entry->setNext(new LinkedHashEntry (key, value)) ;

size++;}}

if (size>=maxsize)

resize() ;}

void remove( int key) {

int hash = ( key% tableSize) ;

if (table(hash)! =NULL) }

LinkedHashEntry*prevEntry= NULL;

LinkedHashEntry * Entry= table (hash) ;

While ( entry->getNext()! =NULL &&entry->getKey()! =key) {

prevEntry =entry;

entry=entry-> getNext () ;}

if ( entry->getKey() ==key) {

If ( prevEntry == NULL) {

LinkedHashEntry* next Entry=entry-> get Next () ;

delete entry;

Table ( hash) = next Entry;}else{

LinkedHashEntry* next = entry->get Next () ;

delete entry;

prevEntry->setNext( next) ;}

size--;

}}}

HashMap ()

{for ( int hash=0;

hash < table Size; hash++)

if ( table ( hash! = NULL) {

LinkedHashEntry * prevEntry= NULL;

LinkedHashEntry * entry= table ( hash) ;

while ( entry! = NULL) {

PrevEntry= Entry;

entry= entry-> getNext() ;

Delete prevEntry;}}

delete [] table;}

};

Add a comment
Know the answer?
Add Answer to:
with c++ Write the code to make a complete copy of a hash table (using chaining) of the last chain (that exists) into a new linear linked list. Assume you know the size of the hash table (size M)....
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
  • 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:...

  • Let 'M' denote the hash table size. Consider the following four different hash table implementations: a....

    Let 'M' denote the hash table size. Consider the following four different hash table implementations: a. Implementation (I) uses chaining, and the hash function is hash(x)x mod M. Assume that this implementation maintains a sorted list of the elements (from biggest to smallest) for each chain. b. Implementation (II) uses open addressing by Linear probing, and the hash function is ht(x) - (hash(x) + f(i)) mod M, where hash(x)x mod M, and f(i)- c. Implementation (III) uses open addressing by...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

  • 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...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

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