Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help code in c++ CODE: #include #include #include using namespace std; struct hashNode; typedef hashNode *NodePtr; struct hashNode { /* node to store data in

Help code in c++

image text in transcribed

image text in transcribed

image text in transcribed

CODE:

#include

#include

#include

using namespace std;

struct hashNode;

typedef hashNode *NodePtr;

struct hashNode

{

/* node to store data in linked list */

int age;

char name[13]; //max size 12 + NULL or 11 + + \0

NodePtr link;

};

struct hash

{

hash();

void InsertNode(const char* name, int age);

void SearchAndDelete(int age);

void printList();

hashNode *first;

};

/* Constructor, initializes values of Linked List */

hash::hash()

{

first = NULL;

}

/* Insert function to create a new node with parameters name and age and inserts them into

* linked list at the start of it. */

void hash::InsertNode(const char* name, int age)

{

//Your Code Here

//alloc new node

//copy name to node

//load all values to node

//if empty, set first to a newNodePtr

//else sets it at the front (start) of linked list

}

/* Function finds if linked list has a node with that age and deletes it

then continues

* searching for more until the end of the linked list. Before deleting

the node, it prints

* the name of the person found along with what it searched for, if

nothing was found it

* prints to screen a message saying not found. Parameters passed: the

age to look for */

void hash::SearchAndDelete(int age)

{

string notf = "not found.";

NodePtr current = first;

NodePtr prev = current;

bool itemfound = false;

bool printedfoundmsg = false;

NodePtr dtemp;

cout

//Your Code Here

if (itemfound == false)

cout

else

cout

}

/* Void Function to print a linked list to the screen. Prints name & age

of each node */

void hash::printList()

{

bool firstprint = false; //for printing formatting

NodePtr current = first;

while(current != NULL)

{

if(!firstprint)

{ //for formatting purposes to print the comma/period correctly

cout name age

firstprint = true;

}

else

cout name age

current = current>link;

}

if(firstprint) cout

}

/* Void Function to print out hash table, function parameters are the

hashtable. The

* function requires another function called printList to print

individual indexes.

* Function prints to the screen*/

void printHashTable(hash* hashtable)

{

for (int i = 0; i

{

cout

hashtable[i].printList();

cout

}

}

/* Value Returning Hash Function. Input Parameter is the age of the

person, and the output

* is where to place it. Function is h(x) = x mod 10 , where x is the age. Function

* depends on table size of 10 and will return value from 09 of where

in array it goes */

int hashFunction(int age)

{

//Your Code Here

}

int main(int argc, char **argv)

{ //Start of Main

if(argc !=3)

cout

else

{

//If correct command line argument then program proceeds, otherwise it terminates

char inputname[13];

int inputage;

hash hashtable[10]; //Hash Table size 10

ifstream inf;

inf.open(argv[1]); //open input file with names and ages to store

inf >> inputname >> inputage; //prime read to make sure file isn't empty

while(inf)

{

//Read until end of file

//Call hash function find where to input data in array[x] and then insert it

hashtable[hashFunction(inputage)].InsertNode(inputname, inputage);

inf >> inputname >> inputage; // read next line

} // End of main read while loop

inf.close();

printHashTable(hashtable);

cout

inf.open(argv[2]); //open input file with ages to search for

inf >> inputage; //prime read to make sure file isn't empty

while(inf)

{ //Read until end of file

//Call hash function to find where to search in array and then try deleting

hashtable[hashFunction(inputage)].SearchAndDelete(inputage);

inf >> inputage; //read next line

} // End of main read while loop

inf.close();

cout

printHashTable(hashtable);

}

return (0);

} //End of Main

Hashing In this assignment you will practice using Hash Tables. This C++ program will open and read (use filestreams) two files and output text to the stdout (screen). The first will contain one or more lines consisting of a name and an age in that order. The program will read both of those values and store them in a hash table (using separate chaining) with a size of 10. The hash function is h(x) x mod 10 where x is the age. The hash table is an array 10] of pointers to 10 individual linked lists. Each item is inserted into the appropriate li the end of the list to add and keeps the most recent values at the front. Then the program prints out arn index showing each linked list and what it contains; for example, nked list at the front of the list. That way there is no need to traverse to Index 8John (68) The second file contains integers(one per line) which represent ages to search for, print and delete. In detail, each integer is read, passed through the hash function and if there are matching items found, then they are printed and then removed from linked list/hash table. Finally after the search and removal of integers in the second file, the full table is reprinted again to show its current state after removals. To accomplish this, take the skeleton code given in the next pages and complete it. In addition here are to some of the key methods/pieces to the struct hashNode This struct should have 3 members: age, name, and NodePtr link. struct hash This stuct should contain the hash, InsertNode, SearchAnDelete, and printList method along with a pointer of hashNode type. hash::hash) This is the Constructor and should initializes values of the Linked List. void hash::InsertNode (const char* name, int age) This is an Insert function to create a new node with parameters name and age and inserts them into the linked list at the start of it. void hash::SearchAndDelete(int age) This Function finds if linked list has a node with that age and deletes it then continues searching for more until the end of the linked list. Before deleting the node, it prints the name of the person found along with what it searched for, if nothing was found it prints to screen a message saying not found. Parameters passed: the age to look for. void hash::printList () This Void Function to print a linked list to the screen. Prints name & age of each node

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

Students also viewed these Databases questions