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

  • PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and...

    PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...

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

  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • Creating linked list data structure Overview The purpose of this assignment is for you to write...

    Creating linked list data structure Overview The purpose of this assignment is for you to write a data structure called a Linked List, which utilizes templates (similar to Java’s generics), in order to store any type of data. In addition, the nature of a Linked List will give you some experience dealing with non-contiguous memory organization. This will also give you more experience using pointers and memory management. Pointers, memory allocation, and understand how data is stored in memory will...

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

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